Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Docker container linking

Docker container linking

Earlier we achieved access to services running inside docker containers through network ports.

Some network applications can run in containers. To make these applications accessible from outside, you can use the -P or -p argument to specify port mapping.

Next, let's implement connecting to a docker container through a port.


Network port mapping

We created a container for a python application.



runoob@runoob:~$ docker run -d -P training/webapp python app.py

fce072cc88cee71b1cdceb57c2821d054a4a59f67da6b416fceb5593f059fc6d

In addition, we can specify the network address the container binds to, such as binding to 127.0.0.1.

We use -P to bind the port number, and use docker ps You can see that container port 5000 is bound to host port 32768.



runoob@runoob:~$ 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 flag to specify binding a container port to a host port.

The difference between the two approaches is:



runoob@runoob:~$ docker run -d -p 5000:5000 training/webapp python app.py

33e4523d30aaf0258915c368e66e03b49535de0ef20317d3f639d40222ba6bc0



runoob@runoob:~$ 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 the container binds to, such as binding to 127.0.0.1.



runoob@runoob:~$ docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py

95c6ceef88ca3e71eaf303c2833fd6701d8d1b2572b5613b5a932dfdfe8a857c

runoob@runoob:~$ 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

In this way, we can access the container's port 5000 by visiting 127.0.0.1:5001.

In the examples above, tcp ports are bound by default. To bind a UDP port, you can add /udp。



runoob@runoob:~$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

6779686f06f6204579c1d655dd8b2b31e8e809b245a97b2d3a8e35abe9dcd22a

runoob@runoob:~$ 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

docker port command lets us quickly view port bindings.



runoob@runoob:~$ 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 linking system that allows multiple containers to be connected together and share connection information.

A docker link creates a parent-child relationship in which the parent container can see information about the child container.


Container naming

When we create a container, docker automatically assigns it a name. In addition, we can also use --name flag to name the container, for example:



runoob@runoob:~$  docker run -d -P --name runoob training/webapp python app.py

43780a6eabaaf14e590b6e849235c75f3012995403f97749775e38436db9a441

We can use the docker ps command to view the container name.



runoob@runoob:~$ docker ps -l

CONTAINER ID     IMAGE            COMMAND           ...    PORTS                     NAMES

43780a6eabaa     training/webapp   "python app.py"  ...     0.0.0.0:32769->5000/tcp   runoob

Create a new network

First, let's create a new Docker network.

$ docker network create -d bridge test-net

Parameter Description:

-d: The argument specifies the Docker network type, which can be bridge or overlay.

The overlay network type is used for Swarm mode, which you can ignore in this section.

Connect containers

Run a container and connect it 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 it to the test-net network:

$ docker run -itd --name test2 --network test-net ubuntu /bin/bash

Click the image to view a larger version:

Next, use ping to verify that the test1 and test2 containers have established an interconnection.

If the test1 and test2 containers do not have the ping command, run the following command inside the containers to install ping (learn and use immediately: you can install it in one container, commit the container to an image, and then rerun the two containers from the new image).

apt-get update

apt install iputils-ping

Enter the following command in the test1 container:

Click the image to view a larger version:

Similarly, the test2 container will also successfully connect to:

Click the image to view a larger version:

In this way, the test1 and test2 containers have established an interconnection.

If you have multiple containers that need to connect to each other, Docker Compose is recommended, which will be introduced later.


Configure DNS

We can add the following content to the /etc/docker/daemon.json file on the host to set the DNS for all containers:

{

  "dns" : [

    "114.114.114.114",

    "8.8.8.8"

  ]

}

After setting, the DNS of started containers will be automatically configured as 114.114.114.114 and 8.8.8.8.

After configuration, you need to restart docker for it to take effect.

To check whether the container's DNS is effective, you can use the following command, which outputs the container's DNS information:

$ docker run -it --rm  ubuntu  cat etc/resolv.conf

Click the image to view a larger version:

Manually specify container configuration

If you only want to set DNS for a specific 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 cleans up the container's internal filesystem when the container exits.

-h HOSTNAME or --hostname=HOSTNAME: Sets the container's hostname, which will be written to /etc/hostname and /etc/hosts inside the container.

--dns=IP_ADDRESS: Adds a DNS server to the container's /etc/resolv.conf so the container uses this server to resolve all hostnames not in /etc/hosts.

--dns-search=DOMAIN: Sets the container's search domain. When the search domain is set to .example.com, when searching for a host named host, DNS searches not only host but also host.example.com.

Click the image to view a larger version:

If at container startup you do not specify --dns and --dns-search, Docker will by default use the /etc/resolv.conf on the host to configure the container's DNS.