Easy Tutorial
❮ Docker Swarm Docker Inspect Command ❯

Docker Container Networking

Previously, we accessed services running inside Docker containers through network ports.

Containers can run network applications, and to allow external access to these applications, we can specify port mapping using the -P or -p parameters.

Next, we will demonstrate connecting to a Docker container via ports.


Network Port Mapping

We created a container for a Python application.

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

Additionally, we can specify the network address to which the container binds, such as 127.0.0.1.

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

tutorialpro@tutorialpro:~$ 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 -p to specify the container port to bind to the host port.

The differences between the two methods are:

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

Furthermore, we can specify the network address to which the container binds, such as 127.0.0.1.

tutorialpro@tutorialpro:~$ docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py
95c6ceef88ca3e71eaf303c2833fd6701d8d1b2572b5613b5a932dfdfe8a857c
tutorialpro@tutorialpro:~$ 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 allows us to access the container's port 5000 via 127.0.0.1:5001.

In the above examples, the default binding is to TCP ports. To bind to UDP ports, append /udp to the port.

tutorialpro@tutorialpro:~$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
6779686f06f6204579c1d655dd8b2b31e8e809b245a97b2d3a8e35abe9dcd22a
tutorialpro@tutorialpro:~$ 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 view port bindings.

tutorialpro@tutorialpro:~$ docker port adoring_stonebraker 5000
127.0.0.1:5001

Docker Container Linking

Port mapping is not the only method to connect Docker containers.

Docker has a linking system that allows multiple containers to be connected, sharing connection information.

Docker linking creates a parent-child relationship where the parent container can see the child container's information.


Container Naming

When creating a container, Docker automatically names it. Alternatively, we can use the --name flag to name the container, for example:

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

We can view the container names using the docker ps command.

tutorialpro@tutorialpro:~$ docker ps -l
CONTAINER ID     IMAGE            COMMAND           ...    PORTS                     NAMES
43780a6eabaa     training/webapp   "python app.py"  ...     0.0.0.0:32769->5000/tcp   tutorialpro

Creating a New Network

$ docker network create -d bridge test-net

Parameter explanation:

-d: Specifies the Docker network type, which can be bridge or overlay.

The overlay network type is used for Swarm mode and can be ignored in this section.

Connecting 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 and run another container to join the test-net network:

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

To prove that the test1 and test2 containers are interconnected, use ping.

If the ping command is not available in the test1 or test2 containers, install it with the following commands:

apt-get update
apt install iputils-ping

In the test1 container, enter the following command:

Similarly, in the test2 container, the connection will be successful:

This establishes the connection between the test1 and test2 containers.

For multiple containers that need to be interconnected, Docker Compose is recommended and will be introduced later.


Configuring DNS

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

{
  "dns" : [
    "114.114.114.114",
    "8.8.8.8"
  ]
}

After setting this, the DNS for newly started containers will automatically be configured to 114.114.114.114 and 8.8.8.8.

The Docker service needs to be restarted for the changes to take effect.

To check if the container's DNS is effective, use the following command, which will output the container's DNS information:

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

Manual Container Configuration

If you only want to set DNS for a specific container, use the following command:

$ docker run -it --rm -h host_ubuntu  --dns=114.114.114.114 --dns-search=test.com ubuntu

Parameter explanation:

--rm: Automatically cleans up the container's filesystem when the container exits.

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

--dns=IP_ADDRESS: Adds a DNS server to the container's /etc/resolv.conf, allowing the container to use this server to resolve hostnames not found in /etc/hosts.

--dns-search=DOMAIN: Sets the container's search domain. When set to .example.com, searching for a host named "host" will also search for "host.example.com". If --dns and --dns-search are not specified when the container starts, Docker will default to using the /etc/resolv.conf of the host machine to configure the container's DNS.

❮ Docker Swarm Docker Inspect Command ❯