user3091919
user3091919

Reputation: 275

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.

I have the following Docker files in this project:

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

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

I have managed to build this API by running the command:

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.

`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:

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?

Upvotes: 4

Views: 1920

Answers (1)

Nguyen Lam Phuc
Nguyen Lam Phuc

Reputation: 1431

From my understanding, you are trying to pass the NPM_TOKEN arguments from the environment variable named MY_NPM_TOKEN.

However, there is an error in the syntax that you should update your docker-compose.yaml file

from - NPM_TOKEN={MY_NPM_TOKEN}

to - NPM_TOKEN=${MY_NPM_TOKEN}

Upvotes: 2

Related Questions