Skip to content

Instantly share code, notes, and snippets.

@kevwan
Last active November 5, 2022 01:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kevwan/1fe6443a03ce196f57fd69bafe2bf0d8 to your computer and use it in GitHub Desktop.
Save kevwan/1fe6443a03ce196f57fd69bafe2bf0d8 to your computer and use it in GitHub Desktop.
The simplest way to write Dockerfile!

1. choose a simple linux image

For example alpine, it's only about 5MB.

2. set timezone if necessary

RUN apk add --no-cache tzdata
ENV TZ America/New_York

3. multi-stage build

  • the first stage to build the binary, ensure the binary is dev box independent
  • use the binary in the first stage to build the final image

4. the simplest way to write Dockerfile

  • first of all, install the tool goctl

    GO111MODULE=on go get -u github.com/tal-tech/go-zero/tools/goctl

  • in project greet, create a service called hello

    goctl api new hello

    file structure listed below:

    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
    
  • in dir hello, one shot to generate Dockerfile

    goctl docker -go hello.go

    the content of Dockerfile like:

    FROM golang:alpine AS builder
    
    LABEL stage=gobuilder
    
    ENV CGO_ENABLED 0
    ENV GOOS linux
    
    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 America/New_York
    
    WORKDIR /app
    COPY --from=builder /app/hello /app/hello
    COPY --from=builder /app/etc /app/etc
    
    CMD ["./hello", "-f", "etc/hello-api.yaml"]
  • in dir greet, build the final image

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

  • check the image

    hello v1 5455f2eaea6b 7 minutes ago 18.1MB

    the image size is like 18MB.

  • start the service

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

  • test the service

    $ 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":""}

5. conclusion

goctl greatly simplifies the writing of Dockerfile. The generated Dockerfile is state-of-art and can be used directly. Also, you can customized the Dockerfile template in $HOME/.goctl/docker if necessary.

If it helps, welcome to star the project. 🤝

6. go-zero

https://github.com/tal-tech/go-zero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment