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

Mutual access between docker Port Mapping and Container

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Port mapping is associated with a container

In addition to network access, docker also provides two functions to meet the basic needs of service access: one is to map the service ports applied in the container to the local host, and the other is the interconnection mechanism to quickly access multiple containers through container names.

Port Mapping Container access

1. Access the container application from the outside. If you do not specify the corresponding parameters when starting the container, the network applications and services in the container cannot be accessed through the network outside the container. You can specify the port mapping through the-P or-p parameter.

-P: will randomly map a port of 49000 to 49900 to a network port open in the internal container

[root@docker01] # docker run-d-P training/webapp python app.py75b2c32292d290aa3004b5b6cbb49775cc6f360796736473978cc32a682fdad9 [root@docker01 ~] # docker ps-lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES75b2c32292d2 training/webapp "python app.py" 3 seconds ago Up 2 seconds 0.0.0.0 training/webapp python app.py75b2c32292d290aa3004b5b6cbb49775cc6f360796736473978cc32a682fdad9 32770-> 5000/tcp musing_carson

View the information of the application

[root@docker01] # docker logs-f musing_carson * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

2. Map all interface addresses and use HostPort:ContainerPort to map the local port 5000 to the container port 5000

[root@docker01] # docker run-d-p 5000 training/webapp python app.py6368e0f567714ad90d2b20ff71afb2231e404b35639055a665cc3715a85f010a

Or bind multiple ports with consecutive-p

[root@docker01] # docker run-d-p 5000-p3000 training/webapp python app.py80bfe8e1f7fd53099e3f350c5efd1160a7247d065233965cc2bc2130e2fdcbb2 80 training/webapp python app.py80bfe8e1f7fd53099e3f350c5efd1160a7247d065233965cc2bc2130e2fdcbb2

3. Map to the specified port of the specified address, and use the IP:HostPort:ContainerPort format to specify that the mapping uses a specific address.

[root@docker01] # docker run-d-p 10.10.17.1995000 training/webapp python app.pyac228c0f2334a59856d9993e6a3eb652b50eb08ec41cedff87b331aa8d5d5f86 [root@docker01] # docker ps-lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESac228c0f2334 training/webapp "python app.py" 5 seconds ago Up 5 seconds 10.10 .17.199VOU 5000-> 5000/tcp eager_noether

4. Map to any port of the specified address, use IP::ContainerPort to bind any port of localhost to port 5000 of the container, and the local host will automatically assign a port.

[root@docker01] # docker run-d-p 127.0.0.1 training/webapp python app.pye72dec38ae77c113418156c4ad50b601fe31d0a38b4b8f995aabdcfcc510fdcc

Use UDP tags to specify udp ports

[root@docker01] # docker run-d-p 127.0.0.1:5001:5001/udp training/webapp python app.py581ff1531c6b604c1b66ece82c87529a02c8da0b7b19e98bd336122c151b524f

5. View the mapped port configuration and use docker port to view the mapped port configuration

[root@docker01 ~] # docker port laughing_faraday 5000127.0.0.1 interconnection mechanism 32768 convenient exchange of visits

Container interconnection is a way to allow applications in multiple containers to interact quickly. It creates 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, and the connection system is executed according to the name of the container. First of all, you need a container name that is easy to remember. Although it is not specified, the system will assign one by default, but the advantage of the assignment is that the name is easy to remember and is named based on the application, such as web. When you need to connect to other containers, you can also use the container name without changing it.

[root@docker01] # docker run-d-P-- name web training/webapp python app.py6ae6d11614f99762a519d55beb8b38f3fbff667af34be90b33d6a11477b8d99a [root@docker01 ~] # docker ps-lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES6ae6d11614f9 training/webapp "python app.py" 7 seconds ago Up 6 seconds 0.0.0.0 name web training/webapp python app.py6ae6d11614f99762a519d55beb8b38f3fbff667af34be90b33d6a11477b8d99a 32771-> 5000/tcp web

2. Containers are interconnected. Use the-- link parameter to allow containers to interact safely.

Create a new database container

[root@docker01] # docker run-d-- name db training/postgresUnable to find image 'training/postgres:latest' locallylatest: Pulling from training/postgresImage docker.io/training/postgres:latest uses outdated schema1 manifest format. Please upgrade to atibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/a3ed95caeb02: Pull complete 6e71c809542e: Pull complete 2978d9af87ba: Pull complete e1bca35b062f: Pull complete 500b6decf741: Pull complete 74b14ef2151f: Pull complete 7afd5ed3826e: Pull complete 3c69bb244f5e: Pull complete d86f9ec5aedf: Pull complete 010fabf20157: Pull complete Digest: sha256:a945dc6dcfbc8d009c3d972931608344b76c2870ce796da00a827bd50791907eStatus: Downloaded newer image for training/postgres:latest67b752f9c5d6f19f2811674be780673b5475c845be6f43d9b15df139ff01c0fd

Delete the previous web container, then create a new web container and connect it to the db container

[root@docker01] # docker rm-f webweb [root@docker01] # docker run-d-P-- name web-- link db:db training/webapp python app.pyde3f340d554dd01789859d6dc0fc3a0bb406e3111ca60e27f78da208def08dca

-- the format of the link parameter is-- link name:alias, where name is the name of the container to be linked and alias is the alias.

Use docker ps to view links to containers

[root@docker01 ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESde3f340d554d training/webapp "python app.py" 4 minutes ago Up 4 minutes 0.0.0.0 minutes ago Up 32772-> 5000/tcp web67b752f9c5d6 training/postgres "su postgres-c'/ us …" 13 minutes ago Up 13 minutes 5432/tcp db

It is equivalent to creating a virtual channel between the two containers, without mapping their ports to the host, and without using the-p or-P flag when starting the db container, avoiding exposing the database server to the external network.

Docker exposes connection information for containers in two ways

# Update environment variables

# Update / etc/hosts file

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

[root@docker01] # docker run-rm-name web2-link db:db training/webapp envPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOSTNAME=2ff63f7eb1d2DB_PORT=tcp://172.17.0.3:5432DB_PORT_5432_TCP=tcp://172.17.0.3:5432DB_PORT_5432_TCP_ADDR=172.17.0.3DB_PORT_5432_TCP_PORT=5432DB_PORT_5432_TCP_PROTO=tcpDB_NAME=/ Web2/dbDB_ENV_PG_VERSION=9.3HOME=/root

In addition to the environment variable docker, host information is added to the / etc/hosts of the parent container.

[root@docker01] # docker run-t-I-rm-- link db:db training/webapp / bin/bashroot@8ed896541b8d:/opt/webapp# cat / etc/hosts127.0.0.1 localhost::1 localhost ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allrouters172.17.0.3 db 67b752f9c5d6172.17.0.4 8ed896541b8droot@8ed896541b8d:/opt/webapp# ping dbPING db (172.17.0.3) 56 (84) bytes of data.64 bytes from db (172.17.0.3): icmp_seq=1 ttl=64 time=0.239 ms64 bytes from db (172.17.0.3): icmp_seq=2 ttl=64 time=0.154 ms64 bytes from db (172.17.0.3): icmp_seq=3 ttl=64 time=0.110 ms64 bytes from db (172.17.0.3): icmp_seq=4 ttl=64 time=0.111 Ms ^ C-db ping statistics-4 packets transmitted 4 received, 0% packet loss, time 3000msrtt min/avg/max/mdev = 0.110 ms 0.153 ms

Users can link multiple child containers to parent containers, for example, multiple web can be linked to the same db container.

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

Servers

Wechat

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

12
Report