zoit
zoit

Reputation: 667

Passing environment variables from host to docker

I know it is a question that many people have asked. But I don't know what I'm doing wrong. I have this Dockerfile

FROM nginx
ENV USER=${USER}
COPY proof.sh .
RUN chmod 777 proof.sh
CMD echo ${USER} >> /usr/share/nginx/html/index.html
CMD ["nginx", "-g", "daemon off;"]  
EXPOSE 80

When I execute the env command in Linux, I got USER=fran and after that I run these commands: sudo docker run --entrypoint "/bin/sh" 5496674f99e5 ./prueba.sh and as well I run this other docker run --env USER -d -p 89:80 prueba . If I have understood well, doing the last command this environment variable from host it should be passed to the docker, but I don't get anything. Why?. Could you help me?. Thanks in advance

Upvotes: 0

Views: 80

Answers (1)

Adiii
Adiii

Reputation: 60046

This should be like

FROM nginx
ARG USER=default
ENV USER=${USER}
RUN echo ${USER} 
CMD ["nginx", "-g", "daemon off;"]  
EXPOSE 80

So now if you build with some build-args like

docker build --no-cache --build-arg USER=fran_new -t my_image .

You will see in logs like fran_new

or if you want to have user from host OS at run time then pass it as an environment variable at run time then

docker run --name my_container -e USER=$USER my_image

So the container user will be the same as in host.

Upvotes: 1

Related Questions