https://studygolang.com/articles/12670
https://studygolang.com/articles/11547
将Golang应用部署到Docker
三、重新构建镜像
重复先前的步骤,回到 gin-blog
的项目根目录下执行 docker build -t blog .
四、创建并运行一个新容器
关联
Q:我们需要将 Golang
容器和 Mysql
容器关联起来,那么我们需要怎么做呢?
A:增加命令 --link dockermysql:mysql
让 Golang
容器与 Mysql
容器互联;通过 --link
,可以在容器内直接使用其关联的容器别名进行访问,而不通过IP,但是--link
只能解决单机容器间的关联,在分布式多机的情况下,需要通过别的方式进行连接
运行
执行命令 docker run --link dockermysql:mysql -p 8080:8080 blog
$ docker run --link dockermysql:mysql -p 8080:8080 blog
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
...
Actual pid is 1
结果
检查启动输出、接口测试、数据库内数据,均正常;我们的 Golang
容器和 Mysql
容器成功关联运行,大功告成 :)
[root@VM_0_5_centos go_blog_src]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest c7109f74d339 3 weeks ago 443MB [root@VM_0_5_centos go_blog_src]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fd03062b8390 mysql "docker-entrypoint.s…" 3 days ago Up 10 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp dockermysql [root@VM_0_5_centos go_blog_src]# docker build -t blog . Sending build context to Docker daemon 20.67MB Step 1/5 : FROM scratch ---> Step 2/5 : WORKDIR $GOPATH/src/go_blog_src ---> Running in 94788ca76455 Removing intermediate container 94788ca76455 ---> a3401ac5c76b Step 3/5 : COPY . $GOPATH/src/go_blog_src ---> 6aa4d7103155 Step 4/5 : EXPOSE 8080 ---> Running in 78d720992aa5 Removing intermediate container 78d720992aa5 ---> 918bd1f06448 Step 5/5 : CMD ["./main"] ---> Running in 6d67476b66d4 Removing intermediate container 6d67476b66d4 ---> a7e009d4e487 Successfully built a7e009d4e487 Successfully tagged blog:latest [root@VM_0_5_centos go_blog_src]# docker run --link dockermysql:mysql -p 8080:8080 blog [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode) [GIN-debug] GET /export/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers) [GIN-debug] HEAD /export/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers) [GIN-debug] GET /upload/images/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers) [GIN-debug] HEAD /upload/images/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers) [GIN-debug] GET /qrcode/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers) [GIN-debug] HEAD /qrcode/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers) [GIN-debug] GET /auth --> go_blog_src/routers/api.GetAuth (3 handlers) [GIN-debug] POST /upload --> go_blog_src/routers/api.UploadImage (3 handlers) [GIN-debug] GET /api/v1/tags --> go_blog_src/routers/api/v1.GetTags (4 handlers) [GIN-debug] POST /api/v1/tags --> go_blog_src/routers/api/v1.AddTag (4 handlers) [GIN-debug] PUT /api/v1/tags/:id --> go_blog_src/routers/api/v1.EditTag (4 handlers) [GIN-debug] DELETE /api/v1/tags/:id --> go_blog_src/routers/api/v1.DeleteTag (4 handlers) [GIN-debug] POST /tags/export --> go_blog_src/routers/api/v1.ExportTag (3 handlers) [GIN-debug] POST /tags/import --> go_blog_src/routers/api/v1.ImportTag (3 handlers) [GIN-debug] GET /api/v1/articles --> go_blog_src/routers/api/v1.GetArticles (4 handlers) [GIN-debug] GET /api/v1/articles/:id --> go_blog_src/routers/api/v1.GetArticle (4 handlers) [GIN-debug] POST /api/v1/articles --> go_blog_src/routers/api/v1.AddArticle (4 handlers) [GIN-debug] PUT /api/v1/articles/:id --> go_blog_src/routers/api/v1.EditArticle (4 handlers) [GIN-debug] DELETE /api/v1/articles/:id --> go_blog_src/routers/api/v1.DeleteArticle (4 handlers) [GIN-debug] POST /api/v1/articles/poster/generate --> go_blog_src/routers/api/v1.GenerateArticlePoster (4 handlers) 2019/07/05 02:13:17 [info] start http server listening :8080 [GIN] 2019/07/05 - 02:13:22 | 200 | 19.832036ms | 183.15.181.255 | GET /auth?username=test&password=test123456
Dockerfile
FROM scratch WORKDIR $GOPATH/src/go_blog_src COPY . $GOPATH/src/go_blog_src EXPOSE 8080 CMD ["./main"]
reference
2、https://studygolang.com/articles/12670