30

I am newbie for docker. I try set a proxy for debian:jessie image but i didnt make it. I follow this link . I apply all of them with cat tag (example: 'cat > proxy.sh' , because vi or another editor not installed ) but there is some error about my proxy in apt-get update command.

Error Photo

enter image description here

My proxy : http://username:password@proxy2.domain.com

MinnuKaAnae
  • 1,646
  • 3
  • 23
  • 35
Sinan Barut
  • 510
  • 1
  • 6
  • 14

3 Answers3

42

You can set the proxy environment variables when starting the container, for example:

docker container run \
  -e HTTP_PROXY=http://username:password@proxy2.domain.com \
  -e HTTPS_PROXY=http://username:password@proxy2.domain.com \
  myimage

If you want the proxy-server to be automatically used when starting a container, you can configure default proxy-servers in the Docker CLI configuration file (~/.docker/config.json). You can find instructions for this in the networking section in the user guide.

For example:

{
  "proxies": {
    "default": {
      "httpProxy": "http://username:password@proxy2.domain.com",
      "httpsProxy": "http://username:password@proxy2.domain.com"
    }
  }
}

To verify if the ~/.docker/config.json configuration is working, start a container and print its env:

docker container run --rm busybox env

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=220e4df13604
HTTP_PROXY=http://username:password@proxy2.domain.com
http_proxy=http://username:password@proxy2.domain.com
HTTPS_PROXY=http://username:password@proxy2.domain.com
https_proxy=http://username:password@proxy2.domain.com
HOME=/root
thaJeztah
  • 27,738
  • 9
  • 73
  • 92
  • 2
    Where's the proxy domain registered? Is the DNS that it's registered in accessible by docker? What happens if you specify the proxy's IP-address instead of the domain name? `Could not resolve` indicates a DNS issue. – thaJeztah Dec 20 '17 at 09:07
  • This was helpful. I had assumed that setting the proxy in `/etc/systemd/system/docker.service.d/proxy.conf` would do this, but apparently that is only for docker daemon (like `docker pull` commands, not for containers). – wisbucky Jun 27 '18 at 17:51
  • 1
    If I set up the `~/.docker/config.json` and I run a container and then `printenv`, I don't see the env var `httpProxy` and it doesn't work. – Alexis Aug 02 '18 at 23:01
  • The environment variables are named different than the options in `config.json`; `httpProxy` sets the `HTTP_PROXY` environment variable – thaJeztah Aug 05 '18 at 07:23
  • This didn't work for me either... I used a simple html boilerplate site which accesses for example the jquery cdn and google analytics but there is nothing showing in my proxy logs. If I build the nginx image, it does go through the proxy to pull the packages it needs. So it seems like the httpProxy settings are not passed to the containers... – PiotrG Jan 11 '19 at 18:55
  • I added a step to check if the configuration is applied – thaJeztah Jan 12 '19 at 17:16
  • Thought setting in the Docker's GUI (Win 10) was enough – Tony Thomas May 16 '19 at 06:22
  • 1
    I print the env as mentioned above but it's not printing the proxy details in the config.json. Am I missing anything can someone please help me – user9473385 Apr 19 '21 at 04:48
  • Stupid mistake: I created the `.docker` folder in the wrong folder. Don't forget the `~/` – Drarig29 May 10 '21 at 17:45
  • Watchout, some distributions require you to use lower case so consider using '-e http_proxy ...' instead of '-e HTTP_PROXY ...' – Greg7000 Aug 25 '23 at 13:02
  • The `~/.docker/config.json` option will set both; it sets both `http_proxy` and `HTTP_PROXY` if you configured it. – thaJeztah Aug 28 '23 at 17:17
5

you need instruct the apt script to connect through proxy inside the container

# echo 'Acquire::http::proxy "proxy:port/";' > /etc/apt/apt.conf.d/40proxy

remember, this should be written inside the container

and in the machine that have docker running, the proxy should be configured like people said before in their comments

George Poliovei
  • 1,009
  • 10
  • 12
1

I had to put the proxy server in quotes for it to work for me.

docker container run \
  -e HTTP_PROXY="http://username:password@proxy2.domain.com" \
  -e HTTPS_PROXY="http://username:password@proxy2.domain.com" \
  myimage