Skip to content

Commit b53ba76

Browse files
feat: Improve Docker build (zeromicro#3682)
1 parent be7f939 commit b53ba76

File tree

9 files changed

+48
-21
lines changed

9 files changed

+48
-21
lines changed

.dockerignore

+6
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
**/.git
2+
.dockerignore
3+
Dockerfile
4+
goctl
5+
Makefile
6+
readme.md
7+
readme-cn.md

.github/dependabot.yml

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55

66
version: 2
77
updates:
8+
- package-ecosystem: "docker" # Update image tags in Dockerfile
9+
directory: "/"
10+
schedule:
11+
interval: "weekly"
12+
- package-ecosystem: "github-actions" # Update GitHub Actions
13+
directory: "/"
14+
schedule:
15+
interval: "weekly"
816
- package-ecosystem: "gomod" # See documentation for possible values
917
directory: "/" # Location of package manifests
1018
schedule:

.github/workflows/go.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Set up Go 1.x
1818
uses: actions/setup-go@v4
1919
with:
20-
go-version: 1.19
20+
go-version: '1.19'
2121
check-latest: true
2222
cache: true
2323
id: go
@@ -53,7 +53,7 @@ jobs:
5353
uses: actions/setup-go@v4
5454
with:
5555
# use 1.19 to guarantee Go 1.19 compatibility
56-
go-version: 1.19
56+
go-version: '1.19'
5757
check-latest: true
5858
cache: true
5959

readme-cn.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromicro
120120
# docker for amd64 architecture
121121
docker pull kevinwan/goctl
122122
# run goctl like
123-
docker run --rm -it -v `pwd`:/app kevinwan/goctl goctl --help
123+
docker run --rm -it -v `pwd`:/app kevinwan/goctl --help
124124

125125
# docker for arm64(Mac) architecture
126126
docker pull kevinwan/goctl:latest-arm64
127127
# run goctl like
128-
docker run --rm -it -v `pwd`:/app kevinwan/goctl:latest-arm64 goctl --help
128+
docker run --rm -it -v `pwd`:/app kevinwan/goctl:latest-arm64 --help
129129
```
130130

131131
确保 goctl 可执行

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ go get -u github.com/zeromicro/go-zero
127127
# docker for amd64 architecture
128128
docker pull kevinwan/goctl
129129
# run goctl like
130-
docker run --rm -it -v `pwd`:/app kevinwan/goctl goctl --help
130+
docker run --rm -it -v `pwd`:/app kevinwan/goctl --help
131131

132132
# docker for arm64(Mac) architecture
133133
docker pull kevinwan/goctl:latest-arm64
134134
# run goctl like
135-
docker run --rm -it -v `pwd`:/app kevinwan/goctl:latest-arm64 goctl --help
135+
docker run --rm -it -v `pwd`:/app kevinwan/goctl:latest-arm64 --help
136136
```
137137

138138
make sure goctl is executable.

tools/goctl/.dockerignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test/
2+
.dockerignore
3+
.go-version
4+
Dockerfile
5+
goctl
6+
Makefile
7+
readme.md
8+
readme-cn.md

tools/goctl/Dockerfile

+15-9
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,33 @@ ENV GOPROXY https://goproxy.cn,direct
88
RUN apk update --no-cache && apk add --no-cache tzdata
99
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
1010
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
11+
RUN addgroup -g 1000 -S app && adduser -u 1000 -S app -G app
1112

1213
WORKDIR /build
1314

14-
ADD go.mod .
15-
ADD go.sum .
16-
RUN go mod download
1715
COPY . .
16+
RUN go mod download
1817
RUN go build -ldflags="-s -w" -o /app/goctl ./goctl.go
1918

2019

2120
FROM golang:alpine
2221

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

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

3129
WORKDIR /app
32-
COPY --from=builder /app/goctl /usr/bin/goctl
30+
USER app
31+
32+
LABEL org.opencontainers.image.authors="Kevin Wan"
33+
LABEL org.opencontainers.image.base.name="docker.io/library/golang:alpine"
34+
LABEL org.opencontainers.image.description="A cloud-native Go microservices framework with cli tool for productivity."
35+
LABEL org.opencontainers.image.licenses="MIT"
36+
LABEL org.opencontainers.image.source="https://github.com/zeromicro/go-zero"
37+
LABEL org.opencontainers.image.title="goctl (cli)"
38+
LABEL org.opencontainers.image.version="v1.6.0"
3339

34-
CMD ["goctl"]
40+
ENTRYPOINT ["/usr/local/bin/goctl"]
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
FROM golang:1.19
1+
FROM golang:1.21-alpine
22

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

66
WORKDIR /app
7-
ADD goctl /usr/bin/goctl
8-
ADD cmd.sh .
7+
COPY goctl /usr/bin/
8+
COPY cmd.sh .
99

10-
RUN chmod +x /usr/bin/goctl
11-
RUN chmod +x cmd.sh
10+
RUN chmod +x /usr/bin/goctl cmd.sh
1211
CMD ["/bin/bash", "cmd.sh"]

tools/goctl/test/integration/model/mongo/mongo.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fi
2525

2626
# run docker image
2727
console_step "docker running"
28-
docker run $image
28+
docker run --rm $image
2929
if [ $? -ne 0 ]; then
3030
rm -f $buildFile
3131
console_red "docker run failed"

0 commit comments

Comments
 (0)