Cloud Native 8 min read

Master Docker Container Networking: IP, Ports, and Link Mechanism Explained

This guide explains three Docker container interconnection methods—direct IP, host‑IP with exposed ports, and the legacy --link mechanism—detailing their pros and cons, usage requirements, underlying principles, and step‑by‑step command examples on CentOS.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Docker Container Networking: IP, Ports, and Link Mechanism Explained

System Environment

Server: CentOS Linux release 7.4.1708 (Core); Docker version 20.10.12; CPU architecture x86_64.

Docker Container Interconnection Overview

2.1 Three Ways to Connect Containers on the Same Host

When multiple Docker containers run on the same host, they can communicate via:

Using the container's IP address directly. This approach hard‑codes IPs, making migration difficult, and IPs may change after a restart unless a static IP is assigned.

Using the host's IP address combined with the container's exposed port. This method is simple but limited to services listening on the published ports.

Using Docker's --link mechanism, which allows one container to discover another by name and securely passes connection information.

2.2 Using docker --link : Precautions

When employing --link, keep in mind:

The linked container must be running.

The container that creates the link also needs the target container to be running.

The host IP of the linked container does not need to be fixed; Docker updates /etc/hosts with a new alias each time the container is created.

2.3 How --link Works

The --link option adds an entry to the target container's /etc/hosts file, mapping a chosen name to the container's IP address, enabling name‑based communication.

Practical Examples

3.1 Communicating via Container IP

Create a MySQL container:

docker run -dit --restart=always --name=mysql5 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 hub.c.163.com/library/mysql:latest

Inspect its IP address:

docker inspect mysql5 | grep -i ipaddr
# Output includes "IPAddress": "172.17.0.4"

Launch a CentOS container and ping the MySQL container:

docker run -it --restart=always --name=centos7 hub.c.163.com/library/centos:latest ping 172.17.0.4

3.2 Communicating via --link

Docker allows inter‑container communication by default; it can be disabled with -icc=false. When disabled, --link name:alias is required.

Create a CentOS container linked to the MySQL container:

docker run -it --restart=always --name=c7 --link mysql5:mysql hub.c.163.com/library/centos:latest ping mysql

Ping results show successful communication using the alias mysql.

Enter the linked container and test connectivity:

docker exec -it c7 /bin/bash
ping mysql

Install a MySQL client (MariaDB) inside the CentOS container: yum -y install mariadb Connect to the MySQL server using the container IP: mysql -uroot -p123456 -h 172.17.0.4 After logging in, you can list databases, e.g., show databases;.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerDevOpsLinuxcontainer networkingLink Mechanism
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.