Skip to content

Commit

Permalink
feat: Improve Docker build (zeromicro#3682)
Browse files Browse the repository at this point in the history
  • Loading branch information
reneleonhardt authored Mar 2, 2024
1 parent be7f939 commit b53ba76
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
**/.git
.dockerignore
Dockerfile
goctl
Makefile
readme.md
readme-cn.md
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

version: 2
updates:
- package-ecosystem: "docker" # Update image tags in Dockerfile
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "github-actions" # Update GitHub Actions
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "gomod" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: '1.19'
check-latest: true
cache: true
id: go
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
uses: actions/setup-go@v4
with:
# use 1.19 to guarantee Go 1.19 compatibility
go-version: 1.19
go-version: '1.19'
check-latest: true
cache: true

Expand Down
4 changes: 2 additions & 2 deletions readme-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromicro
# docker for amd64 architecture
docker pull kevinwan/goctl
# run goctl like
docker run --rm -it -v `pwd`:/app kevinwan/goctl goctl --help
docker run --rm -it -v `pwd`:/app kevinwan/goctl --help

# docker for arm64(Mac) architecture
docker pull kevinwan/goctl:latest-arm64
# run goctl like
docker run --rm -it -v `pwd`:/app kevinwan/goctl:latest-arm64 goctl --help
docker run --rm -it -v `pwd`:/app kevinwan/goctl:latest-arm64 --help
```

确保 goctl 可执行
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ go get -u github.com/zeromicro/go-zero
# docker for amd64 architecture
docker pull kevinwan/goctl
# run goctl like
docker run --rm -it -v `pwd`:/app kevinwan/goctl goctl --help
docker run --rm -it -v `pwd`:/app kevinwan/goctl --help

# docker for arm64(Mac) architecture
docker pull kevinwan/goctl:latest-arm64
# run goctl like
docker run --rm -it -v `pwd`:/app kevinwan/goctl:latest-arm64 goctl --help
docker run --rm -it -v `pwd`:/app kevinwan/goctl:latest-arm64 --help
```

make sure goctl is executable.
Expand Down
8 changes: 8 additions & 0 deletions tools/goctl/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test/
.dockerignore
.go-version
Dockerfile
goctl
Makefile
readme.md
readme-cn.md
24 changes: 15 additions & 9 deletions tools/goctl/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,33 @@ ENV GOPROXY https://goproxy.cn,direct
RUN apk update --no-cache && apk add --no-cache tzdata
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
RUN addgroup -g 1000 -S app && adduser -u 1000 -S app -G app

WORKDIR /build

ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .
RUN go mod download
RUN go build -ldflags="-s -w" -o /app/goctl ./goctl.go


FROM golang:alpine

RUN apk update --no-cache && apk add --no-cache protoc

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
COPY --from=builder /go/bin/protoc-gen-go /usr/bin/protoc-gen-go
COPY --from=builder /go/bin/protoc-gen-go-grpc /usr/bin/protoc-gen-go-grpc
COPY --from=builder /etc/passwd /etc/group /etc/
COPY --from=builder /usr/share/zoneinfo/ /usr/share/zoneinfo/
COPY --from=builder --chown=1000:1000 /go/bin/protoc-gen-go* /app/goctl /usr/local/bin/
ENV TZ Asia/Shanghai

WORKDIR /app
COPY --from=builder /app/goctl /usr/bin/goctl
USER app

LABEL org.opencontainers.image.authors="Kevin Wan"
LABEL org.opencontainers.image.base.name="docker.io/library/golang:alpine"
LABEL org.opencontainers.image.description="A cloud-native Go microservices framework with cli tool for productivity."
LABEL org.opencontainers.image.licenses="MIT"
LABEL org.opencontainers.image.source="https://github.com/zeromicro/go-zero"
LABEL org.opencontainers.image.title="goctl (cli)"
LABEL org.opencontainers.image.version="v1.6.0"

CMD ["goctl"]
ENTRYPOINT ["/usr/local/bin/goctl"]
9 changes: 4 additions & 5 deletions tools/goctl/test/integration/model/mongo/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
FROM golang:1.19
FROM golang:1.21-alpine

ENV TZ Asia/Shanghai
ENV GOPROXY https://goproxy.cn,direct

WORKDIR /app
ADD goctl /usr/bin/goctl
ADD cmd.sh .
COPY goctl /usr/bin/
COPY cmd.sh .

RUN chmod +x /usr/bin/goctl
RUN chmod +x cmd.sh
RUN chmod +x /usr/bin/goctl cmd.sh
CMD ["/bin/bash", "cmd.sh"]
2 changes: 1 addition & 1 deletion tools/goctl/test/integration/model/mongo/mongo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fi

# run docker image
console_step "docker running"
docker run $image
docker run --rm $image
if [ $? -ne 0 ]; then
rm -f $buildFile
console_red "docker run failed"
Expand Down

0 comments on commit b53ba76

Please sign in to comment.