0

I am new to Docker and Jenkins. I have to build and deploy Nest Js app in jenkins. When I run the Jenkins job I have to select the 'DEPLOY_PROFILE' which is equals to 'dev' and 'qa' as follows.

enter image description here

This is my Dockerfile,

    FROM node:16-alpine
    WORKDIR /app
    ADD package.json /app/package.json
    RUN npm config set registry http://registry.npmjs.org
    RUN npm install
    ADD . /app
    EXPOSE 3000
    CMD ["npm", "run", "start"]

I need to pass the 'DEPLOY_PROFILE' variable which is equals to 'dev' or 'qa' to the Dockerfile. Then final docker command should be look like npm run start:dev or npm run start:qa

I have tried using

CMD ["npm", "run", "start", `:${DEPLOY_PROFILE}`]

and

CMD ["npm", "run", "start", `:${env.DEPLOY_PROFILE}`]

But nothing gave me the luck. Any help may highly appreciated!

Sajith Wijerathne
  • 119
  • 1
  • 3
  • 10
  • You can provide a replacement command when you run the container. If the only thing this is used for is to set the default `CMD`, can you default it to `npm run start` but then explicitly `docker run your-image npm run start:qa` if you need to? Also see [How can I use a variable inside a Dockerfile CMD?](https://stackoverflow.com/questions/40454470/how-can-i-use-a-variable-inside-a-dockerfile-cmd) – David Maze Jan 13 '23 at 12:08

1 Answers1

1

You can use an environment variable for that. In your dockerfile, declare an argument (passed into docker build) and an environment variable like this:

ARG DEPLOY_PROFILE
ENV PROFILE=${deploy_profile}

Then use the environment variable like this:

CMD npm run start $PROFILE

Then call buildah (or whatever you are using) like this:

 buildah bud --format=docker --build-arg deploy_profile="$DEPLOY_PROFILE"
StefanFFM
  • 1,526
  • 1
  • 14
  • 25