Docker port mapping is described in detail

  • 2020-05-17 07:09:05
  • OfStack

Docker port mapping:

Recently, I arranged the data of Docker port mapping for 1 time, so as to be applied in the following projects. You can also refer to it.


# Find IP address of container with ID <container_id>  By the container  id  To obtain  ip $ sudo docker inspect <container_id> | grep IPAddress | cut -d '"' -f 4

In any case, these ip are based on local systems and the container's ports are not accessible to non-local hosts. In addition, in addition to the fact that ports are only locally accessible, another problem with containers is that these ip's change every time the container is started.
Docker solves both of these container problems and provides a simple and reliable way to access services inside the container. Docker binds the interface of the host system through a port, allowing non-local clients to access services running inside the container. To facilitate communication between containers, Docker provides this connection mechanism.

5.1 automatic mapping of ports

-P needs to be specified when in use -- the expose option specifies the port on which the service is to be provided

$ sudo docker run -t -P --expose 22 --name server ubuntu:14.04

Using docker run-P to automatically bind all container ports that provide external services, the mapped ports will be drawn from the unused port pool (49000.. 49900) auto selection, you can go through docker ps, docker inspect < container_id > Or docker port < container_id > < port > Determine the specific binding information.

5.2 bind the port to the specified interface

The basic grammar

$ sudo docker run -p [([ < host_interface > :[host_port]])|( < host_port > ):] < container_port > [/udp] < image > < cmd >

By default no binding ip is specified and all network interfaces are listened to.

Bind TCP port

# Bind TCP port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine. $ sudo docker run -p 127.0.0.1:80:8080 < image > < cmd > # Bind TCP port 8080 of the container to a dynamically allocated TCP port on 127.0.0.1 of the host machine. $ sudo docker run -p 127.0.0.1::8080 < image > < cmd > # Bind TCP port 8080 of the container to TCP port 80 on all available interfaces of the host machine. $ sudo docker run -p 80:8080 < image > < cmd > # Bind TCP port 8080 of the container to a dynamically allocated TCP port on all available interfaces $ sudo docker run -p 8080 < image > < cmd >

Bind UDP port

# Bind UDP port 5353 of the container to UDP port 53 on 127.0.0.1 of the host machine. $ sudo docker run -p 127.0.0.1:53:5353/udp < image > < cmd >

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: