Docker Community Forums

Share and learn in the Docker community.

Linking source to recipient container - how is port identified , though implicitly?


(Ben Zhou) #1

To my understanding, linking is about establishing implicit port connection to source container in recipient container, thus avoid explicitly exposing source port, or mapping host port to the source container port. Is this correct?

But in the examples using linking in the tutorial, there isn’t a source port specified when starting the source container:

Create the source container using postgres:
docker run -d --name database postgres
a ‘docker ps’ does show the port 5432, though no mapping.

Create the recipient container and link it to source container:
docker run -d -P --name website --link database:db nginx

My question is: how does the recipient container know which port on the source container to connect to? why is ‘–name’ declaration enough to provide direct connection?


(David Maze) #2

Two and a half answers:

  1. The environment variable DB_PORT_5432_TCP has the port number. The 5432 in this is EXPOSEd in the postgres container’s Dockerfile.
  2. The application is probably ignoring this and connecting to the host name “db” using the standard PostgreSQL port 5432. (--link enables direct container-to-container communication, and I don’t think any version of configuration of Docker ever remapped ports.)
  3. If you are using the newer Docker private network system, you don’t need --link; one container can directly use the other container’s name “database” as a host name and, again, use the standard PostgreSQL port.

(Ben Zhou) #3

thank you so much for the detailed explanation.

Actually the tutorial said the reason to use --link is so it can avoid EXPOSing port.

How can I specify a default port while building an image? Specifically, I’m working on creating an MQSeries container image, using a ‘standard’ port of 1440 across the board. An application container will access MQ queue manager via this port.

Right now, I explicitly map the container port 1440 to 11440 on the host. The application container access MQ via 11440 on the host. I would like to use linking so I don’t need to map port. As you explained, I need to set a standard port in the source image. How should I accomplish that?

Also, what is the ‘newer docker private network system’? is it something to install in addition to docker?

many thanks,


(David Maze) #4

Your tutorial might be wrong. The Dockerfile EXPOSE command does two things: the list of ports it names are “exported” to other containers in environment variables using the old-style docker run --link, and the list of ports it names are published on arbitrary ports when you docker run -P (capital P) a container.

You might use docker run --link to avoid needing to explicitly docker run -p 11440:1440 each individual port and needing to know the host IP address.

Is this the “normal” MQ port (in the same way that 443 is the “normal” HTTP/TLS port, 22 is the “normal” ssh port, 5432 is the “normal” PostgreSQL port, etc.)? You can use the same port number on both sides of the -p option if it helps you (in fact that’s pretty typical) which could make it so you don’t have to change client applications’ port settings.

I’d probably start here: https://docs.docker.com/engine/userguide/networking/dockernetworks/#/user-defined-networks

I say “new” in that this functionality was added in Docker…1.9? 1.10? and even though the official documentation is starting to discourage --link it still works and there’s still a lot of material out there that recommends it.