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.
The next following command will be used to start the Redis client that is called redis_client.
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:
The next commands that you will enter would be used to install the ping.
You would get a reply back after entering the command and pinging the Redis server.
Now we would add the command to connect to the Redis server.
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 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.
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.
After your research in the network, you configure that a subnet and a gateway have been created.
[
{
"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.
Now, we will execute the command for attaching the internal network bridge to the redis_server.
And now attach the client:
After you investigate the internal network, you would realize that the two containers have been included in the network of the bridge.
Now, coming from your redis_client, you would be able to ping redis_server ad later connect to that.
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.