Method introduction:
Add the parameter link when running the container.
The specific steps are as follows:
1. Run the first container
docker run -it --name centos-1 docker.io/centos:latest
2. Run the second container
[root@CentOS ~]# docker run -it --name centos-2 --link centos-1:centos-1 docker.io/centos:latest
Parameter introduction:
--link: The first centos-1 in the parameter is the container name, and the second centos-1 is the defined container alias (use the alias to access the container). For ease of use, generally the alias defaults to the container name.
The test results are as follows:
[root@e0841aa13c5b /]# ping centos-1 PING centos-1 (172.17.0.7) 56(84) bytes of data. bytes from centos-1 (172.17.0.7): icmp_seq=1 ttl=64 time=0.210 ms bytes from centos-1 (172.17.0.7): icmp_seq=2 ttl=64 time=0.116 ms bytes from centos-1 (172.17.0.7): icmp_seq=3 ttl=64 time=0.112 ms bytes from centos-1 (172.17.0.7): icmp_seq=4 ttl=64 time=0.114 ms
Note: This method has requirements on the order in which containers are created. If multiple containers within the cluster need to access each other, it will be inconvenient to use.
Recommended tutorial:docker tutorial
The above is the detailed content of How docker containers call each other. For more information, please follow other related articles on the PHP Chinese website!