0

I would like to set http proxy and https proxy to a docker container while building it. Hence I configured it with ENV keywords at the begining of the Dockerfile. Something like this

FROM hub.docker.example.com/fedora:25

ENV http_proxy=http://example.com:911
ENV https_proxy=https://example.com:912


RUN \
  .
  .
  my tasks, download something which need proxy
  .
  .

ENTRYPOINT sleep infinity

But when I run the container and check with command ENV | grep proxy, there is no proxy being set. Am I doing anything wrong on the Dockerfile?

*It's a fedora image

2 Answers 2

0

If you go through the documentation this is clearly mentioned.

If your container needs to use an HTTP, HTTPS, or FTP proxy server, you can configure it in different ways:

  • In Docker 17.07 and higher, you can configure the Docker client to pass proxy information to containers automatically.

Configure the Docker client

On the Docker client, create or edit the file ~/.docker/config.json in the home directory of the user which starts containers. Add JSON such as the following, substituting the type of proxy with httpsProxy or ftpProxy if necessary, and substituting the address and port of the proxy server. You can configure multiple proxy servers at the same time.

so using this method you get rid of older methods.

When you create or start new containers, the environment variables are set automatically within the container

   {
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://127.0.0.1:3001",
     "noProxy": "*.test.example.com,.example2.com"
   }
 }
}

For older version

When you build the image, or using the --env flag when you create or run the container, you can set one or more of the following variables to the appropriate value. This method makes the image less portable,

enter image description here

2
  • I think he's not trying to create it in cli, but in Dockerfile. So, ENV in Dockerfile better than --env in cli, although they produces the same. Furthermore, his proposal is not about HTTP servers. HTTP server is a consequence, because the fact is an issue about ENV variables in Dockerfile which apparently are not in the container after. Apr 17, 2018 at 8:32
  • share your exact dockerfile may be remove in some intermediae layers
    – Adiii
    Apr 17, 2018 at 9:15
0

I've just checked your issue with fedora and ubuntu and ENV are set. I don't understand your steps but I can post mine, clearly separated so you could tell me if there's something wrong in which of them: My Docker file looks as follows:

FROM fedora

ENV http_proxy=http://example.com:911
ENV https_proxy https://example.com:912

ENTRYPOINT sleep infinity

After that, which takes lastest Fedora, build image executing in the same directory you have the Dockerfile:

docker build -t yourimagename .
docker run -d --name yourcontainername yourimagename 
docker exec -ti yourcontainername bash

Check environment variables with env, not ENV:

env | grep http
https_proxy=https://example.com:912
http_proxy=http://example.com:911

Definitively, you are defining ENV correctly in your Dockerfile. Furthermore, you could also would define them without '=' sign. Both are correct:

ENV http_proxy=http://example.com:911

OR

ENV http_proxy http://example.com:911
7
  • That is an image provided by my organisation. I logged in to that container with run -it <image> /bin/sh and try command import, it shows that command not found. Is this causing me from setting the environment variable?
    – Jeff Pang
    Apr 17, 2018 at 9:14
  • ENV | grep proxy was a typo :)
    – Jeff Pang
    Apr 17, 2018 at 9:25
  • What about pulling image? docker pull hub.docker.example.com/fedora:25 does work? Let's try step by step from the beginning Apr 17, 2018 at 9:35
  • Pulling image was success, hence i can see the image with docker images and it's able to start & exec too.
    – Jeff Pang
    Apr 17, 2018 at 9:40
  • Do you have any http_proxy configuration in other steps in your Dockerfile which could rewrite ENV? Could you try the same Dockerfile without intermediate steps? ARGs are only available in build step but ENV should persist after container start. Apr 17, 2018 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.