Get the App
SLTechnology News&Howtos  ›  Servers  › 

Implementation of Docker Port Mapping

Shulou Source: shulou.com Published: 2022-06-02 04:46:53 10月03日 Update

When the docker container starts, if you do not specify the port mapping parameters, the network applications and services in the container cannot be accessed through the network outside the container.

It can also be configured using the EXPOSE directive in the Dockerfile file.

Port mapping can be implemented using-p,-P:

-p specifies the port to be mapped, and only one container can be bound to a specified port-P randomly maps the open network port inside the container to one port of the host

Formats supported by port mapping:

Ip:hostport:containerport # specify ip, specify host port, specify container portip::containerport # specify ip, not specify host port (random), specify container porthostport:containerport # not specify ip, specify host port, specify container port

There are five ways to map ports:

First, all ports exposed by the container are randomly mapped to the host.

For example: (not recommended)

Docker run-P-it ubuntu / bin/bash

Second, randomly map the specified port of the container to a port of the host.

For example:

Docker run-P 80-it ubuntu / bin/bash

The above instruction will randomly map port 80 of the container to a port of the host.

Third, map the specified port of the container to a port of the host.

For example:

Docker run-p 8000 80-it ubuntu / bin/bash

The above instruction maps 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 Freund 80-it ubuntu / bin/bash

The above instruction will randomly map the ip192.168.0.100 and port 80 of the container to a port of the host.

5. Map the container ip and port to the host.

Docker run-p 192.168.0.100 bin/bash 8000 it ubuntu 80

The above instruction maps the container's ip192.168.0.100 and port 80 to the host's port 8000.

Example:

# Mapping port 80 of nginx to port 800 of host docker run-d-it-p 800 it 80 nginx

View mapped port configuration

Docker port container_ID # Container ID# result output 80/tcp-> 0.0.0.0 80/tcp 800

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Tags: Port container host host instruction network support configuration content parameters that is files methods times formats examples results network applications learning help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS OPPO Reno Xiaomi Microsoft