From 9f481e88ca45dad03c62a8c753d80a88898b425c Mon Sep 17 00:00:00 2001 From: yeasy Date: Wed, 13 May 2026 08:30:20 +0000 Subject: [PATCH] Fix go mod, Docker Hub rate limit, Compose verbose, docker debug, htpasswd security --- 05_container/5.4_attach_exec.md | 3 ++- 06_repository/6.1_dockerhub.md | 2 +- 06_repository/6.3_registry_auth.md | 2 ++ 07_dockerfile/7.17_multistage_builds.md | 17 ++++++++++++++--- 11_compose/11.4_commands.md | 2 +- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/05_container/5.4_attach_exec.md b/05_container/5.4_attach_exec.md index db30342..55a85c1 100644 --- a/05_container/5.4_attach_exec.md +++ b/05_container/5.4_attach_exec.md @@ -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+ 提供的功能,需要 Pro、Team 或 Business 订阅。它会附加一个包含常用调试工具(vim、curl、htop 等)的工具箱到目标容器,即使目标镜像基于 `scratch` 也能使用。 --- ### 5.4.7 常见问题 diff --git a/06_repository/6.1_dockerhub.md b/06_repository/6.1_dockerhub.md index c19c761..74e6870 100644 --- a/06_repository/6.1_dockerhub.md +++ b/06_repository/6.1_dockerhub.md @@ -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` 以获得更高的拉取额度。 #### 滥用限流 diff --git a/06_repository/6.3_registry_auth.md b/06_repository/6.3_registry_auth.md index b33adb9..bca6bb8 100644 --- a/06_repository/6.3_registry_auth.md +++ b/06_repository/6.3_registry_auth.md @@ -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`。 diff --git a/07_dockerfile/7.17_multistage_builds.md b/07_dockerfile/7.17_multistage_builds.md index 34924c7..80f027b 100644 --- a/07_dockerfile/7.17_multistage_builds.md +++ b/07_dockerfile/7.17_multistage_builds.md @@ -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` 来为某一阶段命名,例如 diff --git a/11_compose/11.4_commands.md b/11_compose/11.4_commands.md index e55aafc..98da8c6 100644 --- a/11_compose/11.4_commands.md +++ b/11_compose/11.4_commands.md @@ -46,7 +46,7 @@ docker compose [-f=...] [options] [COMMAND] [ARGS...] * `-p, --project-name NAME` 指定项目名称,默认将使用所在目录名称作为项目名。 -* `--verbose` 输出更多调试信息。 +* `--verbose` 输出更多调试信息。(**已弃用**:在 Docker Compose V2 中,请改用 `docker --log-level debug compose ...` 或设置环境变量 `COMPOSE_DEBUG=1`。) * `-v, --version` 打印版本并退出。