Skip to content

Latest commit

 

History

History
61 lines (56 loc) · 2 KB

docker_prod.md

File metadata and controls

61 lines (56 loc) · 2 KB

build image

It a little different from Dev env, since it needn't recompilation when code change.

一般的Dockerfile都会 go get 所有的库,在images中编译,

FROM golang
RUN go get -u github.com/astaxie/beego
RUN go get -u github.com/beego/bee
ADD app  /go/src/app
WORKDIR /go/src/app
EXPOSE 8080
CMD ["bee", "run"]

这对用的CI/CD说不错,但会增加image体积.通常的解决方案是分多个stage,

FROM golang
WORKDIR /src
COPY hello.go .
RUN go build hello.go
FROM ubuntu  #开始stage 1
COPY --from=0 /src/hello .  #copy stage 0生成的文件
CMD ["./hello"]

不过最简单的方法还是本地静态编译生成可执行文件

FROM scratch
LABEL maintainer="xuyc@sina.com"
ADD . /
EXPOSE 80
CMD ["/myapp"]

只要开发的环境与运行的环境ABI兼容,这样直接copy二进制文件是没问题的。

$ script/create_image.sh 
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
justware/myapp      1.0                 101691d27dfc        8 minutes ago       11.7MB

run image

$ docker run -it --rm  -p 80:80  justware/myapp:1.0
2020/06/03 13:27:22.666 [I] [asm_amd64.s:1373]  http server Running on http://:80
2020/06/03 13:27:30.443 [D] [server.go:2807]  |     172.17.0.1| 200 |   7.677614ms|   match| GET      /     r:/
2020/06/03 13:27:30.498 [D] [server.go:2807]  |     172.17.0.1| 304 |    221.102µs|   match| GET      /static/js/reload.min.js

publish

$ script/docker_publish.sh 
The push refers to repository [docker.io/justware/myapp]
1bcc18978517: Pushed 
1.0: digest: sha256:af2e736d15bf832279d5c560c449fb252988586a534c88203c20da69d2009e44 size: 528

run in k8s

tested too, since the node's OS is Linux too.

CI/CD