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

