Search code examples
kubernetesdocker-in-docker

Container in dind access another container in the same Kubernetes pod


In a Kubernetes pod, I have:

  • busybox container running in a dind container
  • fluentd container

I understand if dind wants to access fluentd, it needs to simply connect to localhost:9880. But what if busybox wants to access fluentd as the depicted diagram below. Which address should I use?

dind access to another container


Solution

  • These tips may help you:

    1. First approach

    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 dind, therefore curl dind:busybox_port would give you the standard.

    And then you could from inside the docker:dind container (busybox) connect to fluentd, it will be available on localhost:9880.

    2. Second approach

    Another approach is to EXPOSE [/<protocol>...] and in this case we assume that busyboox and fluentd are in different networks You can also specify this within a docker run command, such as:

    $ docker run --expose=1234 busybox
    

    But EXPOSE will not allow communication via the defined ports to containers outside of the same network or to the host machine. To allow this to happen you need to publish the ports.

    Publish ports and map them to the host

    To publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

    $ docker run -p 80:80/tcp -p 80:80/udp busybox
    

    And then connect from busybox to fluentd using localhost:9880

    You can find more information here: docker-in-docker.

    I hope it helps.