Search code examples
dockerdocker-containerdocker-network

Docker - connecting to an open port in a container


I'm new to docker and maybe this is something I don't fully understand yet, but what I'm trying to do is connect to an open port in a running docker container. I've pulled and run the rabbitmq container from hub (https://hub.docker.com/_/rabbitmq/). The rabbitmq container should uses port 5672 for clients to connect to.

After running the container (as instructed in the hub page):

$ docker run -d --hostname my-rabbit --name some-rabbit rabbitmq:3

Now what I want to do is telnet into the open post (it is possible on a regular rabbitmq installation and should be on a container as well).

I've (at least I think I did) gotten the container IP address using the following command:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

And the result I got was 172.17.0.2. When I try to access using telnet 172.17.0.2 5672 it's unsuccessful.

The address 172.17.0.2 seems strange to me because if I run ipconfig on my machine I don't see any interface using 172.17.0.x address. I do see Ethernet adapter vEthernet (DockerNAT) using the following ip: 10.0.75.1. Is this how it is supposed to be?

If I do port binding (adding -p 5672:5672) then I can telnet into this port using telnet localhost 5672 and immidiatly connect.

What am I missing here?


Solution

  • As you pointed out, you need port binding in order to achieve the result you need because you are running the application over the default bridge network (on Windows i guess).

    From the official docker doc

    Containers connected to the same user-defined bridge network automatically expose all ports to each other, and no ports to the outside world. [...] If you run the same application stack on the default bridge network, you need to open both the web port and the database port, using the -p or --publish flag for each. This means the Docker host needs to block access to the database port by other means.

    Later in the rabbitmq hub there is a reference to a Management Plugin which is run by executing the command

    docker run -d --hostname my-rabbit --name some-rabbit -p 8080:15672 rabbitmq:3-management
    

    Which exposes the port 8080 used for management which I think is what you may need. You should also notice that they talk about clusters and nodes there, maybe they meant the container to be run as a service in a swarm (hence using the overlay network and not the bridge one).

    Hope I could help somehow :)