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.
before any apt-get command in your Dockerfile you should put this line
Dont'f forget to create apt.conf in the same folder that you have the Dockerfile, the content of the apt.conf file should be like this:
if you use username and password to connect to your proxy then the apt.conf should be like as below:
for example :
Where the foo is the username and bar is the password.
You can use the
--build-arg
option when you want to build using a Dockerfile.From a link on https://github.com/docker/docker/issues/14634 , see the section "Build with --build-arg with multiple HTTP_PROXY":
NOTE: On your own system, make sure you have set the HTTP_PROXY and NO_PROXY environment variables.
UPDATE:
You have wrong capitalization of environment variables in ENV. Correct one is
http_proxy
. Your example should be:or
All variables specified in ENV are prepended to every RUN command. Every RUN command is executed in own container/environment, so it does not inherit variables from previous RUN commands!
Note: There is no need to call docker daemon with proxy for this to work, although if you want to pull images etc. you need to set the proxy for docker deamon too. You can set proxy for daemon in
/etc/default/docker
in Ubuntu (it does not affect containers setting).Also, this can happen in case you run your proxy on host (i.e. localhost, 127.0.0.1). Localhost on host differ from localhost in container. In such case, you need to use another IP (like 172.17.42.1) to bind your proxy to or if you bind to 0.0.0.0, you can use 172.17.42.1 instead of 127.0.0.1 for connection from container during
docker build
.You can also look for an example here: How to rebuild dockerfile quick by using cache?
Use --build-arg in lower case environment variable:
If you have the proxies set up correctly, and still cannot reach the internet, it could be the DNS resolution. Check
/etc/resolve.conf
on the host Ubuntu VM. If it containsnameserver 127.0.1.1
, that is wrong.Run these commands on the host Ubuntu VM to fix it:
Now
/etc/resolv.conf
should have a valid value for nameserver, which will be copied by the docker containers.As suggested by other answers,
--build-arg
may be the solution. In my case, I had to add--network=host
in addition to the--build-arg
options.