我正在尝试运行这个命令Dockerfile
但它没有向文件产生任何输出:
FROM alpine:3.19 as base
RUN <<-EOF > /root/.npmrc
registry=https://\${NPM_VIRTUAL_REGISTRY_REPO_URL}
strict-ssl=false
always-auth=true
email=\${PUBLISHER_EMAIL}
//\${NPM_VIRTUAL_REGISTRY_REPO_URL}:_auth=\${NPM_BASIC_AUTH}
EOF
RUN ls -al /root && cat $HOME/.npmrc
生成:
docker build -t base --target=base .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
Sending build context to Docker daemon 946.2kB
Step 1/3 : FROM alpine:3.19 as base
---> 05455a08881e
Step 2/3 : RUN <<-EOF > /root/.npmrc
---> Using cache
---> f18fb43d4ffa
Step 3/3 : RUN ls -al /root && cat $HOME/.npmrc
---> Running in 24b3b8f942a8
total 8
drwx------ 1 root root 4096 Jan 30 02:55 .
drwxr-xr-x 1 root root 4096 Jan 30 02:55 ..
-rw-r--r-- 1 root root 0 Jan 30 02:55 .npmrc
Removing intermediate container 24b3b8f942a8
---> 7b21305b1704
Successfully built 7b21305b1704
Successfully tagged base:latest
而且,如果我尝试从 CLI 输出它,也是同样的情况:
-> % docker run -it base cat /root/.npmrc
[18:57:04] [~/dev/app] [CES-2242_refactor-pipeline_build *]
-> %
但是,如果我在笔记本电脑(Mac OS 14.3)的 CLI 上运行相同的命令:
[18:58:34] [~/dev/app] [CES-2242_refactor-pipeline_build *]
-> % <<-EOF > .npmrc
registry=https://\${NPM_VIRTUAL_REGISTRY_REPO_URL}
strict-ssl=false
always-auth=true
email=\${PUBLISHER_EMAIL}
//\${NPM_VIRTUAL_REGISTRY_REPO_URL}:_auth=\${NPM_BASIC_AUTH}
EOF
[18:58:45] [~/dev/app] [CES-2242_refactor-pipeline_build *]
-> % cat .npmrc
registry=https://${NPM_VIRTUAL_REGISTRY_REPO_URL}
strict-ssl=false
always-auth=true
email=${PUBLISHER_EMAIL}
//${NPM_VIRTUAL_REGISTRY_REPO_URL}:_auth=${NPM_BASIC_AUTH}
它运行正常。我遗漏了什么?
编辑
我删除了BUILD_KIT=0
之前的标志。没什么区别
答案1
解析此命令:
RUN <<-EOF > /root/.npmrc
registry=https://\${NPM_VIRTUAL_REGISTRY_REPO_URL}
strict-ssl=false
always-auth=true
email=\${PUBLISHER_EMAIL}
//\${NPM_VIRTUAL_REGISTRY_REPO_URL}:_auth=\${NPM_BASIC_AUTH}
EOF
我相信结果是在容器中运行以下命令的 shell:
> /root/.npmrc
registry=https://\${NPM_VIRTUAL_REGISTRY_REPO_URL}
strict-ssl=false
always-auth=true
email=\${PUBLISHER_EMAIL}
//${NPM_VIRTUAL_REGISTRY_REPO_URL}:_auth=${NPM_BASIC_AUTH}
这是一个不向文件输出任何内容的命令.npmrc
,后面跟着 5 个单独的命令来在 shell 中设置变量,此时 shell 退出并丢失所有变量状态。
要输出到文件,有一个带有 HEREDOC 支持的 COPY 语法:
COPY <<-EOF /root/.npmrc
registry=https://\${NPM_VIRTUAL_REGISTRY_REPO_URL}
strict-ssl=false
always-auth=true
email=\${PUBLISHER_EMAIL}
//\${NPM_VIRTUAL_REGISTRY_REPO_URL}:_auth=\${NPM_BASIC_AUTH}
EOF
或者您可以在容器内运行一个命令,该命令本身会执行 HEREDOC,这可能会更好地处理变量:
RUN <<EORUN
cat <<-EOCAT > /root/.npmrc
registry=https://\${NPM_VIRTUAL_REGISTRY_REPO_URL}
strict-ssl=false
always-auth=true
email=\${PUBLISHER_EMAIL}
//\${NPM_VIRTUAL_REGISTRY_REPO_URL}:_auth=\${NPM_BASIC_AUTH}
EOCAT
EORUN