In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to make multiple container coordinate work in Docker". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to make multiple container coordinate work in Docker"!
Connect the container to work together
In the chapter on how to use docker, we connect to a service running in docker over the Internet. This is a way to interact with servie in docker. (the idea of docker is a service and a container, so link is very important.) in this chapter we will introduce some advanced ways to connect the various services of docker.
Network port mapping refresher
Network interface mapping
In the chapter on how to use docker, we created a container that runs Flask applications.
$sudo docker run-d-P training/webapp python app.py
Note each container has its own network and IP address. (recall how we used docker inspect to display the IP address of container in the chapter on how to use docker.) Docker can have a variety of network configurations, see Docker's network for details.
When we use-P when creating a container, the Port exposed inside the docker is automatically assigned to a random Port of the host, ranging from 49000 to 49900. Using the docker ps command after creation, we can see that port 5000 inside container is tied to 49155 of the host.
$sudo docker ps nostalgic_morseCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESbc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0 seconds 49155-> 5000/tcp nostalgic_morse
We can also use-p to bind the IP inside container to a host port that you define.
$sudo docker run-d-p 5000UR 5000 training/webapp python app.py
This approach is not good because a host port can only be bound by a port of one container. Translator: I don't seem to see anything better than this. It seems that what I want to say is that port belongs to IP. If you add IP, you can get a few more. See the following paragraph)
There are other ways to use-p. By default,-p binds the Port of container to the port number of all ip on the host. (if your host has multiple IP, the port of each IP will be tied to the port of the container.) We can specify the interface, for example, the port is only bound to the localhost.
$sudo docker run-d-p 127.0.0.1 purl 5000 training/webapp python app.py
This will bind port 5000 inside container to port 5000 of host 127.0.0.1.
If only a network interface is specified and no port number is specified, the port of container will be tied to a random port on that network interface.
$sudo docker run-d-p 127.0.0.1 Virgo 5000 training/webapp python app.py
We can also use UDP
$sudo docker run-d-p 127.0.0.1:5000:5000/udp training/webapp python app.py
Using docker port, you can view the binding of the current container port and the configuration of special ports. For example, if we bind the port of container to the localhost of the host, the output when we enter docker port will be:
$docker port nostalgic_morse127.0.0.1:49155
Note-p can be used many times to bind port in multiple container to the host machine.
Docker Container Linking
Mapping network ports is not the only way for container to connect to each other. Docker's linking system allows you to connect multiple container and let them interact with each other. Docker's linking creates a parent-child relationship. The parent container can see the information provided by his son container.
Network port mappings are not the only way Docker containers can connect to one another. Docker also has a linking system that allows you to link multiple containers together and share connection information between them. Docker linking will create a parent child relationship where the parent container can see selected information about its child.
Container naming
Docker's linking system relies on the name container. We have noticed that each container is automatically assigned a name, and in this tutorial you may be familiar with the name nostalgic_morse (translator: docker automatically assigned names are real words, some names are more interesting, for example, one of my container is called tender einstein gentle Einstein, for most cases These names are definitely an opportunity to test your English vocabulary, such as backstabbing nobel, stoic carson). We can name container ourselves. Naming serves two purposes:
Easy to remember, such as naming a container hosting a web service as web
Container can refer to each other, for example, one web container uses one db container
You can name container with-- name, for example:
$sudo docker run-d-P-- name web training/webapp python app.py
You can see that we launched a new container, using the name to name the container web. We can check the name of container by commanding docker ps
$sudo docker ps-lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaed84ee21bde training/webapp:latest python app.py 12 hours ago Up 2 seconds 0.0.0.0 hours ago Up 49154-> 5000/tcp web
We can also use docker inspect to check the name of container.
$sudo docker inspect-f "{{.Name}}" aed84ee21bde/web
(translator: the above good grammar comes from the template language that comes with golang.)
Note the name of Container must be unique. That means you can only name one container web. When you want to reuse a container name, you must delete the container with that name before you can use it again. To delete container, you can use docker rm. Another convenient way is to use the-rm flag when starting container. In this way, container will be deleted automatically when it is stopped. (translator: if you have used docker for a period of time, using docker ps-a you may find a lot of stopped container. In many docker tutorials, you will start container with-rm.)
Container Linking
Links runs container to find each other and communicate securely with each other. You can create a link using-- link. Let's create a container that runs the database.
$sudo docker run-d-name db training/postgres
Here we create a container called db based on training/postgres image, which provides PostgreSQL database services.
Now let's create a container called web and connect it to db container.
$sudo docker run-d-P-name web-- link db:db training/webapp python app.py
This command connects db to web-the use of link:
-- link name:alias
Here name is the name of the container we want to connect to, and alias is an alias for one link. We will see how to use this alias below.
Let's use docker ps to look at the container connected together.
$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 dbaed84ee21bde training/webapp:latest python app.py 16 hours ago Up 2 minutes 0.0.0.0 hours ago Up 49154-> 5000/tcp db/web,web
Here we can see the two container we created called db and web, and notice that web container shows another name db/web in the name column. This name tells us that web container is connected to db container and establishes a father-son relationship.
What on earth is the use of linking? We have seen that link creates a parent-son relationship between two container. The db in the case of the parent container can get the information on his child container web. Docker is achieved by establishing a secure channel between the two container so that the container does not have to expose the port. You may have noticed that we didn't use-p or-P when we started db container. Because we have connected the two container through link, there is no need to expose the database service through the port.
Docker exposes the information in the child container to the parent container in two ways:
Environment variable
Update / etc/host file
Let's first take a look at the environment variables set by docker. In web containre, let's run the env command to list all the environment variables.
Root@aed84ee21bde:/opt/webapp# envHOSTNAME=aed84ee21bde. . .DB _ NAME=/web/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. . .
Note: these environment variables are only set for the first process in container. Similar daemons, such as sshd, erase these variables when a child shell is created.
We can see that Docker has created a list of environment variables that are useful for us to use db. Each variable begins with DB, and this DB is the alias above. (translator: why use aliases? it's clear here that image doesn't need to know the names of other container, just map the names to what they know when run.) if the alias we give is db1, then the prefix of the name of the environment variable is DB1_.. You can use these environment variables to configure your application to connect to the database in db container. The connection is secure and private only between web and db.
In addition to the environment variables, Docker adds the IP of the parent container to the / etc/hosts of the child container. Let's take a look at the hosts file in web container:
Root@aed84ee21bde:/opt/webapp# cat / etc/hosts172.17.0.7 aed84ee21bde. . .172.17.0.5 db
We can see that there are two related host configuration items. The first one is for web container, whose name is container's ID. Second, the alias of the parent container is tied to the IP of the parent container. Let's try to ping by the name of host.
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
Note the first command above is used to install ping because the container we are using is not installed.
We can use this host to connect the web service to the database service.
Note
A parent container can connect multiple child container. For example, we can connect the container of multiple web services to a database container
Next step
Now that we know how to combine multiple container to work together in Docker, we will explain how to manage data and volumne in docker.
Jump to how to manage container data.
At this point, I believe you have a deeper understanding of "how to make multiple container coordinate work in Docker". 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.
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.