Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Docker Port Mapping and Container Interconnection

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how to achieve Docker port mapping and container interconnection". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to interconnect Docker port mapping and containers".

1. Port mapping enables access to the container

1. Access container applications from the outside

When starting the container, if you do not specify the corresponding parameters, the network applications and services inside the container cannot be accessed through the network outside the container.

When some network applications are running in the container, you can specify the port mapping with the-p or-p parameter when you want to allow external access to these applications. When using the-p (uppercase p) flag, docker randomly maps a port to a network port open in the internal container (the port range is outside the ports used by the linux system, usually more than 10,000):

[root@docker ~] # docker run-d-name nginx_1-p nginx:latest f769af3e98478b27b87e008f3ad785e2055da4047442c4a8dcb8f621f810dbea [root@docker ~] # docker pscontainer id image command created status ports namesf769af3e9847 nginx:latest "nginx-g 'daemon..." 3 seconds ago up 2 seconds 0.0.0.0 nginx:latest f769af3e98478b27b87e008f3ad785e2055da4047442c4a8dcb8f621f810dbea 32768-> 80/tcp nginx_1 [root@docker ~] #

Through docker ps, you can see that port 80 of the nginx_1 container is mapped to port 32768 on the native machine. You can access the web interface provided by the application in the container by accessing port 32768 of the host host:

Similarly, you can view the application information through the docker logs command:

[root@docker ~] # docker logs nginx_110.0.0.253-- [29/nov/2017:06:25:38 + 0000] "get / http/1.1" 200612 "-" mozilla/5.0 (windows nt 6.1; win64; x64) Rv:57.0) gecko/20100101 firefox/57.0 ""-"10.0.0.253-- [29/nov/2017:06:25:39 + 0000]" get / favicon.ico http/1.1 "404 169"-"" mozilla/5.0 (windows nt 6.1; win64; x64 Rv:57.0) gecko/20100101 firefox/57.0 ""-"06:25:39 on 2017-11-29 [error] 5: * 1 open ()" / usr/share/nginx/html/favicon.ico "failed (2: no such file or directory), client: 10.0.0.253, server: localhost, request:" get / favicon.ico http/1.1 " Host: "10.0.0.31 get 32768" 10.0.0.253-- [29/nov/2017:06:25:39 + 0000] "get / favicon.ico http/1.1" 404 169 "-" mozilla/5.0 (windows nt 6.1) Win64; x64 Rv:57.0) gecko/20100101 firefox/57.0 ""-"06:25:39 on 2017-11-29 [error] 5: * 1 open ()" / usr/share/nginx/html/favicon.ico "failed (2: no such file or directory), client: 10.0.0.253, server: localhost, request:" get / favicon.ico http/1.1 " Host: "10.0.0.31 get 32768" 10.0.0.253-- [29/nov/2017:06:26:55 + 0000] "get / http/1.1" 200612 "-" mozilla/5.0 (windows nt 6.1) Wow64) applewebkit/537.36 (khtml, like gecko) chrome/61.0.3163.100 safari/537.36 "-" 10.0.0.253-- [29/nov/2017:06:26:56 + 0000] "get / favicon.ico http/1.1" 404 571 "http://10.0.0.31:32768/"" mozilla/5.0 (windows nt 6.1) Wow64) applewebkit/537.36 (khtml, like gecko) chrome/61.0.3163.100 safari/537.36 ""-"06:26:56 on 2017-11-29 [error] 5: * 2 open ()" / usr/share/nginx/html/favicon.ico "failed (2: no such file or directory), client: 10.0.0.253, server: localhost, request:" get / favicon.ico http/1.1 ", host:" 10.0.0.3132768 " Referrer: "http://10.0.0.31:32768/"[root@docker ~] #

-p (lowercase p) can specify the port to be mapped, and only one container can be bound on a specified port. Supported formats are: ip:hostport:containerport | ip::containerport | hostport:containerport.

two。 Map all interface addresses

Use hostport:containerport format to map the local port 5000 to the container port 5000:

[root@docker] # docker run-itd-p 5000 name nginx_2 nginx:latest 5bdca2bde33d7db72861399ca49e82f0d209d13289d20b181843da5b10e6f2d3 [root@docker] # docker pscontainer id image command created status ports names5bdca2bde33d nginx:latest "nginx-g 'daemon..." 8 seconds ago up 7 seconds 80/tcp 0.0.0.0daemon 5000-> 5000/tcp nginx_2f769af3e9847 nginx:latest "nginx-g'daemon..." 15 minutes ago up 15 minutes 0.0.0.0 minutes 32768-> 80/tcp nginx_1 [root@docker ~] #

All addresses on all local interfaces are bound by default. You can bind multiple ports by using the-p parameter multiple times:

