-1

We have an angular/express app that is dockerized and deployed in k8s.

Dockerfile:

FROM node:18

WORKDIR /usr/src/app

COPY . .

EXPOSE 1234
CMD [ "npm", "run", "prod" ]

Is it possible to have a variable and have that variable be dynamic?

Example of what I want:

FROM node:18

WORKDIR /usr/src/app

COPY . .

EXPOSE 1234
CMD [ "npm", "run", ENV ] <<<<<------ HERE (want to pass in either dev or prod)

I have a build script (which needs to be run with an arg passed in) that does the following:

  • ./build_and_deploy_app.sh dev or ./build_and_deploy_app.sh prod

dev or prod are the variables I want to pass into Dockerfile

  • builds docker app

  • tags docker app

  • pushes docker app to ECR

Once that docker image is pushed to ECR:

  • We update our k8s deployment to use the newly uploaded image

I wondering if there is a way to allow our Dockerfile (docker image that is uploaded to ECR) to use a dynamic variable instead of a static variable (like shown above).

Thank you!

Alphie
  • 51
  • 4
  • (Building a separate container per environment is something of an anti-pattern; ideally you can run an identical image in all environments. It's easy to override the `CMD` when you run the container, which may simplify this use case for you.) – David Maze Jan 13 '23 at 18:42

1 Answers1

1

You could use the Docker ARG instruction:

FROM node:18

WORKDIR /usr/src/app

COPY . .

EXPOSE 1234
ARG env
CMD [ "npm", "run", ${env}]

And then pass the value with --build-arg:

docker build --build-arg env=prod -t mycontainer .

And of course, you could take this from the shell script's argument:

docker build --build-arg env=$1 -t mycontainer .
Mureinik
  • 297,002
  • 52
  • 306
  • 350