Docker Container Networking with Docker-in-Docker

I would like to connect to the container of the child docker from the parent container of the docker, with the docker installed in the docker.

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 with installing dockers in docker, I get an error 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 8080:80 httpd:alpine
/ # curl localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused

I tried a couple of changes with no luck. Interface Indication 0.0.0.0:

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 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 could not find any existing articles about this. Does anyone here have an idea?

Thank!

+13
source share
3 answers

DinD, bind Docker, , , . , , .

Docker-in-Docker, Apache httpd :

1) docker:dind localhost:8080.

2) docker:latest, , , docker:dind. --name mydind, curl mydind:8080 Apache <html><body><h1>It works!</h1></body></html>.

, !

+9

:

2) docker:latest container, [...] , docker:dind. --name mydind, curl mydind:8080 [...]

Gitlab CI DinD ( , ):


, Wordpress API .

, , tutum/wordpress .gitlab-ci.yml:

services:
- tutum/wordpress:latest

, tutum/wordpress, :

  • tutum-wordpress
  • tutum__wordpress

service:
- docker:dind

docker:8080:

  script:
  - docker run -d -p 8080:80 httpd:alpine
  - curl docker:8080

: , , , alias:

services:
- name: docker:dind
  alias: dind-service

  script:
  - docker run -d -p 8080:80 httpd:alpine
  - curl dind-service:8080

Hth,

+4

I am very convinced that @Yuriy Znatokov's answer is what I want, but I understood it a long time ago. To facilitate understanding for subsequent users, I exported all the steps.

1) From docker: container dind

docker run -d --name mydind --privileged docker:dind
/ # docker run -d -p 8080:80 httpd:alpine
/ # curl localhost:8080
<html><body><h1>It works!</h1></body></html>

2) From docker: last container

docker run -d --name mydind --privileged docker:dind
docker run -it --link mydind:docker docker:latest sh
/ # docker run -d -p 8080:80 httpd:alpine
/ # curl mydind:8080
<html><body><h1>It works!</h1></body></html>
0
source

Source: https://habr.com/ru/post/1680431/


All Articles