Fix go mod, Docker Hub rate limit, Compose verbose, docker debug, htpasswd security

This commit is contained in:
yeasy
2026-05-13 08:30:20 +00:00
parent ef5a97fa09
commit 9f481e88ca
5 changed files with 20 additions and 6 deletions
+2 -1
View File
@@ -249,10 +249,11 @@ $ docker exec myapp python manage.py migrate
$ docker exec -it myapp bash
OCI runtime exec failed: exec failed: unable to start container process: exec: "bash": executable file not found
## 解决方案:使用调试容器
## 解决方案:使用调试容器(需要 Docker Desktop Pro/Team/Business 订阅)
$ docker debug myapp
```
> **注意**`docker debug` Docker Desktop 4.33+ 提供的功能需要 ProTeam Business 订阅它会附加一个包含常用调试工具vimcurlhtop 的工具箱到目标容器即使目标镜像基于 `scratch` 也能使用
---
### 5.4.7 常见问题
+1 -1
View File
@@ -75,7 +75,7 @@ Docker Hub 对不同类型用户实施拉取速率限制(基于 6 小时周期
| **免费账户** (已登录) | 6 小时 200 次请求 |
| **Pro/Team/Business 账户** | 无限制公平使用政策 |
> **注意** 2025 4 所有付费订阅用户享有无限制拉取额度匿名用户和免费账户的限制保持不变建议在 CI/CD 环境中始终配置 `docker login` 以获得更高的拉取额度
> **注意**Docker 曾计划于 2025 4 调整拉取限制策略但在 2025 2 月宣布取消该计划目前付费订阅用户享有无限制拉取额度匿名用户和免费账户的限制保持不变建议在 CI/CD 环境中始终配置 `docker login` 以获得更高的拉取额度
#### 滥用限流
+2
View File
@@ -126,6 +126,8 @@ $ docker run --rm \
-Bbn username password > auth/nginx.htpasswd
```
> 将上面的 `username` `password` 替换为你自己的用户名和密码
>
> **安全提示**上述命令会将密码明文暴露在 shell 历史记录和进程列表中生产环境建议使用交互式方式输入密码不带 `-b` 参数或通过环境变量/文件传入
> **版本说明**使用 `httpd:2.4-alpine` 基于 Apache 2.4 的精简镜像如需其他版本可替换为 `httpd:latest` 或指定具体版本号如 `httpd:2.4.58-alpine`
+14 -3
View File
@@ -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` 来为某一阶段命名例如
+1 -1
View File
@@ -46,7 +46,7 @@ docker compose [-f=<arg>...] [options] [COMMAND] [ARGS...]
* `-p, --project-name NAME` 指定项目名称默认将使用所在目录名称作为项目名
* `--verbose` 输出更多调试信息
* `--verbose` 输出更多调试信息(**已弃用** Docker Compose V2 请改用 `docker --log-level debug compose ...` 或设置环境变量 `COMPOSE_DEBUG=1`)
* `-v, --version` 打印版本并退出