Search code examples
dockerproxyenvironment-variablesdocker-swarmhttp-proxy

Docker proxy settings not consistent


I set a proxy on my host machine according to the docker docs in ~/.docker/config.json (https://docs.docker.com/network/proxy/#configure-the-docker-client):

{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://127.0.0.1:3001",
     "httpsProxy": "http://127.0.0.1:3001"
   }
 }
}

If I start a new container, the environment variables seem to be set. If I check it form the host:

# docker exec <container> echo $http_proxy
# http://127.0.0.1:3001

However, if I enter the container with an interactive shell, the environment variables are not there:

# docker exec -it <container> sh
# echo $http_proxy
#

Why can't I see it here? I seems like my application cannot see the setting neither. Is the proxy setting just set for different users?


Solution

  • The two commands are very different, and not caused by docker, but rather your shell on the host. This command:

    # docker exec <container> echo $http_proxy
    # http://127.0.0.1:3001
    

    Expands the variable in your shell on the host, then passes that expanded variable to the docker command that runs inside the container. It's very similar to running:

    # echo $http_proxy
    http://127.0.0.1:3001
    # docker exec <container> echo http://127.0.0.1:3001
    http://127.0.0.1:3001
    

    The second command you provided does expand the variable inside the container, so this shows you do not have the setting applied inside your container.