Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing environment variables in dockerfile

I need to pass the environment variable in my Dockerfile like below. May i know the efficient way to do this. I tried using build args

docker build --build-arg myIP=123 --rm -t react_2811_1154 .

but it didnt work.

Here is my Dockerfile

ARG myIP

FROM node:11

ENV myIP1 $myIP


ENV REACT_APP_MOCK_API_URL=http://${myIP1}:8080/API
ENV REACT_APP_MOCK_API_URL_AUTH=http://${myIP1}:8080/API/AUTH
ENV REACT_APP_MOCK_API_URL_PRESENTATION=http://${myIP1}:8080/API/PRESENTATION

# set working directory
RUN mkdir /usr/src/app/
WORKDIR /usr/src/app/

COPY . /usr/src/app/.

RUN npm install 

#RUN npm start
CMD ["npm", "start" ]

So when i run my docker container i believe i dont want to send any environment variable to it.

Please advise.

like image 649
Ranjith Avatar asked Mar 06 '26 13:03

Ranjith


1 Answers

I ran your Dockerfile and myIP is indeed empty when I run env inside of the container.

To fix it, try putting the ARG line AFTER the FROM line.

So,

FROM node:11

ARG myIP

ENV myIP1 $myIP

ENV REACT_APP_MOCK_API_URL=http://${myIP1}:8080/API
ENV REACT_APP_MOCK_API_URL_AUTH=http://${myIP1}:8080/API/AUTH
ENV REACT_APP_MOCK_API_URL_PRESENTATION=http://${myIP1}:8080/API/PRESENTATION

Building using this Dockerfile, I was able to set myIP.

like image 165
Rickkwa Avatar answered Mar 08 '26 21:03

Rickkwa