Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding docker port mappings

Please explain the following output:

docker ps ExanpleContainter

Ports
--------
10.10.10.10:443->443/tcp, 0.0.0.0:1937->1937/tcp

And, docker inspect ExanpleContainter

"Ports": {
  "1937/tcp": [
    {
      "HostIp": "0.0.0.0",
      "HostPort": "1937"
    }
  ],
  "443/tcp": [
    {
      "HostIp": "10.10.10.10",
      "HostPort": "443"
    }
  ]
}

I'd want to understand how mapping is done and which port is mapped where?

like image 754
Aniruddha Avatar asked Jan 23 '17 02:01

Aniruddha


People also ask

What do the ports mean Docker?

Ports This section is used to define the mapping between the host server and Docker container. ports: - 10005:80. It means the application running inside the container is exposed at port 80. But external system/entity cannot access it, so it need to be mapped to host server port.

Which ports does Docker use?

The Docker client will default to connecting to unix:///var/run/docker.sock on Linux, and tcp://127.0.0.1:2376 on Windows. For example: tcp:// -> TCP connection to 127.0. 0.1 on either port 2376 when TLS encryption is on, or port 2375 when communication is in plain text.

What is the significance of 80 8080 when used in the Docker?

Port 80 is the default port. It's what gets used when no port is specified. 8080 is Tomcat's default port so as not to interfere with any other web server that may be running. If you are going to run Tomcat as your web server, the port can be changed to 80 so that visitors do not need to specify it.

How do you map a host port to the container port?

Method to Map a Host Port to a Container Port in Docker You can open it by searching in the application menu by typing the keyword “terminal” in the search bar or by utilizing the “Ctrl+Alt+T” shortcut key. Once it is opened, you have to write the below-listed command to run the image named 'nginx' with the –P flag.


2 Answers

10.10.10.10:443->443/tcp

This means docker host IP 10.10.10.10 on port 443 is mapped to specified container on port 443. packets arriving at that ip port combination will be accessible in the container on the same port (443)

0.0.0.0:1937->1937/tcp

Packets arriving on all available IP addresses (0.0.0.0) on docker host on port 1937 are accessible on specified container on the same port (1937)

Read explanation in the answer and compare it to output of docker inspect ExanpleContainter.

like image 143
Farhad Farahi Avatar answered Sep 27 '22 22:09

Farhad Farahi


Above answer is correct I am trying to explain another way which helped me understand port mappings.

docker ps ExanpleContainter

Ports


10.10.10.10:443->443/tcp, 0.0.0.0:1937->1937/tcp

Here your container ExanpleContainer is exposing two ports.

  1. 10.10.10.10:443->443/tcp
  2. 0.0.0.0:1937->1937/tcp

Output in following format,

[IP_ADDR]:{PORT_OUTSIDE_CONTAINER} -> {PORT_INSIDE_CONTAINER} / {PROTOCOL} 

Now while creating container if you don't specify which internal ports to map it will map random ports but in your case both (host and exposed) ports are mapped same.

like image 42
darth vader Avatar answered Sep 28 '22 00:09

darth vader