Cloud Native 3 min read

How to Ensure Stable Docker Container Communication Without Relying on Changing IPs

Learn how to connect Docker containers reliably by using container names instead of volatile IP addresses, with step-by-step commands for naming, linking, and verifying connectivity, ensuring network stability even after container or Docker service restarts.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Ensure Stable Docker Container Communication Without Relying on Changing IPs

1. Connecting via IP

Containers have a virtual bridge and can be assigned their own IP addresses, allowing direct IP‑based communication. After launching two containers and checking their IPs (e.g., 192.168.42.4 and 192.168.42.5), a ping from one to the other succeeds, confirming network connectivity.

However, container IPs change after a restart, making IP‑based communication unreliable.

2. Connecting via container name

Docker provides an alias linking mechanism that removes the dependency on IP addresses. First, start a container with a name: docker run -d -p 22 --name net001 dys/centos:ssh The --name net001 option assigns the name net001 to the container, which persists across Docker service restarts.

Start a second container and link it to the first one:

docker run -d -p 22 --name net002 --link net001:lk001 dys/centos:ssh

The --link net001:lk001 option creates an alias lk001 for net001. Inside net002, you can ping net001 (or lk001) and the connection succeeds, regardless of the first container’s IP changes.

What Docker does

Docker updates the /etc/hosts file inside the linked container. For example, after linking, cat /etc/hosts in net002 shows a line such as: 192.168.42.4 lk001 4193f1ac3f78 net001 When net001 receives a new IP after a restart, Docker automatically rewrites this entry, ensuring that name‑based communication remains stable.

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.

Cloud NativeDockerIP addressingcontainer linking
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.