Docker

Docker Container Links

Many single applications use docker as a container. Many people use it across the globe, and one of the reasons for its popularity is its ease of usage. You do not need to be skilled in any software to use and set up docker and start using it for experiments. However, as the user tries more complex servers, it is necessary to know about docker networking techniques. The article below discusses docker container links and the features of its networking.

Docker Container Links

Docker link’s main use is to allow linking containers together. Before version 1.9, this was the only way for the connection of the containers. Docker links are not to remain in the future, and people usually avoid this for new designs. However, as a novice, you must have some idea about the linking if you have to deal with a legacy code.

Docker Link Example

In the following paragraph, we will be creating two containers that would be working as a Redis server and Redis client, respectively. We will enter data and information to the Redis server by using the Redis client. The following first command will be starting a Redis server that is called redis_server.

$ docker run -d --name redis_server redis

$ sudo docker ps

The next following command will be used to start the Redis client that is called redis_client.

$ sudo docker run -it --rm --name redis_client --link redis_server:redisDB redis bash

The option of ‘link is used here to link the redis_server while giving the information to redisDB. After you have typed a command, a command prompt will open in front of you like the following:

root@e2364251d31d:/data#

The next commands that you will enter would be used to install the ping.

$ apt-get update

$ apt-get upgrade

$ apt-get install iputils-ping

You would get a reply back after entering the command and pinging the Redis server.

$ ping redisDB

Now we would add the command to connect to the Redis server.

$ sudo docker exec -it redis_client sh

$ redis-cli –h redisDB
redisDB:6379>

This new command DB:6379 means that we are connected to the Redis server. Now you can add information to the server. An example is given below.

$ redisDB:6379>set book "The Happy Prince"
$ redisDB:6379>set author "Mark Twain"
$ redisDB:6379>get book
$ redisDB:6379>get author

Docker Networking

The docker installed the networking feature in its 1.9 version. The new version creates three networks automatically after we enter the following command.

$ sudo docker network ls

None, Bridge, and host are the networks that come to existence in this whole process. Let them discuss below:

Bridge: the bridge network represents Docker0. Docker0 is a virtual Ethernet bridge whose task is to forward packets to other network interfaces attached to it. Additionally, the client can build their self-designed bridges.

Host: The main task of the host network is to add containers to the host network stack. Once you have defined a host network, the separation and difference between host and container are gone.

Note: The none network’s main task is to turn off networking. Some apps run without any networks, and they don’t need any network for any reason.

Networking Example Based on User-Defined Bridge Network

This section will help to test Docker using the Redis server. Firstly we will be creating a network called “internal network” with the command.

$ sudo docker network create -d bridge internal_network

After your research in the network, you configure that a subnet and a gateway have been created.

$ sudo docker network inspect internal_network
[
    {
        "Name": "internal_network",
        "Id": "9bc2213d3a39d46765fe50ef8e9b7819df8e7124b0a46552447cbda84e31b049",
        "Created": "2017-11-02T08:01:05.119528611Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

You can also list the bridge network which was created before.

$sudo docker network ls

Now, we will execute the command for attaching the internal network bridge to the redis_server.

$ sudo docker run -d --network=internal_network --name=redis_server redis

And now attach the client:

$ sudo docker run -d --network=internal_network --name=redis_client redis bash

After you investigate the internal network, you would realize that the two containers have been included in the network of the bridge.

$ sudo docker network inspect internal_network


Now, coming from your redis_client, you would be able to ping redis_server ad later connect to that.

$ ping redis_server

Conclusion:

In this article, I have shown you how to configure and work with docker container links. Docker is a very easy to use container technology used for single applications. Many people use it across the globe, and one of the reasons for its popularity is its ease of usage.

About the author

Younis Said

I am a freelancing software project developer, a software engineering graduate and a content writer. I love working with Linux and open-source software.