fix(content): harden secret handling examples

This commit is contained in:
yeasy
2026-06-06 00:43:52 -07:00
parent 67d5fc775a
commit 070d4d6d69
2 changed files with 19 additions and 10 deletions
+6 -4
View File
@@ -146,17 +146,19 @@ RUN if [ "$ENABLE_DEBUG" = "true" ]; then \
#### 4. 配置私有仓库
```docker
ARG NPM_TOKEN
不要用 `ARG` `ENV` 传递仓库 tokenDocker 官方构建检查会把这类写法视为不安全因为构建参数和环境变量可能进入镜像元数据或历史记录使用 BuildKit secret mount 只在单条 `RUN` 指令期间暴露凭据
RUN echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc && \
```docker
RUN --mount=type=secret,id=npm_token \
NPM_TOKEN="$(cat /run/secrets/npm_token)" && \
printf "//registry.npmjs.org/:_authToken=%s\n" "$NPM_TOKEN" > ~/.npmrc && \
npm install && \
rm ~/.npmrc
```
```bash
## 构建时传入 token
$ docker build --build-arg NPM_TOKEN=xxx .
$ docker build --secret id=npm_token,env=NPM_TOKEN .
```
---