Docker primary network port mapping configuration

Docker primary network port mapping configuration

Port Mapping

Before the Docker container is started, if port mapping is not performed, network applications and services in the container cannot be accessed through the network outside the container. Because some network applications and services are often run in containers, if you want to access the network applications and services in the container through the network outside the container, you need to map ports to the container. You can use the -P or -p option in the docker run command to map ports.

Random port mapping

When port mapping is performed through the -P option in the docker run command, Docker will randomly map a port to the network port opened by the container.

Taking nginx as an example, first pull the nginx image.

[root@izoq008ryseuupz ~]# docker image pull nginx
Using default tag: latest
latest: Pulling from library/nginx
852e50cd189d: Already exists 
571d7e852307: Pull complete 
addb10abd9cb: Pull complete 
d20aa7ccdb77: Pull complete 
8b03f1e11359: Pull complete 
Digest: sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@izoq008ryseuupz ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest bc9a0695f571 2 days ago 133MB

Then create a container nginx.1 with the nginx image and use the -P option to perform random port mapping.

[root@izoq008ryseuupz ~]# docker run -itd -P --name nginx.1 nginx
91e3c7ed957f8196fd631eb7bce21acaa96bc253551303ecba2bf193201284ba

Through the docker ps -l command, you can see that port 32775 of the local host is mapped to port 80 of the container. At this time, access port 32775 of the local host to access the interface provided by the nginx application in the container.

