In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to realize the interconnection of Docker containers". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Mount a host directory as a data volume
Using the-v flag, you can also specify that the directory of a local host be mounted to the container.
$sudo docker run-d-P-name web-v / src/webapp:/opt/webapp training/webapp python app.py
The above command loads the host's / src/webapp directory into the container's / opt/webapp directory. This feature is very convenient when testing, for example, users can place some programs in the local directory to see if the container is working properly. The path to the local directory must be absolute. If the directory does not exist, Docker will automatically create it for you.
* Note: this usage is not supported in Dockerfile because Dockerfile is for migration and sharing. However, different operating systems have different path formats, so they cannot be supported at this time.
The default permission for Docker to mount data volumes is read and write, and users can also specify read-only through: ro.
$sudo docker run-d-P-name web-v / src/webapp:/opt/webapp:rotraining/webapp python app.py
After adding ro, it will be mounted as read-only.
two。 Data volume container
If you have some continuously updated data to share between containers, it is best to create a data volume container.
A data volume container is actually a normal container designed to provide data volumes for other containers to mount.
First, create a named data volume container dbdata:
$sudo docker run-d-v / dbdata-- name dbdata training/postgres echo Data-only container for postgres
Then, use-- volumes-from in other containers to mount the data volumes in the dbdata container.
$sudo docker run-d-volumes-from dbdata-name db1 training/postgres$ sudo docker run-d-volumes-from dbdata-name db2 training/postgres
You can also use multiple-- volumes-from parameters to mount multiple volumes from multiple containers. You can also mount data volumes from other containers that already have container volumes mounted.
$sudo docker run-d-name db3-volumes-from db1 training/postgres
* Note: the container of the data volume mounted with the-- volumes-from parameter does not need to remain running on its own.
If you delete mounted containers (including dbdata, db1, and db2), the data volume is not automatically deleted. If you want to delete a data volume, you must use the docker rm-v command to delete the associated container when you delete the last container that still holds it. This allows users to upgrade and move data volumes between containers.
3. External access container
Some network applications can be run in the container, and to make them accessible externally, you can specify the port mapping with the-P or-p parameter.
When using the-P flag, Docker randomly maps a port of 49000 to 49900 to a network port open in the internal container.
Using docker ps, you can see that 49155 of the local host is mapped to port 5000 of the container. At this point, you can access the interface provided by the web application in the container by accessing port 49115 of this machine.
$sudo docker run-d-P training/webapp python app.py$ sudo docker ps-lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESbc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0purl 49155-> 5000/tcp nostalgic_morse
Similarly, you can view the information of the application through the docker logs command.
$sudo docker logs-f nostalgic_morse* Running on http://0.0.0.0:5000/10.0.2.2-- [23/May/2014 20:16:31] "GET / HTTP/1.1" 200-10.0.2.2-- [23/May/2014 20:16:31] "GET / favicon.ico HTTP/1.1" 404-
-p (lowercase) can specify the port to be mapped, and only one container can be bound on a specified port. The supported formats are ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort.
Map all interface addresses
Using the local port 5000 in hostPort:containerPort format to the port 5000 of the container, you can execute the
$sudo docker run-d-p 5000UR 5000 training/webapp python app.py
All addresses on all local interfaces are bound by default.
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, such as the localhost address 127.0.0.1
$sudo docker run-d-p 127.0.0.1 purl 5000 training/webapp python app.py
Map to any port at the specified address
Use ip::containerPort to bind any port of localhost to port 5000 of the container, and the local host automatically assigns a port.
$sudo docker run-d-p 127.0.0.1 Virgo 5000 training/webapp python app.py
You can also use udp tags to specify udp ports
$sudo docker run-d-p 127.0.0.1:5000:5000/udp training/webapp python app.py
View mapped port configuration
Use docker port to view the port configuration of the current mapping, and you can also see the bound address
$docker port nostalgic_morse 5000127.0.0.1 purl 49155.
Note:
The container has its own internal network and ip address (all variables can be obtained using docker inspect, and Docker can have a variable network configuration. )
The-p flag can be used multiple times to bind multiple ports
For example
$sudo docker run-d-p 5000 5000-p 300015 80 training/webapp python app.py4. Container interconnection
Use the-- link parameter to allow containers to interact securely.
Let's first create a new database container.
$sudo docker run-d-name db training/postgres
Delete the previously created web container
$docker rm-f web
Then create a new web container and connect it to the db container
$sudo docker run-d-P-name web-- link db:db training/webapp python app.py
At this point, the db container and the web container establish an interconnection.
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 for this connection.
Use docker ps to view the connection of the container
$docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES349169744e49 training/postgres:latest su postgres-c'/ usr About a minute ago Up About a minute 5432/tcp db, web/dbaed84ee21bde training/webapp:latest python app.py 16 hours ago Up 2 minutes 0.0.0.0purl 49154-> 5000/tcp web
You can see the custom named containers, and the names columns for db and web,db containers include db and web/db. This means that the web container is linked to the db container, and the web container will be allowed to access the db container's information.
Docker creates a secure tunnel between two interconnected containers without mapping their ports to the host host. The-p and-P flags are not used when starting the db container, thus avoiding exposing the database port to the external network.
Docker exposes connection information for containers in two ways:
Environment variable
Update / etc/hosts file
Use the env command to view the environment variables of the web container
Sudo docker run-- rm-- name web2-- link db:db training/webapp env. . .DB _ NAME=/web2/dbDB_PORT=tcp://172.17.0.5:5432DB_PORT_5000_TCP=tcp://172.17.0.5:5432DB_PORT_5000_TCP_PROTO=tcpDB_PORT_5000_TCP_PORT=5432DB_PORT_5000_TCP_ADDR=172.17.0.5. . .
The environment variable that begins with DB_ is used by the web container to connect to the db container with an uppercase connection alias prefix.
In addition to environment variables, Docker also adds host information to the / etc/hosts file of the parent container. The following is the hosts file of the parent container web
Sudo docker run-t-I-- rm-- link db:db training/webapp / bin/bashroot@aed84ee21bde:/opt/webapp# cat / etc/hosts172.17.0.7 aed84ee21bde. . .172.17.0.5 db
There are two hosts, the first is the web container, the web container uses id as its hostname, and the second is the ip and hostname of the db container. You can install the ping command in the web container to test connectivity to the db container.
Root@aed84ee21bde:/opt/webapp# apt-get install-yqq inetutils-pingroot@aed84ee21bde:/opt/webapp# ping dbPING db (172.17.0.5): 48 data bytes56 bytes from 172.17.0.5: icmp_seq=0 ttl=64 time=0.267 ms56 bytes from 172.17.0.5: icmp_seq=1 ttl=64 time=0.250 ms56 bytes from 172.17.0.5: icmp_seq=2 ttl=64 time=0.256 ms
Test the db container with ping, and it parses to 172.17.0.5. * Note: the official ubuntu image does not have ping installed by default. You need to install it yourself.
Users can link multiple child containers to parent containers, for example, they can link multiple web to db containers.
This is the end of the content of "how to interconnect Docker containers". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.