Implementation of Docker port mapping
- 2020-11-30 08:41:31
- OfStack
When the docker container is started, network applications and services inside the container cannot be accessed from outside the container without specifying port mapping parameters.
It can also be configured using the EXPOSE directive in the Dockerfile file.
Port mapping can be implemented using -ES8en and -ES9en:
Formats supported by port mapping:
ip:hostport:containerport # The specified ip , specify the host port , specify the container port
ip::containerport # The specified ip Host is not specified port (random), specify the container port
hostport:containerport # Is not specified ip , specify the host port , specify the container port
There are five ways to map ports:
1. Randomly map all ports exposed by the container to the host.
For example :(not recommended)
docker run -P -it ubuntu /bin/bash
2. Randomly map the container specified port to one port of the host.
Such as:
docker run -P 80 -it ubuntu /bin/bash
The above instructions will randomly map port 80 of the container to one port of the host.
3. Map the container specified port to one port of the host.
Such as:
docker run -p 8000:80 -it ubuntu /bin/bash
The above instructions map port 80 of the container to port 8000 of the host.
4. Randomly map the container ip and port to the host.
docker run -P 192.168.0.100::80 -it ubuntu /bin/bash
The above instructions will randomly map the container's ip192.168.0.100 and 80 ports to one port on the host machine.
5. Map container ip and port to host specified.
docker run -p 192.168.0.100:8000:80 -it ubuntu /bin/bash
The above instructions map the container's ports ip192.168.0.100 and 80 to the host's port 8000.
Example:
# will nginx the 80 Port mapped to the host 800 On port
docker run -d -it -p 800:80 nginx
View the map port configuration
docker port container_ID # The container ID
# Results output
80/tcp -> 0.0.0.0:800