[root@izoq008ryseuupz ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91e3c7ed957f nginx "/docker-entrypoint.…" 17 minutes ago Up 5 seconds 0.0.0.0:32775->80/tcp nginx.1

insert image description here

Through the docker logs nginx.1 command, you can see the record of accessing the interface provided by the nginx application in the container with the browser.

[root@izoq008ryseuupz ~]# docker logs nginx.1
...
111.8.49.168 - - [27/Nov/2020:09:03:53 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"
2020/11/27 09:03:53 [error] 21#21: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 111.8.49.168, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "ip:32775", referrer: "http://ip:32775/"
111.8.49.168 - - [27/Nov/2020:09:03:53 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://ip:32775/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"

Specifying port mapping

Use the -p option in the docker run command to specify port mapping. Map port 8083 of the local host to port 80 of the container.

[root@izoq008ryseuupz ~]# docker run -itd -p 8083:80 --name nginx.2 nginx
0e62792a194559bf33c2b39f70b64133cb89a48846278ccee651fda68aa6dad1
[root@izoq008ryseuupz ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e62792a1945 nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 0.0.0.0:8083->80/tcp nginx.2

At this time, access port 8083 of the local host to access the interface provided by the nginx application in the container.

insert image description here

Through the docker logs nginx.2 command, you can also see the record of accessing the interface provided by nginx application in the container with the browser.

[root@izoq008ryseuupz ~]# docker logs nginx.2
...
111.8.49.168 - - [27/Nov/2020:09:28:32 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"
2020/11/27 09:28:32 [error] 28#28: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 111.8.49.168, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "ip:8083", referrer: "http://ip:8083/"
111.8.49.168 - - [27/Nov/2020:09:28:32 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://ip:8083/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"

Through the docker ps command, you can see that the port mappings of the two containers created previously are both on 0.0.0.0 of the local host.

[root@izoq008ryseuupz ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e62792a1945 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 0.0.0.0:8083->80/tcp nginx.2
91e3c7ed957f nginx "/docker-entrypoint.…" 53 minutes ago Up 36 minutes 0.0.0.0:32775->80/tcp nginx.1

Explanation of 0.0.0.0 on the Internet

First, 0.0.0.0 cannot be ping . In the server, 0.0.0.0 is not a real IP address, it represents all the IPv4 addresses in the local machine. Listening to port 0.0.0.0 means listening to the ports of all IPs in the local machine.

C:\Users\Kaven>ping 0.0.0.0

Pinging 0.0.0.0 with 32 bytes of data:
PING: Transmission failed. Common faults.
PING: Transmission failed. Common faults.
PING: Transmission failed. Common faults.
PING: Transmission failed. Common faults.

Ping statistics for 0.0.0.0:
  Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

Specified port mapping for a specified address

You can use the format of ip:hostPort:containerPort to map a specified port to a specified address, such as 127.0.0.1:8083 .

[root@izoq008ryseuupz ~]# docker stop nginx.2
nginx.2
[root@izoq008ryseuupz ~]# docker run -itd -p 127.0.0.1:8083:80 --name nginx.3 nginx
a20b18b97fa4bf7576f6a0d769394a390cfb64ad5fc895453b548bf3f75105ca
[root@izoq008ryseuupz ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a20b18b97fa4 nginx "/docker-entrypoint.…" 4 seconds ago Up 4 seconds 127.0.0.1:8083->80/tcp nginx.3

You can also view the port mapping information through docker inspect nginx.3 command.

      "Ports": {
        "80/tcp": [
          {
            "HostIp": "127.0.0.1",
            "HostPort": "8083"
          }
        ]
      }

The following command cannot start the container because it uses 0.0.0.0 by default, which is why the error message is given - address already in use .

docker run -itd -p 8083:80 --name nginx.4 nginx
[root@izoq008ryseuupz ~]# docker run -itd -p 8083:80 --name nginx.4 nginx
a5211ec678440044945e4d6f16fe59f3ac10367192262768096e7b748ebd027c
docker: Error response from daemon: driver failed programming external connectivity on endpoint nginx.4 (809bed1de2bb89f5d54b5b200503701279a153e60dac08b93e13af8c40a02e36): Error starting userland proxy: listen tcp 0.0.0.0:8083: bind: address already in use.

The following command can start the container.

docker run -itd -p 127.0.0.2:8083:80 --name nginx.5 nginx
[root@izoq008ryseuupz ~]# docker run -itd -p 127.0.0.2:8083:80 --name nginx.5 nginx
f60dcb586968471e7a8127ccef4cb4e89c4198df748da404f78afaff4afa42de

Through the docker ps -a command, you can see some information about all containers.

[root@izoq008ryseuupz ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f60dcb586968 nginx "/docker-entrypoint.…" 19 seconds ago Up 18 seconds 127.0.0.2:8083->80/tcp nginx.5
a5211ec67844 nginx "/docker-entrypoint.…" 2 minutes ago Created nginx.4
a20b18b97fa4 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 127.0.0.1:8083->80/tcp nginx.3
0e62792a1945 nginx "/docker-entrypoint.…" 29 minutes ago Exited (0) 7 minutes ago nginx.2
91e3c7ed957f nginx "/docker-entrypoint.…" About an hour ago Up 58 minutes 0.0.0.0:32775->80/tcp nginx.1

Random port mapping for a specified address

You can use the format of ip::containerPort to map a random port to a specified address, such as a random port on 127.0.0.1 .

[root@izoq008ryseuupz ~]# docker run -itd -p 127.0.0.1::80 --name nginx.6 nginx
396b7d9e64c047187c0391485ba1d275587d86409573e882bc68a9357dfcb6f2
[root@izoq008ryseuupz ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
396b7d9e64c0 nginx "/docker-entrypoint.…" 9 seconds ago Up 9 seconds 127.0.0.1:32768->80/tcp nginx.6

You can see that Docker maps port 32768 of 127.0.0.1 to port 80 of the container, and this port 32768 is randomly assigned by Docker.

Multiple port mappings

Use multiple -p options to map multiple ports.

[root@izoq008ryseuupz ~]# docker run -itd -p 9091:9091 -p 9092:8080 -p 9093:8083 --name nginx.8 nginx
5ac6438dcc2a2d1b7f668eb10d8285085c396a44f896aae0b3dc09935e4e95eb
[root@izoq008ryseuupz ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ac6438dcc2a nginx "/docker-entrypoint.…" 6 seconds ago Up 4 seconds 80/tcp, 0.0.0.0:9091->9091/tcp, 0.0.0.0:9092->8080/tcp, 0.0.0.0:9093->8083/tcp nginx.8

View port mapping configuration

docker port command can view the port mapping configuration of the container.

[root@izoq008ryseuupz ~]# docker port nginx.6 80
127.0.0.1:32768
[root@izoq008ryseuupz ~]# docker port nginx.5 80
127.0.0.2:8083
[root@izoq008ryseuupz ~]# docker port nginx.3
80/tcp -> 127.0.0.1:8083
[root@izoq008ryseuupz ~]# docker port nginx.8
8080/tcp -> 0.0.0.0:9092
8083/tcp -> 0.0.0.0:9093
9091/tcp -> 0.0.0.0:9091

This is the end of the introduction to port mapping for Docker containers.

This is the end of this article about the configuration of Docker's primary network port mapping. For more relevant Docker port mapping content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker port mapping and external inaccessibility issues
  • How to achieve network access with Docker port mapping
  • Docker enables multiple port mapping commands
  • Docker volumes file mapping method
  • Docker file storage path, modify port mapping operation mode
  • How to modify the port mapping of a running Docker container
  • Docker sets up port mapping, but cannot access the solution

<<:  A line of CSS code that crashes Chrome

>>:  JS realizes the effect of Baidu News navigation bar

Recommend

Definition and function of zoom:1 attribute in CSS

Today I was asked what the zoom attribute in CSS ...

Summary of 10 must-see JavaScript interview questions (recommended)

1.This points to 1. Who calls whom? example: func...

A simple way to implement all functions of shopping cart in Vue

The main functions are as follows: Add product in...

Tips for importing csv, excel or sql files into MySQL

1. Import csv file Use the following command: 1.m...

Analysis and Solution of ERROR:2002 Reported When MySQL Starts

Preface This article mainly introduces the analys...

The difference between absolute path and relative path in web page creation

1. Absolute path First of all, on the local compu...

MySQL uses SQL statements to modify table names

In MySQL, you can use the SQL statement rename ta...

What to do if you forget your password in MySQL 5.7.17

1. Add skip-grant-tables to the my.ini file and r...

NodeJS realizes image text segmentation

This article shares the specific code of NodeJS t...

How to use JavaScript to implement sorting algorithms

Table of contents Bubble Sort Selection Sort Inse...

HTML basics - CSS style sheets, style attributes, format and layout details

1. position : fixed Locked position (relative to ...

mysql 8.0.19 win10 quick installation tutorial

This tutorial shares the installation tutorial of...

Display special symbols in HTML (with special character correspondence table)

Problem Reproduction When using HTML for editing,...