0

I am facing a challenge while working with Docker in a corporate network environment. To overcome the network restrictions, I have configured the Docker daemon's DNS as mentioned in this Stack issue. Additionally, I have set the proxy environment variables in the Docker image as follows:

ENV http_proxy = http://login:pass@proxy-server.fr:1111
ENV https_proxy = http://login:pass@proxy-server.fr:1111
ENV ftp_proxy = http://login:pass@proxy-server.fr:1111
ENV no_proxy = 127.0.0.1, z.z.z.z , y.y.y.y, x.x.x.x,localhost

By doing this I succeded to bypass the apt-get, but the problem here is that when I try to do this by writing the proxy parameters inbside of /etc/environment using a command in this form:

RUN echo "\nexport http_proxy = http://login:pass@proxy-server.fr:1111\nexport https_proxy = ... etc" >> /etc/environment

In order to refresh the environment variables I follow this Stack issue by adding this line to change the default shell from /bin/sh to /bin/bash Note that this is needed otherwise you'll get the error /bin/sh source command not found

SHELL ["/bin/bash", "-c"]
RUN source /etc/environment  

Then to check if the refreshing has happened I just type

env | grep proxy 

There is no proxy configuration and there for I can not perform ** RUN apt-get update**

Note that if I run the container and perform this refreshing command

source /etc/environment

And then perform apt-get update every thing goes well !!!
I don't really get what is exactely the problem thank you for giving any explanation.

Thank you for your reading.

VERSIONS:

  • Docker version 20.10.23
  • Kubuntu 22.04 LTS
  • Reading environment variables from a file in Docker can be tricky. Using Dockerfile `ENV` will be easier than trying to write the values into a shell dotfile that won't usually get read. – David Maze Feb 01 '23 at 19:18
  • Thank you for your response @David, yes I guess that's what I am doing write know but I tried to do as said in the response bellow I guess the docker is not even capable of performing the **source /etc/environment** because it ignores the **apt-get install** that comes after the && – sabri mahmoud Feb 02 '23 at 08:08
  • If you want the proxy settings to be available to subsequent dockerfiler RUN commands, then use `ARG` in place of `ENV`. `ARG` injects environment variables into the Dockerfiles build environment (i.e. the environment each RUN command is executed with). ENV adds env environment variables to the runtime environment (the environment given to the CMD directive that starts a container). – Chris Becke Feb 02 '23 at 08:34

1 Answers1

0

Every RUN command in Dockerfile starts a new shell. So when you call RUN source /etc/environment The environment is set for this shell but for the next RUN apt-get update a new shell is started and it has a new environment which doesn't have your http_proxy variable.

The convention in these situations is to use ENV insturction just like you mentioned at start of the question. But if you're sure that you want to use /etc/environment file you can use it like this:

RUN source /etc/environment && apt-get update
  • Thank you @MohammadAmineSalarkia for your response, the problem here is that source /etc/environment command is not refreshing so with the condition && that you put the docker-daemon won't even look to what's after the &&, besides that's true that the command is set for a specific shell but note that "The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile" as said in [Dockerfile reference](https://docs.docker.com/engine/reference/builder/) – sabri mahmoud Feb 02 '23 at 08:02