0

I am dockerizing a node application.

This is the Dockerfile that I am using:

FROM node:10-slim

# Sets environment variable
ENV NODE_ENV production

# Sets work directory
WORKDIR /usr/src/app

# Copy package.json
COPY ["package.json", "./"]

# Installs dependencies 
RUN npm install

# Copy working files
COPY . /usr/src/app

EXPOSE 80

# Starts run command
CMD npm start

But then, since I have several .env files, I would like to pass an argument to choose which env file I will use.

Like this

npm start -- --env="test" 

So what I ultimately want is

docker run -p 8080:8080 test/nodeapp:1.0 -- -evn="test" 

How should I override the CMD on docker run?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Bruce Oh
  • 17
  • 2

1 Answers1

1

See this:

$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

You can override the COMMAND when do docker run as you like, so for you case, if your command in your question is correct, then it is:

docker run -p 8080:8080 test/nodeapp:1.0 npm start -- --env="test"

If still need -- --env="test" as you said in comment, you need to use entrypoint, then have a look for this

atline
  • 28,355
  • 16
  • 77
  • 113
  • thanks! it works! but i still want to "docker run -p 8080:8080 test/nodeapp:1.0 -- -evn="test", should i move "npm start" to entrypoint in dockerfile? – Bruce Oh Jun 03 '19 at 05:09
  • Then, you should use `entrypoint in dockerfile`, and runtime command will be act as parameters. – atline Jun 03 '19 at 05:11