May 22, 2021 Docker From entry to practice
A container's connection system is another way to interact with the application in the container, in addition to port mapping.
The system creates a tunnel between the source and the receiving container, which can see the information specified by the source container.
The connection system is performed according to the name of the container. Therefore, you first need to customize the naming of a well-remembered container.
Although when the container is created, a name is assigned by default. Custom named containers have two benefits:
Use
--name
to customize the name for the container.
$ sudo docker run -d -P --name web training/webapp python app.py
Use
docker ps
verify the name of the setting.
$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aed84ee21bde training/webapp:latest python app.py 12 hours ago Up 2 seconds 0.0.0.0:49154->5000/tcp web
You can also
docker inspect
see the name of the container
$ sudo docker inspect -f "{{ .Name }}" aed84ee21bde
/web
Note: The name of the container is unique.
If you have named a container called the web, when you want to use the name web again, you need
docker rm
remove the container of the same name that you created earlier.
If you add a
--rm
tag when you execute
docker run
the container is deleted as soon as it terminates.
Note that
--rm
-d
cannot be used at the same time.
Using
--link
parameter allows safe interaction between containers.
Let's start by creating a new database container.
$ sudo docker run -d --name db training/postgres
Delete the web container you created earlier
$ 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 are interconnected.
--link
the
--link name:alias
name is the
name
of the container to be linked, and
alias
of the connection.
Use
docker ps
view the connection to the container
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
349169744e49 training/postgres:latest su postgres -c '/usr About a minute ago Up About a minute 5432/tcp db, web/db
aed84ee21bde training/webapp:latest python app.py 16 hours ago Up 2 minutes 0.0.0.0:49154->5000/tcp web
You can see custom named containers, db and web, and the names columns of db containers have both db and web/db. This means that the web container is linked to the db container, and the web container will be allowed access to the information of the db container.
Docker creates a secure tunnel between two interconnected containers without mapping their ports to the host host.
The db container is not started with
-p
-P
thus avoiding exposing database ports to external networks.
Docker exposes connection information to containers in 2 ways:
/etc/hosts
file
Use
env
to view the environment variables of the web container
$ sudo docker run --rm --name web2 --link db:db training/webapp env
. . .
DB_NAME=/web2/db
DB_PORT=tcp://172.17.0.5:5432
DB_PORT_5000_TCP=tcp://172.17.0.5:5432
DB_PORT_5000_TCP_PROTO=tcp
DB_PORT_5000_TCP_PORT=5432
DB_PORT_5000_TCP_ADDR=172.17.0.5
. . .
Where DB_ at the beginning of the web container connection db container, the prefix uses a capital connection alias.
In addition to the environment variables, Docker adds host information to the
/etc/hosts
file.
Below is the hosts file for the parent container web
$ sudo docker run -t -i --rm --link db:db training/webapp /bin/bash
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
172.17.0.7 aed84ee21bde
. . .
172.17.0.5 db
There are 2 hosts, the first is the web container, which uses id as his host name, and the second is the ip and host name of the db container. You can install ping commands in a web container to test connectivity to the db container.
root@aed84ee21bde:/opt/webapp# apt-get install -yqq inetutils-ping
root@aed84ee21bde:/opt/webapp# ping db
PING db (172.17.0.5): 48 data bytes
56 bytes from 172.17.0.5: icmp_seq=0 ttl=64 time=0.267 ms
56 bytes from 172.17.0.5: icmp_seq=1 ttl=64 time=0.250 ms
56 bytes from 172.17.0.5: icmp_seq=2 ttl=64 time=0.256 ms
Test the db container with ping, which resolves
172.17.0.5
Note: The official ubuntu image does not have pings installed by default and needs to be installed on its own.
Users can link multiple parent containers to child containers, such as multiple webs to db containers.