【发布时间】:2019-07-17 02:11:17
【问题描述】:
我将构建参数传递到:docker build --build-arg RUNTIME=test
在我的 Dockerfile 中,我想在 CMD 中使用参数的值:
CMD ["npm", "run", "start:${RUNTIME}"]
这样做会导致此错误:npm ERR! missing script: start:${RUNTIME} - 它没有扩展变量
我阅读了这篇文章:Use environment variables in CMD
所以我尝试这样做:CMD ["sh", "-c", "npm run start:${RUNTIME}"] - 我最终得到了这个错误:/bin/sh: [sh,: not found
当我运行构建的容器时会发生这两个错误。
我使用节点高山图像作为基础。任何人都知道如何让参数值在 CMD 中扩展?提前致谢!
完整的 Dockerfile:
FROM node:10.15.0-alpine as builder
ARG RUNTIME_ENV=test
RUN mkdir -p /usr/app
WORKDIR /usr/app
COPY . .
RUN npm ci
RUN npm run build
FROM node:10.15.0-alpine
COPY --from=builder /usr/app/.npmrc /usr/app/package*.json /usr/app/server.js ./
COPY --from=builder /usr/app/config ./config
COPY --from=builder /usr/app/build ./build
RUN npm ci --only=production
EXPOSE 3000
CMD ["npm", "run", "start:${RUNTIME_ENV}"]
更新: 为了清楚起见,我遇到了两个问题。 1. Samuel P. 描述的问题。 2、容器间不携带ENV值(多级)
这是我可以在 CMD 中扩展环境变量的工作 Dockerfile:
# Here we set the build-arg as an environment variable.
# Setting this in the base image allows each build stage to access it
FROM node:10.15.0-alpine as base
ARG ENV
ENV RUNTIME_ENV=${ENV}
FROM base as builder
RUN mkdir -p /usr/app
WORKDIR /usr/app
COPY . .
RUN npm ci && npm run build
FROM base
COPY --from=builder /usr/app/.npmrc /usr/app/package*.json /usr/app/server.js ./
COPY --from=builder /usr/app/config ./config
COPY --from=builder /usr/app/build ./build
RUN npm ci --only=production
EXPOSE 3000
CMD npm run start:${RUNTIME_ENV}
【问题讨论】:
-
你能分享你完整的 Dockerfile 吗?
-
Dockerfile已编辑到帖子中,感谢查看
标签: docker dockerfile