[root@docker] # docker run-itd-p 3000 itd 2700-p 2389 root@docker 8863-- name nginx_3 nginx:latest 65fbfbe9761eb5146501311016d681f210b1891ca5f5af62dc978ad6f2a22750 [root@docker ~] # docker pscontainer id image command created status ports names65fbfbe9761e nginx:latest "nginx-g 'daemon..." 3 seconds ago up 2 seconds 80/tcp, 0.0.0.0 itd 3000-> 2700/tcp 0.0.0.0daemon 2389-> 8863/tcp nginx_35bdca2bde33d nginx:latest "nginx-g'daemon..." 2 minutes ago up 2 minutes 80/tcp, 0.0.0.0daemon 5000-> 5000/tcp nginx_2f769af3e9847 nginx:latest "nginx-g 'daemon..." 18 minutes ago up 18 minutes 0.0.0.0daemon 32768-> 80/tcp nginx_1 [root@docker ~] #

3. Map to the specified port of the specified address

You can use the ip:hostport:containerport format to specify that the mapping uses a specific address:

[root@docker] # docker run-itd-p 10.0.0.31 name nginx_4 nginx:latest 16a476837222d413926053e1c8175c993b0495732073fbc6251dfd4696db8242 [root@docker] # docker pscontainer id image command created status ports names16a476837222 nginx:latest "nginx-g 'daemon..." 4 seconds ago up 3 seconds 80/tcp 10.0.31 nginx 89-> 8081/tcp nginx_465fbfbe9761e nginx:latest "nginx-g'daemon..." 2 minutes ago up 2 minutes 80/tcp, 0.0.0.0 daemon 3000-> 2700/tcp, 0.0.0.0 daemon 2389-> 8863/tcp nginx_35bdca2bde33d nginx:latest "nginx-g 'daemon..." 5 minutes ago up 5 minutes 80/tcp 0.0.0.0daemon 5000-> 5000/tcp nginx_2f769af3e9847 nginx:latest "nginx-g'daemon..." 20 minutes ago up 20 minutes 0.0.0.0 minutes 32768-> 80/tcp nginx_1 [root@docker ~] #

4. Map to any port at the specified address

Bind any port of the local machine to the specified port of the container using ip::containerport format:

[root@docker] # docker run-itd-p 10.0.0.31 name nginx_5 nginx:latest 3436fd5fbdca6529c70c664f42edfd10d51edb0fb541b096b47c9b168887b2ca [root@docker] # docker pscontainer id image command created status ports names3436fd5fbdca nginx:latest "nginx-g 'daemon..." 2 seconds ago up 2 seconds 80/tcp 10.0.0.31minutes 80/tcp 32769-> 8082/tcp nginx_516a476837222 nginx:latest "nginx-g'daemon..." 2 minutes ago up 2 minutes 80/tcp, 10.0.0.31daemon 89-> 8081/tcp nginx_465fbfbe9761e nginx:latest "nginx-g 'daemon..." 4 minutes ago up 4 minutes 80/tcp, 0.0.0.0 daemon 3000-> 2700/tcp 0.0.0.0daemon 2389-> 8863/tcp nginx_35bdca2bde33d nginx:latest "nginx-g'daemon..." 7 minutes ago up 7 minutes 80/tcp, 0.0.0.0daemon 5000-> 5000/tcp nginx_2f769af3e9847 nginx:latest "nginx-g 'daemon..." 22 minutes ago up 22 minutes 0.0.0.0daemon 32768-> 80/tcp nginx_1 [root@docker ~] #

After the container starts, this machine randomly automatically allocates an unoccupied port.

5. View mapped port configuration

Use the docker port command to view the currently mapped port configuration, or you can view the bound address

[root@docker] # docker port nginx_180/tcp-> 0.0.0.0root@docker 32768 [root@docker] # docker port nginx_25000/tcp-> 0.0.0.0Vl5000 [root@docker] # docker port nginx_32700/tcp-> 0.0.0.0:30008863/tcp-> 0.0.0.0root@docker 2389 [root@docker] # docker port nginx_48081/tcp-> 10.0.0.31VR89 [root@docker] # Docker port nginx_58082/tcp-> 10.0.0.31 32769 [root@docker ~] #

Note:

The container has its own internal network and ip address. You can get the specific information of the container by using docker inspect + container id.

2. Interconnection mechanism to achieve convenient access

Container interconnection is a way to allow applications in multiple containers to interact quickly. It establishes a connection between the source and the receiving container, and the receiving container can quickly access the source container through the container name without specifying a specific ip address.

1. Custom container naming

The connection system is performed according to the name of the container. Therefore, the first thing to do is to define a simple and memorable name for the container. When a container is created, the system will randomly create a container name, but it has no special meaning and is not easy to remember. All custom container names have the following advantages:

1. Custom naming is easy to remember. For example, a web application container can be named web, which is convenient to remember and understand the function of the container.

two。 When you want to connect to other containers, even if you restart, you can use a custom container name, such as the web container, to connect to the db container.

Use the-- name parameter to customize the naming of the container:

