Question: passing in dynamic variable into Dockerfile...?

569 Views Asked by At

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!

1

There are 1 best solutions below

0
Mureinik On

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 .