mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-08-10 08:27:25 +00:00
Fix go mod, Docker Hub rate limit, Compose verbose, docker debug, htpasswd security
This commit is contained in:
@@ -33,7 +33,7 @@ WORKDIR /go/src/github.com/go/helloworld/
|
||||
COPY app.go .
|
||||
|
||||
RUN go mod init helloworld \
|
||||
&& go get -d -v github.com/go-sql-driver/mysql \
|
||||
&& go get github.com/go-sql-driver/mysql \
|
||||
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . \
|
||||
&& cp /go/src/github.com/go/helloworld/app /root
|
||||
|
||||
@@ -62,7 +62,8 @@ WORKDIR /go/src/github.com/go/helloworld
|
||||
|
||||
COPY app.go .
|
||||
|
||||
RUN go get -d -v github.com/go-sql-driver/mysql \
|
||||
RUN go mod init helloworld \
|
||||
&& go get github.com/go-sql-driver/mysql \
|
||||
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
|
||||
```
|
||||
编写 `Dockerfile.copy` 文件
|
||||
@@ -125,7 +126,8 @@ RUN apk --no-cache add git
|
||||
|
||||
WORKDIR /go/src/github.com/go/helloworld/
|
||||
|
||||
RUN go get -d -v github.com/go-sql-driver/mysql
|
||||
RUN go mod init helloworld \
|
||||
&& go get github.com/go-sql-driver/mysql
|
||||
|
||||
COPY app.go .
|
||||
|
||||
@@ -158,6 +160,15 @@ go/helloworld 1 f55d3e16affc 2 minutes ago 295MB
|
||||
```
|
||||
很明显使用多阶段构建的镜像体积小,同时也完美解决了上边提到的问题。
|
||||
|
||||
> **Go Modules 最佳实践**:上述示例为简化演示在 Dockerfile 中临时执行 `go mod init`。在实际项目中,通常已在代码仓库中维护好 `go.mod` 和 `go.sum` 文件。推荐的 Dockerfile 写法是先拷贝这两个文件并执行 `go mod download` 以利用 Docker 层缓存,再拷贝源码并构建:
|
||||
>
|
||||
> ```docker
|
||||
> COPY go.mod go.sum ./
|
||||
> RUN go mod download
|
||||
> COPY . .
|
||||
> RUN go build -o app .
|
||||
> ```
|
||||
|
||||
### 7.17.4 只构建某一阶段的镜像
|
||||
|
||||
我们可以使用 `as` 来为某一阶段命名,例如
|
||||
|
||||
Reference in New Issue
Block a user