
- Docker - Home
- Docker - Overview
- Docker - Installing on Linux
- Docker - Installation
- Docker - Hub
- Docker - Images
- Docker - Containers
- Docker - Registries
- Docker - Compose
- Docker - Working With Containers
- Docker - Architecture
- Docker - Layers
- Docker - Container & Hosts
- Docker - Configuration
- Docker - Containers & Shells
- Docker - Dockerfile
- Docker - Building Files
- Docker - Public Repositories
- Docker - Managing Ports
- Docker - Web Server
- Docker - Commands
- Docker - Container Linking
- Docker - Data Storage
- Docker - Volumes
- Docker - Networking
- Docker - Security
- Docker - Toolbox
- Docker - Cloud
- Docker - Build Cloud
- Docker - Logging
- Docker - Continuous Integration
- Docker - Kubernetes Architecture
- Docker - Working of Kubernetes
- Docker - Generative AI
- Docker - Hosting
- Docker - Best Practices
- Docker - Setting Node.js
- Docker - Setting MongoDB
- Docker - Setting NGINX
- Docker - Setting ASP.Net
- Docker - Setting MySQL
- Docker - Setting Go
- Docker - Setting Rust
- Docker - Setting Apache
- Docker - Setting MariaDB
- Docker - Setting Jupyter
- Docker - Setting Portainer
- Docker - Setting Rstudio
- Docker - Setting Plex
- Docker Setting - Flame
- Docker Setting - PostgreSql
- Docker Setting - Mosquitto
- Docker Setting - Grafana
- Docker Setting - Nextcloud
- Docker Setting - Pawns
- Docker Setting - Ubuntu
- Docker Setting - RabbitMQ
- Docker - Setting Python
- Docker - Setting Java
- Docker - Setting Redis
- Docker - Setting Alpine
- Docker - Setting BusyBox
- Docker Setting - Traefik
- Docker Setting - WordPress
- Docker Useful Resources
- Docker - Quick Guide
- Docker - Useful Resources
- Docker - Discussion
Docker - Container Linking
Docker container linking is a compelling feature that enables containers to communicate with one another in a very secure and effective way. It creates a secure tunnel between containers when you link them so that one container can access services running within another. This particularly comes in handy during the setup of microservices architectures since different services will have to be placed inside separate containers.
In this chapter, lets understand what is container linking and how to achieve it with the help of Docker commands and examples.
What is Docker Container Linking?
As discussed, Container linking in Docker can help you build a network of interconnected services that work together to form a complete application. When linking containers, environment variables are to be specified, and the /etc/hosts file is updated to make it easier for the linked containers to discover each other.
Note that Docker container linking is already a legacy feature and the modern way containers communicate with one another lies in Docker networks. Docker networks provide greater flexibility, manageability, and scalability in inter-container communications. With Docker networks, the addition or removal of containers happens dynamically without disrupting the existing channels of communication.
While the linking container remains useful for development environments, Docker recommends that for more complex and production-grade environments, Docker networks should be used. The move to using networks reflects Docker's evolution toward more robust, scalable, and manageable solutions in terms of container orchestration.
Connect Container using the linking System
Connecting Docker containers using the --link option provides secure, direct communication where a container can connect to and use services offered within another container. It sets up the environment variables and populates the /etc/hosts configuration file of the receiving container to connect the two without exposing any redundant ports to the outside world.
To link Docker containers together using --link, when starting a new container, you can use the following syntax −
$ docker run -d --name recipient_container --link source_container:image_alias image_name:tag
Here's a breakdown of the command −
- -d − Run container in detached mode.
- --name recipient_container − It is possible to specify a recipient container name.
- --link source_container/image_alias − Links source container to the recipient container for the given image alias.
- image_name:tag − Specifies the Docker image to use for creating the recipient container.
For example, linking a MySQL database container named db with a PHP application container −
$ docker run -d --name php_app --link db:mysql php:latest

