参考:https://www.bookstack.cn/read/gin-EDDYCJY-blog/golang-gin-2018-03-26-Gin%E5%AE%9E%E8%B7%B5-%E7%95%AA%E5%A4%96-Golang%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91.md
在项目目录中运行下面命令:
GOOS=linux GOARCH=386 go build -o hangqing-chandi .
Dockerfile
定制go程序镜像在项目目录下创建下面两个文件
Dockerfile文件内容:
FROM golang
MAINTAINER weihaoyu
WORKDIR /go/src/hangqing-chandi
COPY . .
EXPOSE 8080
CMD ["/bin/bash", "/go/src/hangqing-chandi/start.sh"]
start.sh脚本内容:
#!/usr/bin/env bash
cd /go/src/hangqing-chandi/ && ./hangqing-chandi
chmod +x start.sh
docker build -t hangqing-chandi .
Sending build context to Docker daemon 597.1MB
Step 1/6 : FROM golang
latest: Pulling from library/golang
dc65f448a2e2: Pull complete
346ffb2b67d7: Pull complete
dea4ecac934f: Pull complete
8ac92ddf84b3: Pull complete
7ca605383307: Pull complete
f47e6cebc512: Waiting
f47e6cebc512: Pull complete
530350156010: Pull complete
Digest: sha256:fe6b1742d48c4d6d360c6fac1e289e8529aaab924fa0e49a330868be50c0f2f4
Status: Downloaded newer image for golang:latest
---> 297e5bf50f50
Step 2/6 : MAINTAINER weihaoyu
---> Running in e9c60172650c
Removing intermediate container e9c60172650c
---> 7a1fa325aa47
Step 3/6 : WORKDIR /go/src/hangqing-chandi
---> Running in f499eab438aa
Removing intermediate container f499eab438aa
---> 7b668a52514c
Step 4/6 : COPY . .
---> b533255d1799
Step 5/6 : EXPOSE 777
---> Running in 1fb3280e6b66
Removing intermediate container 1fb3280e6b66
---> 933fe1e02708
Step 6/6 : CMD ["/bin/bash", "/go/src/hangqing-chandi/script/start.sh"]
---> Running in c51654e09c40
Removing intermediate container c51654e09c40
---> ceb4e2185452
Successfully built ceb4e2185452
Successfully tagged hangqing-chandi:latest
localhost:hangqing-chandi why$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hangqing-chandi latest ceb4e2185452 2 minutes ago 1.39GB
golang latest 297e5bf50f50 8 days ago 803MB
mysql latest d435eee2caa5 3 months ago 456MB
containerize/elastichd latest c2202f76db37 2 years ago 28.1MB
version: '3'
networks:
basic:
services:
hangqing-chandi:
container_name: hangqing-chandi
image: hangqing-chandi
ports:
- "777:8080"
volumes:
- ./hangqing-chandi:/go/src/hangqing-chandi/hangqing-chandi:rw
networks:
- basic
$ docker-compose -f docker-compose.yml up -d hangqing-chandi
Creating hangqing-chandi ... done
显示如上图则启动成功
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a627a3839a9b hangqing-chandi "/bin/bash /go/src/h…" 2 minutes ago Up 2 minutes 0.0.0.0:777->777/tcp hangqing-chandi
docker exec -it a627a3839a9b /bin/bash
root@a627a3839a9b:/go/src/hangqing-chandi#
如果需要更新代码,在docker中git pull最新代码即可(容器里的git可以在Dockerfile文件中加入RUN yum install git,RUN命令在docker build时执行,可参考:https://www.runoob.com/docker/docker-dockerfile.html)
然后在容器中平滑重启:https://blog.csdn.net/why444216978/article/details/104225690
$ docker-compose logs -f hangqing-chandi
Attaching to hangqing-chandi
hangqing-chandi | [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
hangqing-chandi | - using env: export GIN_MODE=release
hangqing-chandi | - using code: gin.SetMode(gin.ReleaseMode)
hangqing-chandi |
hangqing-chandi | [GIN-debug] GET /ping --> hangqing-chandi/routers.InitRouter.func1 (5 handlers)
hangqing-chandi | [GIN-debug] POST /hangqing_chandi/api/customer_origin_list --> hangqing-chandi/routers.InitRouter.func2 (5 handlers)
hangqing-chandi | 2020/02/22 14:09:02 Actual pid is 7
hangqing-chandi | map[query:map[] reqBody: reqHeader:map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9] Accept-Encoding:[gzip, deflate, br] Accept-Language:[zh-CN,zh;q=0.9] Cache-Control:[max-age=0] Connection:[keep-alive] Sec-Fetch-Mode:[navigate] Sec-Fetch-Site:[cross-site] Sec-Fetch-User:[?1] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36]] respBody:{"data":{},"errmsg":"success","errno":0,"user_msg":""}
hangqing-chandi | ]
hangqing-chandi | map[query:map[] reqBody: reqHeader:map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9] Accept-Encoding:[gzip, deflate, br] Accept-Language:[zh-CN,zh;q=0.9] Cache-Control:[max-age=0] Connection:[keep-alive] Sec-Fetch-Mode:[navigate] Sec-Fetch-Site:[cross-site] Sec-Fetch-User:[?1] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36]] respBody:{"data":{},"errmsg":"success","errno":0,"user_msg":""}
hangqing-chandi | ]