简体   繁体   English

将 ARG 从 docker-compose.yml 传递给 docker 镜像

[英]Pass ARG from docker-compose.yml to a docker image

So I have a NodeJS API which I have created, which contains a Private NPM package in the package.json.所以我创建了一个 NodeJS API,它在 package.json 中包含一个 Private NPM 包。

I have the following Docker files in this project:我在这个项目中有以下 Docker 文件:

Dockerfile文件

FROM node:10

WORKDIR /usr/src/app
ARG NPM_TOKEN
COPY .npmrc ./  
COPY package.json ./
RUN npm install  
RUN rm -f ./.npmrc
COPY . .

CMD [ "npm", "start" ]

.npmrc .npmrc

//registry.npmjs.org/:_authToken=${NPM_TOKEN}

I have managed to build this API by running the command:我通过运行以下命令设法构建了这个 API:

docker build --build-arg NPM_TOKEN=${MY_NPM_TOKEN} -t api:1.0.0 .

This successfully builds the image.这成功地构建了映像。

In my main application, I have a docker-compose.yml which I want to run this image.在我的主应用程序中,我有一个 docker-compose.yml 想要运行这个镜像。

version: '3' services: redis: container_name: redis image: redis:3.2.8 ports: - "6379:6379" volumes: - ./data:/data api: container_name: api image: api:1.0.0 build: context: . args: - NPM_TOKEN={MY_NPM_TOKEN} ports: - "3001:3001"

When I run docker-compose up it fails with the error:当我运行docker-compose up它失败并显示错误:

Failed to replace env in config: ${NPM_TOKEN}

Does anyone have an idea, why my image is not taking in the ARG that is passed?有没有人知道,为什么我的图像没有接受通过的ARG

From my understanding, you are trying to pass the NPM_TOKEN arguments from the environment variable named MY_NPM_TOKEN .根据我的理解,您正在尝试从名为MY_NPM_TOKEN的环境变量传递NPM_TOKEN参数。

However, there is an error in the syntax that you should update your docker-compose.yaml file但是,您应该更新docker-compose.yaml文件的语法错误

from - NPM_TOKEN={MY_NPM_TOKEN}来自- NPM_TOKEN={MY_NPM_TOKEN}

to - NPM_TOKEN=${MY_NPM_TOKEN}- NPM_TOKEN=${MY_NPM_TOKEN}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM