0

I am trying to set my Docker image environment variables conditionally based on argument passed while building the docker image. Here is what my docker file looks like

# pull the base image
FROM node:alpine as build

# set the working direction
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install app dependencies
COPY package.json ./

COPY yarn.lock ./

RUN yarn

# add app
COPY . ./

# define environment variables
ARG REACT_APP_BASE_URL=https://service.dev.localhost.com
ARG REACT_APP_SERVICES_BASE_URL=https://services.dev.localhost.com
ARG REACT_APP_BASE_URL_PROD=https://service.localhost.com
ARG REACT_APP_SERVICES_BASE_URL_PROD=https://services.localhost.com
ARG app_env

ENV app_stage $app_env
ENV REACT_APP_BASE_URL=$REACT_APP_BASE_URL
ENV REACT_APP_SERVICES_BASE_URL=$REACT_APP_SERVICES_BASE_URL
ENV REACT_APP_BASE_URL_PROD=$REACT_APP_BASE_URL_PROD
ENV REACT_APP_SERVICES_BASE_URL_PROD=$REACT_APP_SERVICES_BASE_URL_PROD

RUN if ["$app_stage" = "dev"]; then \
    echo "REACT_APP_BASE_URL=$REACT_APP_BASE_URL" > .env; \
    echo "REACT_APP_WALLET_BASE_URL=$REACT_APP_WALLET_BASE_URL" > .env; \
    echo "REACT_APP_TRANSACTION_BASE_URL=$REACT_APP_TRANSACTION_BASE_URL" > .env; \
    else \
    echo "REACT_APP_BASE_URL=$REACT_APP_BASE_URL_PROD" > .env; \
    echo "REACT_APP_WALLET_BASE_URL=$REACT_APP_WALLET_BASE_URL_PROD" > .env; \
    echo "REACT_APP_TRANSACTION_BASE_URL=$REACT_APP_TRANSACTION_BASE_URL_PROD" > .env; \
    fi

CMD ["yarn", "start"]

Here I am setting environment variables to production or development based on app_env when I run my docker build command.

This is the command I run to build image

For production docker build --build-arg app_env=production -t app-prod . and for dev docker build --build-arg app_env=dev -t app-dev .

And then I run the app using

docker run \
    -it \
    --rm \
    -v {$PWD}:/app \
    -v /app/node_modules \
    -p 5000:3000 \
    -e CHOKIDAR_USEPOLLING=true \
    app-dev

It seems that the environment variables are not set conditionally because no matter what image I run prod or dev, the environment variables point to dev.

How do I fix this?

abunickabhi
  • 558
  • 2
  • 9
  • 31
codebarz
  • 327
  • 1
  • 5
  • 28
  • https://stackoverflow.com/questions/43654656/dockerfile-if-else-condition-with-external-arguments – Hassan Imam Aug 30 '21 at 09:36
  • Thanks @HassanImam but the issue here doesn't seem to be how to set the environment variables conditionally. If I am setting it wrong can you point out where exactly because from the link you sent, I am setting it correctly. – codebarz Aug 30 '21 at 09:38
  • 1
    Why not set these variables when you _run_ the image; `docker run -e REACT_APP_BASE_URL=...`? A Docker Compose setup could write them into a file so you don't have to repeat them, or you can use a `docker run --env-file` option to inject the `.env` file. – David Maze Aug 30 '21 at 11:04

2 Answers2

1

blanks in [], >> for append in file

ARG app_env
RUN if [ "$app_env" = "dev" ]; then \
    echo "REACT_APP_BASE_URL=$REACT_APP_BASE_URL" > .env; \
    echo "REACT_APP_WALLET_BASE_URL=$REACT_APP_WALLET_BASE_URL" >> .env; \
    echo "REACT_APP_TRANSACTION_BASE_URL=$REACT_APP_TRANSACTION_BASE_URL" >> .env; \
    else \
    echo "REACT_APP_BASE_URL=$REACT_APP_BASE_URL_PROD" > .env; \
    echo "REACT_APP_WALLET_BASE_URL=$REACT_APP_WALLET_BASE_URL_PROD" >> .env; \
    echo "REACT_APP_TRANSACTION_BASE_URL=$REACT_APP_TRANSACTION_BASE_URL_PROD" >> .env; \
    fi
/app # cat .env
REACT_APP_BASE_URL=https://service.dev.localhost.com
REACT_APP_WALLET_BASE_URL=
REACT_APP_TRANSACTION_BASE_URL=
/app #
/app # cat .env
REACT_APP_BASE_URL=https://service.localhost.com
REACT_APP_WALLET_BASE_URL=
REACT_APP_TRANSACTION_BASE_URL=
/app #

the 2 envs are not set yet..

araisch
  • 1,727
  • 4
  • 15
0

I decided to do this at the command level just as @David Maze mentioned in the comment and it worked. So I passed the args from the comment like this

docker build --build-arg app_env=production --build-arg REACT_APP_BASE_URL=https://service.locahost.com --build-arg REACT_APP_SERVICES_BASE_URL=https://services.localhost.com --build-arg -t image-tag

And I didn't have to pass the args default in the Dockerfile any more so I removed them

# install app dependencies
COPY package.json ./

COPY yarn.lock ./

RUN yarn

# add app
COPY . ./

# define environment variables
ARG REACT_APP_BASE_URL
ARG REACT_APP_SERVICES_BASE_URL
ARG app_env

ENV app_stage $app_env
ENV REACT_APP_BASE_URL=$REACT_APP_BASE_URL
ENV REACT_APP_SERVICES_BASE_URL=$REACT_APP_SERVICES_BASE_URL
codebarz
  • 327
  • 1
  • 5
  • 28