Docker Container Networking with Docker-in-Docker

前端 未结 3 559
傲寒
傲寒 2021-02-04 01:35

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

相关标签:
3条回答
  • 2021-02-04 01:53

    Building upon Yuriy's answer:

    2) From inside the docker:latest container, [...] 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 [...]

    In the Gitlab CI config, you can address the DinD container by the name of its image (in addition to the name of its container, which is auto-generated):

    Accessing the services


    Let’s say that you need a Wordpress instance to test some API integration with your application.

    You can then use for example the tutum/wordpress image in your .gitlab-ci.yml:

    services:
    - tutum/wordpress:latest
    

    If you don’t specify a service alias, when the job is run, tutum/wordpress will be started and you will have access to it from your build container under two hostnames to choose from:

    • tutum-wordpress
    • tutum__wordpress

    Using

    service:
    - docker:dind
    

    will allow you to access that container as docker:8080:

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

    Edit: If you'd prefer a more explicit host name, you can, as the documentation states, use an alias:

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

    and then

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

    Hth, dtk

    0 讨论(0)
  • 2021-02-04 01:56

    There are pros and cons for both DinD and bind mounting the Docker socket and there are certainly use cases for both. As an example, check out this set of blog posts, which does a good job of explaining one of the use cases.

    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>.

    Hope it makes sense!

    0 讨论(0)
  • 2021-02-04 01:56

    I am very convinced that @Yuriy Znatokov's answer is what I want, but I have understood it for a long time. In order to make it easier for later people to understand, I have exported the complete steps.

    1) From inside the docker:dind container

    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 inside the docker:latest 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 讨论(0)
提交回复
热议问题