fix(content): harden Docker examples

This commit is contained in:
yeasy
2026-06-01 10:33:50 -07:00
parent 094965e039
commit 1cdd3c582a
8 changed files with 34 additions and 25 deletions
@@ -1,11 +1,7 @@
FROM golang:alpine as builder FROM golang:alpine as builder
RUN apk --no-cache add git
WORKDIR /go/src/github.com/go/helloworld/ WORKDIR /go/src/github.com/go/helloworld/
RUN go get -d -v github.com/go-sql-driver/mysql
COPY app.go . COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
@@ -1,10 +1,7 @@
FROM golang:alpine FROM golang:alpine
RUN apk --no-cache add git
WORKDIR /go/src/github.com/go/helloworld WORKDIR /go/src/github.com/go/helloworld
COPY app.go . COPY app.go .
RUN go get -d -v github.com/go-sql-driver/mysql \ RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
@@ -1,13 +1,12 @@
FROM golang:alpine FROM golang:alpine
RUN apk --no-cache add git ca-certificates RUN apk --no-cache add ca-certificates
WORKDIR /go/src/github.com/go/helloworld/ WORKDIR /go/src/github.com/go/helloworld/
COPY app.go . COPY app.go .
RUN go get -d -v github.com/go-sql-driver/mysql \ RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . \
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . \
&& cp /go/src/github.com/go/helloworld/app /root && cp /go/src/github.com/go/helloworld/app /root
WORKDIR /root/ WORKDIR /root/
+3 -3
View File
@@ -11,7 +11,7 @@ Docker Hub 是 Docker 的中央镜像仓库,通过它您可以轻松地分享
- **官方镜像** Docker 官方和软件厂商 ( NginxMySQLNode.js) 维护的高质量镜像 - **官方镜像** Docker 官方和软件厂商 ( NginxMySQLNode.js) 维护的高质量镜像
- **个人/组织仓库**用户可以上传自己的镜像 - **个人/组织仓库**用户可以上传自己的镜像
- **自动构建** GitHub/Bitbucket 集成 (需付费) - **自动构建** GitHub/Bitbucket 集成的历史功能Docker 已标记为 deprecated并计划于 2027-04-01 完全退役
- **Webhooks**镜像更新时触发回调 - **Webhooks**镜像更新时触发回调
--- ---
@@ -131,8 +131,8 @@ Docker Hub 提供 Docker Scout 安全扫描功能。官方镜像的漏洞扫描
### 6.1.6 自动构建 ### 6.1.6 自动构建
> 目前仅限付费用户 (Pro/Team) 使用 > Docker Hub Automated Builds 已被 Docker 标记为 deprecated并计划于 2027-04-01 完全退役新项目应优先使用 GitHub ActionsBuildx 或自有 CI 构建并推送镜像
链接 GitHub/Bitbucket 仓库后当代码有提交或打标签时Docker Hub 会自动运行构建这保证了镜像总是与代码同步且由可信的官方环境构建 对于仍在迁移期内的旧仓库链接 GitHub/Bitbucket 仓库后当代码有提交或打标签时Docker Hub 会自动运行构建不要把它作为新架构的默认方案
--- ---
+14 -3
View File
@@ -11,10 +11,13 @@ services:
- db_data:/var/lib/mysql - db_data:/var/lib/mysql
restart: always restart: always
environment: environment:
MYSQL_ROOT_PASSWORD: somewordpress MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
MYSQL_DATABASE: wordpress MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress MYSQL_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_root_password
- db_password
wordpress: wordpress:
depends_on: depends_on:
@@ -26,6 +29,14 @@ services:
environment: environment:
WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
volumes: volumes:
db_data: db_data:
secrets:
db_root_password:
file: ./secrets/db_root_password.txt
db_password:
file: ./secrets/db_password.txt
@@ -96,7 +96,7 @@ done
```bash ```bash
# CPU % 超过 80%:需要增加 CPU 限制或优化应用 # CPU % 超过 80%:需要增加 CPU 限制或优化应用
# MEM % 接近 100%:容器即将 OOM,需要增加内存或排查内存泄漏 # MEM % 接近 100%:容器即将 OOM,需要增加内存或排查内存泄漏
# 如果 NET I/O 中 dropped 为非零:网络拥塞或丢包 # NET I/O 只显示收发字节;丢包要看 ip -s link、cAdvisor/Prometheus 或主机网卡计数器
``` ```
### 19.3.3 cAdvisor 容器监控系统 ### 19.3.3 cAdvisor 容器监控系统
@@ -376,7 +376,8 @@ docker run -m 512m --memory-swap 1g myapp:latest
# 参数说明: # 参数说明:
# -m / --memory:内存限制(这里是 512MB # -m / --memory:内存限制(这里是 512MB
# --memory-swap:内存+SWAP 总额(这里是 1GB,意味着 SWAP 为 512MB # --memory-swap:内存+SWAP 总额(这里是 1GB,意味着 SWAP 为 512MB
# 如果不设置 --memory-swap则等于 --memory 值 # 如果设置 --memory 但不设置 --memory-swap有主机 swap 时容器总量可达内存限制的 2 倍;
# 要禁用 swap,应把 --memory-swap 设置为与 --memory 相同
# Docker Compose 配置 # Docker Compose 配置
services: services:
@@ -606,10 +607,15 @@ docker run -m 512m --memory-swap 1g myapp:latest
症状应用性能突然下降 CPU 使用率不高 症状应用性能突然下降 CPU 使用率不高
诊断 诊断
```bash ```bash
# 查看 CPU 限流统计 # 查看 CPU 限流统计;现代发行版多为 cgroup v2,具体路径需先定位容器 cgroup
docker exec <container_id> cat /sys/fs/cgroup/cpu/cpu.stat docker inspect --format '{{.State.Pid}}' <container_id>
grep cgroup /proc/<pid>/mountinfo
docker exec <container_id> cat /sys/fs/cgroup/cpu.stat
# 如果 throttled_time > 0,说明发生了 CPU 限流 # cgroup v1 主机可能仍使用:
# docker exec <container_id> cat /sys/fs/cgroup/cpu/cpu.stat
# 如果 nr_throttled / throttled_usec > 0,说明发生了 CPU 限流
# 解决方案:增加 CPU 限制 # 解决方案:增加 CPU 限制
docker update --cpus 2 <container_id> docker update --cpus 2 <container_id>
``` ```
+1 -1
View File
@@ -82,7 +82,7 @@ jobs:
### 21.2.3 最佳实践 ### 21.2.3 最佳实践
* 固定 action 的主版本例如 `@v4` / `@v6`避免使用 `@master` 这类浮动引用 * 生产流水线要按完整 commit SHA 固定第三方 action示例中使用 `@v4` / `@v6` 只是为了可读性仍属于信任 tag 维护者的取舍不能等同于不可变引用
* 设置最小权限例如 `contents: read`需要写入权限时再打开 * 设置最小权限例如 `contents: read`需要写入权限时再打开
* 需要依赖缓存时优先使用官方支持的缓存方案例如针对语言包管理器的 cache BuildKit cache * 需要依赖缓存时优先使用官方支持的缓存方案例如针对语言包管理器的 cache BuildKit cache
* 敏感凭据Registry 密码Deploy Key 一律通过 `secrets` 注入禁止硬编码 * 敏感凭据Registry 密码Deploy Key 一律通过 `secrets` 注入禁止硬编码
+2 -2
View File
@@ -14,7 +14,7 @@ services:
environment: environment:
- DRONE_SERVER_HOST=${DRONE_SERVER_HOST:-drone.domain.com} - DRONE_SERVER_HOST=${DRONE_SERVER_HOST:-drone.domain.com}
- DRONE_SERVER_PROTO=${DRONE_SERVER_PROTO:-https} - DRONE_SERVER_PROTO=${DRONE_SERVER_PROTO:-https}
- DRONE_RPC_SECRET=${DRONE_RPC_SECRET:-secret} - DRONE_RPC_SECRET=${DRONE_RPC_SECRET:?set DRONE_RPC_SECRET}
- DRONE_GITHUB_SERVER=https://github.com - DRONE_GITHUB_SERVER=https://github.com
- DRONE_GITHUB_CLIENT_ID=${DRONE_GITHUB_CLIENT_ID} - DRONE_GITHUB_CLIENT_ID=${DRONE_GITHUB_CLIENT_ID}
- DRONE_GITHUB_CLIENT_SECRET=${DRONE_GITHUB_CLIENT_SECRET} - DRONE_GITHUB_CLIENT_SECRET=${DRONE_GITHUB_CLIENT_SECRET}
@@ -29,7 +29,7 @@ services:
environment: environment:
- DRONE_RPC_PROTO=http - DRONE_RPC_PROTO=http
- DRONE_RPC_HOST=drone-server - DRONE_RPC_HOST=drone-server
- DRONE_RPC_SECRET=${DRONE_RPC_SECRET:-secret} - DRONE_RPC_SECRET=${DRONE_RPC_SECRET:?set DRONE_RPC_SECRET}
- DRONE_RUNNER_NAME=${HOSTNAME:-demo} - DRONE_RUNNER_NAME=${HOSTNAME:-demo}
- DRONE_RUNNER_CAPACITY=2 - DRONE_RUNNER_CAPACITY=2
dns: 114.114.114.114 dns: 114.114.114.114