Docker Container Connection
Earlier we implemented accessing services running inside a docker container through a network port.
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.
Let’s implement the following to connect to a docker container through a port.
Network port mapping
We create a container for a python application.
apidemos@apidemos:~$ docker run -d -P training/webapp python app.py
fce072cc88cee71b1cdceb57c2821d054a4a59f67da6b416fceb5593f059fc6d
In addition, we can specify the network address to which the container is bound, for example, bind 127.0.0.1.
We use -P to bind the port number, and use docker ps to see that container port 5000 binds to host port 32768.
apidemos@apidemos:~$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper
We can also use the -p identifier to specify that the container port is bound to the host port.
The difference between the two ways is :
- -P : is the container internal port randomly mapped to the host port.
- -p : is the container internal port bound to the specified host port.
apidemos@apidemos:~docker run -d -p 5000:5000 training/webapp python app.py
33e4523d30aaf0258915c368e66e03b49535de0ef20317d3f639d40222ba6bc0
apidemos@apidemos:~ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
33e4523d30aa training/webapp "python app.py" ... 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper
In addition, we can specify the network address to which the container is bound, for example, to 127.0.0.1.
apidemos@apidemos:~docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py
95c6ceef88ca3e71eaf303c2833fd6701d8d1b2572b5613b5a932dfdfe8a857c
apidemos@apidemos:~ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
95c6ceef88ca training/webapp "python app.py" ... 5000/tcp, 127.0.0.1:5001->5000/tcp adoring_stonebraker
33e4523d30aa training/webapp "python app.py" ... 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper
This way we can access the container’s port 5000 by accessing 127.0.0.1:5001.
In the above example, the default is to bind the tcp port, if you want to bind the UDP port, you can add /udp after the port.
apidemos@apidemos:~docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
6779686f06f6204579c1d655dd8b2b31e8e809b245a97b2d3a8e35abe9dcd22a
apidemos@apidemos:~ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
6779686f06f6 training/webapp "python app.py" ... 5000/tcp, 127.0.0.1:5000->5000/udp drunk_visvesvaraya
95c6ceef88ca training/webapp "python app.py" ... 5000/tcp, 127.0.0.1:5001->5000/tcp adoring_stonebraker
33e4523d30aa training/webapp "python app.py" ... 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper
The docker port command allows us to quickly see how the ports are bound.
apidemos@apidemos:~$ docker port adoring_stonebraker 5000
127.0.0.1:5001
Docker Container Interconnection
Port mapping is not the only way to connect docker to another container.
docker has a connection system that allows multiple containers to be connected together and share connection information.
A docker connection creates a parent-child relationship, where the parent container can see information about the child containers.
Container naming
When we create a container, docker will automatically name it. Alternatively, we can name the container using the –name identifier, e.g.
apidemos@apidemos:~$ docker run -d -P --name apidemos training/webapp python app.py
43780a6eabaaf14e590b6e849235c75f3012995403f97749775e38436db9a441
We can use the docker ps command to see the container names.
apidemos@apidemos:~$ docker ps -l
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
43780a6eabaa training/webapp "python app.py" ... 0.0.0.0:32769->5000/tcp apidemos
Create a new network
Here’s how to create a new Docker network.
$ docker network create -d bridge test-net
Parameter description.
-d: parameter specifies the Docker network type, there are bridge, overlay.
The overlay network type is used in Swarm mode and you can ignore it in this section.
Connecting containers
Run a container and connect to the newly created test-net network:
$ docker run -itd --name test1 --network test-net ubuntu /bin/bash
Open a new terminal, run another container and join the test-net network:
$ docker run -itd --name test2 --network test-net ubuntu /bin/bash
Output:
The following ping is used to demonstrate that the test1 container and the test2 container have established an interconnection relationship.
If there is no ping command in the test1 and test2 containers, execute the following command inside the container to install ping (ready to learn: you can install it in a container, commit the container to the image, and re-run the above two containers with the new image).
apt-get update
apt install iputils-ping
Enter the following command in the test1 container.
Similarly, the test2 container will successfully connect to the :
This establishes an interconnection between the test1 container and the test2 container.
If you have multiple containers that need to interconnect with each other, it is recommended to use Docker Compose, which will be described later.
Configuring DNS
We can set the DNS for all containers by adding the following to the /etc/docker/daemon.json file on the host.
{
"dns" : [
"114.114.114.114",
"8.8.8.8"
]
}
After setting, the DNS of the startup container will be automatically configured to 114.114.114.114 and 8.8.8.8.
After configuration, you need to restart docker to take effect.
To check whether the DNS of the container is in effect, you can use the following command, which will output the DNS information of the container.
$ docker run -it --rm ubuntu cat etc/resolv.conf
Manually specify the configuration of the container
If you want to set DNS only in the specified container, you can use the following command.
$ docker run -it --rm -h host_ubuntu --dns=114.114.114.114 --dns-search=test.com ubuntu
Parameter description.
–rm: Automatically clean up the container’s internal filesystem when the container exits.
-h HOSTNAME or –hostname=HOSTNAME: Set the container’s hostname, which will be written to /etc/hostname and /etc/hosts inside the container.
–dns=IP_ADDRESS: Add a DNS server to the container’s /etc/resolv.conf
and let the container use this server to resolve all hostnames that are not in /etc/hosts.
–dns-search=DOMAIN: Set the container’s search domain. When the search domain is set to .example.com
, when searching for a host named host, DNS will search not only for the host, but also for host.example.com.
If –dns and –dns-search are not specified at container startup, Docker will configure the container’s DNS with /etc/resolv.conf on the host host by default.