Master Docker Container Networking: 3 Methods and Practical Link Examples
This guide explains Docker container networking on a CentOS host, covering three connection methods, the use and principles of the --link option, and step‑by‑step commands to create MySQL and CentOS containers, test connectivity, and access MySQL from the CentOS container.
1. System Environment
Server version: CentOS Linux release 7.4.1708 (Core); Docker version: 20.10.12; CPU architecture: x86_64.
2. Docker Container Interconnection Overview
2.1 Three ways to connect containers on the same host
Use the container's IP address directly (hard‑coded IPs are not portable and may change on restart).
Use the host IP plus the container's exposed port (limited to services listening on that port).
Use Docker's link mechanism, which allows containers to discover each other by name and securely share connection information.
2.2 Using docker --link : precautions
The linked container must be running.
The container that creates the link must also 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.
2.3 Principle of docker --link
The link adds name‑to‑IP mappings in /etc/hosts, enabling name‑based communication between containers.
3. Docker Container Interconnection
3.1 Communicating via container IP address
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:latestInspect its IP address:
# docker inspect mysql5 | grep -i ipaddr
"IPAddress": "172.17.0.4",Start 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.43.2 Communicating via --link parameter
Docker allows inter‑container communication by default; it can be disabled with -icc=false. When disabled, use --link name:alias to connect specific containers.
Create a CentOS container linked to MySQL:
# docker run -it --restart=always --name=c7 --link mysql5:mysql hub.c.163.com/library/centos:latest ping mysqlEnter the container and ping by name:
# docker exec -it c7 /bin/bash
# ping mysqlInstall a MySQL client inside the CentOS container (MariaDB client works for MySQL): # yum -y install mariadb Connect to the MySQL server from the CentOS container: # mysql -uroot -p123456 -h 172.17.0.4 After successful login, you can list databases and perform queries.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