[root@docker ~] # docker run-itd-- name centos_1 centos:latest 4d58a9f6f324185caf53dbe5eaee85f35e853842ffb037cf272c2a92cee89716 [root@docker ~] # docker pscontainer id image command created status ports names4d58a9f6f324 centos:latest "/ bin/bash" 6 seconds ago up 5 seconds centos_13436fd5fbdca nginx:latest "nginx-g'daemon..." 11 minutes ago up 11 minutes 80/tcp 10.0.0.31daemon 32769-> 8082/tcp nginx_516a476837222 nginx:latest "nginx-g'daemon..." 13 minutes ago up 13 minutes 80/tcp, 10.0.0.31daemon 89-> 8081/tcp nginx_465fbfbe9761e nginx:latest "nginx-g 'daemon..." 15 minutes ago up 15 minutes 80/tcp, 0.0.0.0 daemon 3000-> 2700/tcp 0.0.0.0daemon 2389-> 8863/tcp nginx_35bdca2bde33d nginx:latest "nginx-g'daemon..." 18 minutes ago up 18 minutes 80/tcp, 0.0.0.0daemon 5000-> 5000/tcp nginx_2f769af3e9847 nginx:latest "nginx-g 'daemon..." 34 minutes ago up 34 minutes 0.0.0.0daemon 32768-> 80/tcp nginx_1 [root@docker ~] #

The custom name of the container can be found through docker ps or docker ps-a, and the custom name of the container can also be obtained by using docker inspect:

[root@docker ~] # docker inspect-f "{{.name}}" 4d58a9f6f324/centos_1 [root@docker ~] #

Note:

The name of the container is unique. If you have already named a web container, you will get an error when you use the name web again. If you must use it, you need to delete the previously created web container with docker rm first.

If the-- rm parameter is added when docker run is executed, the container will be deleted immediately after termination. The rm parameter and the-d parameter cannot be used at the same time.

2. Container interconnection

Use the-- link parameter to allow containers to interact securely.

Create a database container:

[root@docker] # docker run-itd-- name db-- env mysql_root_password=example mariadbb239b124946c99b7da63e00c22df802e9612fbe8bc636389205baf6c2f6963bd [root@docker ~] # docker pscontainer id image command created status ports namesb239b124946c mariadb "docker-entrypoint..." 3 seconds ago up 2 seconds 3306/tcp db [root@docker ~] #

Create a web container and connect it to the db container:

[root@docker] # docker run-itd-p-- name web-- link db:db nginx:latest 42fa6662784010368b5e615d495e71920d85cc1bc089a5d181657514973ee90a [root@docker] # docker pscontainer id image command created status ports names86ef0f632ffe nginx:latest "nginx-g 'daemon..." 44 seconds ago up 43 seconds 80/tcp webb239b124946c mariadb "docker-entrypoint..." about a minute ago up 59 seconds 3306/tcp db [root@docker ~] #

At this point, the web container has established an interconnection with the db container:-- the format of the link parameter is:-- link name:alias, where name is the name of the container to be connected, and alias is the alias for this connection.

Docker is equivalent to creating a virtual channel between two interconnected containers without mapping their ports to the host. The-p or-p parameters are not used when starting the db container, thus avoiding exposing the database service port to the external network.

Docker exposes connection information for containers in two ways:

1. Update environment variables

two。 Update the / etc/hosts file.

Use the env command to view the environment variables of the web container:

[root@docker] # docker run-rm-name web3-link db:db nginx:latest envpath=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binhostname=7258de738125db_port=tcp://172.17.0.2:3306db_port_3306_tcp=tcp://172.17.0.2:3306db_port_3306_tcp_addr=172.17.0.2db_port_3306_tcp_port=3306db_port_3306_tcp_proto=tcpdb_name=/ Web3/dbdb_env_mysql_root_password=exampledb_env_gosu_version=1.10db_env_gpg_keys=199369e5404bd5fc7d2fe43bcbcb082a1bb943db 430bdf5c56e7c94e848ee60c1c4cbdcdcd2efd2a 4d1bb29d63d98e422b2113b19334a25f8507efa5db_env_mariadb_major=10.2db_env_mariadb_version=10.2.11+maria~jessienginx_version=1.13.7-1~stretchnjs_version=1.13.7.0.1.15-1~stretchhome=/ root [root @ docker ~] #

The environment variable that begins with db_ is used by the web container to connect to the db container, prefixed with an uppercase connection alias.

In addition to the environment variables, docker adds host information to the / etc/hosts file of the parent container.

[root@docker] # docker run-it-- rm-- link db:db nginx:latest / bin/bashroot@16b8e6fde27f:/# cat / etc/hosts 172.17.0.2 db b239b124946c172.17.0.5 16b8e6fde27froot@16b8e6fde27f:/#

There are two hosts messages, the first is the ip of the db container and the container name + container id, the second is the web's own ip and the hosts file in the container id,web container uses the container's id as the hostname. Ping can be communicated between interconnected containers.

At this point, I believe you have a deeper understanding of "Docker port mapping and container interconnection". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report