The --link option in Docker provides an excellent way to create a microservices architecture where several containers can communicate securely. This simplifies the networking setup because it configures the environment variables and hosts' file entries readily for smooth communication between linked containers.
Connect Containers using Network Port Mapping
Port mapping in Docker lets containers communicate with the host system. This essentially makes a few ports inside your Docker container visible to the host machine so that you can access services running inside a container from another network or other containers within the same Docker network.
You will use the -p flag in a Docker run command to connect containers using port mapping. The flag sets the mappings of the host system and container ports.
Here's how you can do it −
$ docker run -d --name my-container -p host_port:container_port image_name:tag
In this command −
- -d − Run the container in a detached mode in the background.
- --name my-container − Names the container so you can easily manage it.
- -p host_port:container_port − Exposes the container_port from the Docker container on the host system as host_port.
- image_name:tag − This specifies the Docker image to be used by a container upon creation.
For example, the following command will run an Nginx web server and map the container port 80 to host port 8080 −
$ docker run -d --name my-nginx -p 8080:80 nginx:latest

This command will start a new container called my-nginx from the nginx:latest image. Now, the Nginx web server inside the container will be available by visiting http://localhost:8080 in any web browser.
Environment Variables Created by Docker
Understanding Docker environment variables, especially when using --link to connect containers and update the /etc/hosts file, is essential in configuring and managing interdependent Docker environments. While linking containers using --link, Docker sets a few environment variables itself; it also modifies the /etc/hosts configuration in the receiving container to support smooth communication between the linked containers.
For every link created, Docker sets corresponding environment variables in the target container. These are automatically populated with all the information about the source container's connections, including IP addresses and ports. The environment variables Docker sets −
- Environment Variables Format − Docker sets variables with the format <alias>_PORT_<port>_PROTO, <alias>_PORT_<port>_PORT, and <alias>_PORT_<port>_ADDR.
- Here, <alias> is the alias given to the source container, <port> is the exposed port number in the source container, and PROTO, PORT, and ADDR refer to protocol type (TCP or UDP), port number, and IP address respectively.
For example, linking a MySQL container (DB) to an application container will create variables like DB_PORT_3306_TCP_PROTO, DB_PORT_3306_TCP_PORT, and DB_PORT_3306_TCP_ADDR inside of the environment of the app container.
Updating the /etc/hosts File
Docker modifies the content of /etc/hosts inside the target container to add an entry for every linked container. The entry associates the alias specified with `--link` option to the IP address of the source container. For example, if you link a MySQL container (db) with an alias mysql, Docker modifies the content of the /etc/hosts in the target container as −
<IP_address_of_db_container> mysql
This allows the recipient container to resolve the alias mysql to the IP address where db has mapped for eased network communication between linked containers.
Important Considerations
Dynamic Updates
All of these environment variables and entries in the /etc/hosts file are dynamically updated by Docker any time containers are linked or restarted to ensure uniformity and reliability in communication.
Deprecation
While the --link still works, Docker recommends using user-defined networks for connecting containers as it provides better control over networking configurations and more flexibility.
Security
It is best to prevent sensitive data from being hardcoded within environment variables or /etc/host entries. Instead, practices like Docker secrets or configuration management security tools for managing sensitive data must be used.
Conclusion
Docker has made it easy to connect containers by using link and other advanced methods like port mapping and network configurations for smooth interaction among containers in Dockerized environments. This will be useful, especially within microservices architectures and complex application deployments.
While link provides an easy way to connect containers, using user-defined networks is the recommended Docker way, as it supports enhanced networking capabilities and adheres to best practices. User-defined networks provide much more control over the communication, scaling, and security aspects compared with legacy methods.
FAQs on Docker Container Linking
We have collected here a set of Frequently Asked Questions on how to dockerize Container Linking followed by their answers −
1. Is Docker container linking still used?
Docker container linking has been deprecated and substituted by more up-to-date networking options like user-defined bridge networks, Docker Compose, etc. Legacy linking might still pop up on some older set-ups, but using the newer networking options is recommended to maintain better safety and security.
2. What is the alternative to Docker container linking?
The major alternatives for linking containers are user-defined bridge networks and Docker Compose. These have the advantage of flexibility and control over network traffic between containers. User-defined networks allow users to create isolated networks for their containers, while Docker Compose makes managing multi-container applications - and their networking configurations - much more seamless.