I am running a virtual machine (Ubuntu 13.10) with docker (version 0.8.1, build a1598d1). I am trying to build an image with a dockerfile. First, I want to update the packages (using the code below - the proxy is obfuscated) but apt-get
times out with the error: Could not resolve 'archive.ubuntu.com'
.
FROM ubuntu:13.10
ENV HTTP_PROXY <HTTP_PROXY>
ENV HTTPS_PROXY <HTTPS_PROXY>
RUN export http_proxy=$HTTP_PROXY
RUN export https_proxy=$HTTPS_PROXY
RUN apt-get update && apt-get upgrade
I have also run the following in the host system:
sudo HTTP_PROXY=http://<PROXY_DETAILS>/ docker -d &
The host is able to run apt-get
without issue.
How can I change the dockerfile to allow it to reach the ubuntu servers from within the container?
Update
I ran the code in CentOS (changing the FROM ubuntu:13.10
to FROM centos
) and it worked fine. It seems to be a problem with Ubuntu.
We are doing ...
and at end of dockerfile ...
This, for now (until docker introduces build env vars), allows the proxy env vars to be used for the build ONLY without exposing them
The alternative to solution is NOT to build your images locally behind a proxy but to let docker build your images for you using docker "automated builds". Since docker is not building the images behind your proxy the problem is solved. An example of an automated build is available at ...
https://github.com/danday74/docker-nginx-lua (GITHUB repo)
https://registry.hub.docker.com/u/danday74/nginx-lua (DOCKER repo which is watching the github repo using an automated build and doing a docker build on a push to the github master branch)
Updated on 02/10/2018
With new feature in docker option
--config
, you needn't set Proxy in Dockerfile any more. You can have same Dockerfile to be used in and out corporate environment.or environment variable
DOCKER_CONFIG
https://docs.docker.com/engine/reference/commandline/cli/
https://docs.docker.com/network/proxy/
I recommend to set proxy with
httpProxy, httpsProxy, ftpProxy
andnoProxy
.Adjust proxy IP and port if needed and save to
~/.docker/config.json
After yo set properly with it, you can run docker build and docker run as normal.
Old answer
Below setting in Dockerfile works for me. I tested in
CoreOS
,Vagrant
andboot2docker
. Suppose the proxy port is3128
In Centos:
In Ubuntu:
Be careful of the format, some have http in it, some haven't, some with single quota. if the IP address is 192.168.0.193, then the setting will be:
In Centos:
In Ubuntu:
If you need set proxy in coreos, for example to pull the image
and If you want to set proxy for wget you should put these line in your Dockerfile
i had the same problem and found another little workaround: i have a provisioner script that is added form the docker build environment. In the script i set the environment variable dependent on a ping check:
Dockerfile:
script.sh:
this is still somewhat crude but worked for me
As Tim Potter pointed out, setting proxy in dockerfile is horrible. When building the image, you add proxy for your corporate network but you may be deploying in cloud or a DMZ where there is no need for proxy or the proxy server is different.
Also, you cannot share your image with others outside your corporate n/w.
A slight alternative to the answer provided by @Reza Farshi (which works better in my case) is to write the proxy settings out to
/etc/apt/apt.conf
usingecho
via the Dockerfile e.g.:The advantage of this approach is that the proxy addresses can be passed in dynamically at image build time, rather than having to copy the settings file over from the host.
e.g.
docker build --build-arg HTTP_PROXY=http://<host>:<port> --build-arg HTTPS_PROXY=http://<host>:<port> .
as per docker build docs.