Skip to content

Commit 070d4d6

Browse files
committed
fix(content): harden secret handling examples
1 parent 67d5fc7 commit 070d4d6

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

07_dockerfile/7.7_arg.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,19 @@ RUN if [ "$ENABLE_DEBUG" = "true" ]; then \
146146

147147
#### 4. 配置私有仓库
148148

149-
```docker
150-
ARG NPM_TOKEN
149+
不要用 `ARG``ENV` 传递仓库 token。Docker 官方构建检查会把这类写法视为不安全,因为构建参数和环境变量可能进入镜像元数据或历史记录。使用 BuildKit secret mount 只在单条 `RUN` 指令期间暴露凭据:
151150

152-
RUN echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc && \
151+
```docker
152+
RUN --mount=type=secret,id=npm_token \
153+
NPM_TOKEN="$(cat /run/secrets/npm_token)" && \
154+
printf "//registry.npmjs.org/:_authToken=%s\n" "$NPM_TOKEN" > ~/.npmrc && \
153155
npm install && \
154156
rm ~/.npmrc
155157
```
156158
```bash
157159
## 构建时传入 token
158160

159-
$ docker build --build-arg NPM_TOKEN=xxx .
161+
$ docker build --secret id=npm_token,env=NPM_TOKEN .
160162
```
161163
---
162164

16_cloud/16.3_alicloud.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,19 @@ spec:
247247
在 ACK 集群中创建 Secret,用于拉取私有镜像:
248248
249249
```bash
250-
# 创建镜像拉取 Secret
251-
kubectl create secret docker-registry acr-secret \
252-
--docker-server=registry.cn-hangzhou.aliyuncs.com \
253-
--docker-username=<阿里云账户ID> \
254-
--docker-password=<RAM访问密钥或密码> \
255-
--docker-email=<邮箱地址>
250+
# 避免把真实口令写入命令历史。使用临时 Docker config 并通过标准输入登录。
251+
export DOCKER_CONFIG="$(mktemp -d)"
252+
read -rsp "ACR token: " ACR_TOKEN; echo
253+
printf "%s" "$ACR_TOKEN" | docker login registry.cn-hangzhou.aliyuncs.com \
254+
--username "<阿里云账户ID>" \
255+
--password-stdin
256+
257+
kubectl create secret generic acr-secret \
258+
--from-file=.dockerconfigjson="$DOCKER_CONFIG/config.json" \
259+
--type=kubernetes.io/dockerconfigjson
260+
261+
rm -rf "$DOCKER_CONFIG"
262+
unset ACR_TOKEN DOCKER_CONFIG
256263

257264
# 查看创建的 Secret
258265
kubectl get secret acr-secret

0 commit comments

Comments
 (0)