最简单的Go Dockerfile编写姿势,没有之一!

1,617 阅读2分钟

1. Dockerfile一些额外注意点

  • 选择最简单的镜像

    比如alpine,整个镜像5M左右

  • 设置镜像时区

    RUN apk add --no-cache tzdata
    ENV TZ Asia/Shanghai
    

2. 多阶段构建

  • 第一阶段构建否则构建出可执行文件,确保构建过程独立于宿主机
  • 第二阶段将第一阶段的输出作为输入,构建出最终的极简镜像

3. 完整Dockerfile编写过程

  • 首先安装 goctl 工具

    GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/go-zero/tools/goctl

  • greet 项目下创建一个 hello 服务

    goctl api new hello

    文件结构如下:

    greet
    ├── go.mod
    ├── go.sum
    └── service
        └── hello
            ├── etc
            │   └── hello-api.yaml
            ├── hello.api
            ├── hello.go
            └── internal
                ├── config
                │   └── config.go
                ├── handler
                │   ├── hellohandler.go
                │   └── routes.go
                ├── logic
                │   └── hellologic.go
                ├── svc
                │   └── servicecontext.go
                └── types
                    └── types.go
    
  • hello 目录下一键生成 Dockerfile

    goctl docker -go greet.go

    Dockerfile 内容如下:

    FROM golang:alpine AS builder
    
    LABEL stage=gobuilder
    
    ENV CGO_ENABLED 0
    ENV GOOS linux
    ENV GOPROXY https://goproxy.cn,direct
    
    WORKDIR /build/zero
    
    ADD go.mod .
    ADD go.sum .
    RUN go mod download
    COPY . .
    COPY service/hello/etc /app/etc
    RUN go build -ldflags="-s -w" -o /app/hello service/hello/hello.go
    
    
    FROM alpine
    
    RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
    ENV TZ Asia/Shanghai
    
    WORKDIR /app
    COPY --from=builder /app/hello /app/hello
    COPY --from=builder /app/etc /app/etc
    
    CMD ["./hello", "-f", "etc/hello-api.yaml"]
    
  • greet 目录下 build 镜像

    docker build -t hello:v1 -f service/hello/Dockerfile .

  • 查看镜像

    hello v1 5455f2eaea6b 7 minutes ago 18.1MB

    可以看出镜像大小约为18M。

  • 启动服务

    docker run --rm -it -p 8888:8888 hello:v1

  • 测试服务

    $ curl -i http://localhost:8888/from/you
    HTTP/1.1 200 OK
    Content-Type: application/json
    Date: Thu, 10 Dec 2020 06:03:02 GMT
    Content-Length: 14
    
    {"message":""}
    

4. 总结

goctl 工具极大简化了 Dockerfile 文件的编写,提供了开箱即用的最佳实践,并且支持了模板自定义。

如果觉得工具有帮助,欢迎 star 🤝

5. 项目地址

github.com/tal-tech/go…