Docker Container Networking with Docker-in-Docker

+1 vote

I would like to network with a child docker container from a parent docker container, with a docker-in-docker setup.

Let's say I'm trying to connect to a simple Apache httpd server. When I run the httpd container on my host machine, everything works fine:

asnyder:~$ docker run -d -p 8080:80 httpd:alpine
asnyder:~$ curl localhost:8080
<html><body><h1>It works!</h1></body></html>
But when I do the same from a docker-in-docker setup, I get a Connection refused error:
asnyder:~$ docker run -d --name mydind --privileged docker:dind
asnyder:~$ docker run -it --link mydind:docker docker:latest sh
/ # docker run -d -p 8080:80 httpd:alpine
/ # curl localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused
asnyder:~$ docker run -d --name mydind --privileged docker:dind
asnyder:~$ docker run -it --link mydind:docker docker:latest sh
/ # docker run -d -p 0.0.0.0:8080:80 httpd:alpine
/ # curl 0.0.0.0:8080
curl: (7) Failed to connect to 0.0.0.0 port 8080: Connection refused

Using the host network:

asnyder:~$ docker run -d --name mydind --privileged docker:dind
asnyder:~$ docker run -it --link mydind:docker docker:latest sh
/ # docker run -d --network host httpd:alpine
/ # curl localhost:80
curl: (7) Failed to connect to localhost port 80: Connection refused

Surprisingly, I was unable to find any existing articles on this. Does anyone here have some insight?

Jul 30, 2018 in Docker by Hannah
• 16,300 points
691 views

1 answer to this question.

0 votes

Given your example docker-in-docker setup above, you can access Apache httpd server in one of two ways:

1) From inside the docker:dind container, it will be available on localhost:8080.

2) From inside the docker:latest container, where you were trying to access it originally, it will be available on whatever hostname is set for the docker:dind container. In this case, you used --name mydind, therefore curl mydind:8080 would give you the standard
Apache <html><body><h1>It works!</h1></body></html>.
answered Jul 30, 2018 by Kalgi
• 42,330 points
I tried exactly this, but it did not work for me.

to boot up the parent container i do :

docker run -v //var/run/docker.sock:/var/run/docker.sock -it myLocalDockerImage /bin/bash

then from inside the container I run:

docker run -d -p 5432:5432 --name db myPostgresImage

but then i type:

ping db

and it says Temporary failure in name resolution (this also happens if i type ping db:5432)

Make sure your DNS Resolver config file is writable:

sudo chmod o+r /etc/resolv.conf

Related Questions In Docker

0 votes
1 answer

Enter in a Docker container already running with a new TTY?

Here is what you can try. For docker ...READ MORE

answered Oct 26, 2018 in Docker by shubham
• 6,890 points
178 views
+2 votes
6 answers
0 votes
1 answer

Different file owner inside Docker container and in host machine Ask

Filesystems, at least in Unix- and Linux-like ...READ MORE

answered Jun 25, 2018 in Docker by Damon Salvatore
• 5,510 points
82 views
0 votes
1 answer

Different file owner inside Docker container and in host machine

Here is what you can try. Since ...READ MORE

answered Jun 27, 2018 in Docker by Atul
• 10,240 points
250 views
0 votes
1 answer
0 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 56,920 points
224 views
0 votes
1 answer

ps command doesn't work in docker container

ps is not installed in the base ...READ MORE

answered Jul 27, 2018 in Docker by Kalgi
• 42,330 points
1,291 views
0 votes
1 answer