Dockerfile CMD不插值
我尝试了我能想到的每种组合。
docker-compose.yml
version: "3.1"
services:
node-server:
build:
context: .
dockerfile: Dockerfile
args:
- MYVAR=${MYVAR}
environment:
- ENV_MYVAR=${MYVAR}
ports:
- 8000:8000
env_file:
- .env
# command: ["npm", "run", "_${MYVAR}_"] This works, but for the sake of testing CMD in Dockerfile, I can't get that to work
dockerfile
FROM node:14
ARG MYVAR
ENV ENV_MYVAR="${MYVAR}"
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm i
COPY . .
EXPOSE ${port}
RUN echo "1"
RUN echo $MYVAR # Works
CMD [ "npm", "run", "_${ENV_MYVAR}_"] # OUTPUTS as _${ENV_MYVAR}_". Does not interpolate!!
.env
MYVAR=boot
package.json
{
"name": "node-server-ts",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"test": "echo 'test'",
"boot": "echo 'boot'",
"_boot_": "echo '_boot_'"
}
}
terminal tright
docker-compose up
期望:
npm run _boot_
错误-Node -server_1 | $ {env_myvar}
I have tried every combination I can think of.
docker-compose.yml
version: "3.1"
services:
node-server:
build:
context: .
dockerfile: Dockerfile
args:
- MYVAR=${MYVAR}
environment:
- ENV_MYVAR=${MYVAR}
ports:
- 8000:8000
env_file:
- .env
# command: ["npm", "run", "_${MYVAR}_"] This works, but for the sake of testing CMD in Dockerfile, I can't get that to work
Dockerfile
FROM node:14
ARG MYVAR
ENV ENV_MYVAR="${MYVAR}"
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm i
COPY . .
EXPOSE ${port}
RUN echo "1"
RUN echo $MYVAR # Works
CMD [ "npm", "run", "_${ENV_MYVAR}_"] # OUTPUTS as _${ENV_MYVAR}_". Does not interpolate!!
.env
MYVAR=boot
package.json
{
"name": "node-server-ts",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"test": "echo 'test'",
"boot": "echo 'boot'",
"_boot_": "echo '_boot_'"
}
}
terminal
docker-compose up
Expect:
npm run _boot_
error - node-server_1 | ${ENV_MYVAR}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试卸下方括号。
_”
cmd npm运行“ _ $ {env_myvar } -version“> stackoverflow问题关于差异
Try removing the square brackets.
CMD npm run "_${ENV_MYVAR}_"
See this StackOverflow question about the differences