Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker build has no network, but docker run has

If I want to build my Dockerfile, it can't connect to the network or at least DNS:

Sending build context to Docker daemon 15.95 MB
Sending build context to Docker daemon 
Step 0 : FROM ruby
 ---> eeb85dfaa855
Step 1 : RUN apt-get update -qq && apt-get install -y build-essential libpq-dev
 ---> Running in ec8cbd41bcff
W: Failed to fetch http://httpredir.debian.org/debian/dists/jessie/InRelease  

W: Failed to fetch http://httpredir.debian.org/debian/dists/jessie-updates/InRelease  

W: Failed to fetch http://security.debian.org/dists/jessie/updates/InRelease  

W: Failed to fetch http://httpredir.debian.org/debian/dists/jessie/Release.gpg  Could not resolve 'httpredir.debian.org'

W: Failed to fetch http://httpredir.debian.org/debian/dists/jessie-updates/Release.gpg  Could not resolve 'httpredir.debian.org'

W: Failed to fetch http://security.debian.org/dists/jessie/updates/Release.gpg  Could not resolve 'security.debian.org'

W: Some index files failed to download. They have been ignored, or old ones used instead.
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package build-essential
INFO[0001] The command "/bin/sh -c apt-get update -qq && apt-get install -y build-essential libpq-dev" returned a non-zero code: 100

But if I run exactly the same command via docker run it works:

docker run --name="test" ruby /bin/sh -c 'apt-get update -qq && apt-get install -y build-essential libpq-dev'

Does anybody have an idea, why docker build does not work? I have tried all DNS related tipps on StackOverflow, like starting docker with --dns 8.8.8.8 etc.

Thanks in advance

like image 687
sweiler Avatar asked Aug 01 '15 15:08

sweiler


People also ask

What network does Docker build use?

By default, docker uses the bridge network driver, which connects the containers to a network separate from the host.

How do I expose Docker to network?

By default, when you create or run a container using docker create or docker run , it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag.

Do Docker containers have Internet access by default?

It should have internet access because it's on a custom network. Use --network common2 to make the container use the common2 network. The container cannot reach the internet on the common2 network. If it is started with --network host, then it will have access…


1 Answers

Check what networks are available on your host with the below command:

docker network ls

then pick one that you know is working, the host one could be a good candidate.

Now assuming you are in the directory where it is available your Dokerfile, build your image appending the flag --networks and change the <image-name> with yours:

docker build . -t <image-name> --no-cache --network=host
like image 176
Raffa Avatar answered Oct 29 '22 10:10

Raffa