问题描述
根据 Docker Compose 的
links
选项被弃用后,帖子需要更新.
基本上,links
不再需要,因为它的主要目的是通过添加环境变量使容器可以被另一个容器访问,它隐含在 network
中.当容器放置在同一个网络中时,它们可以使用容器名称和其他别名作为主机相互访问.
对于 docker run
,--link
也已弃用,应替换为自定义网络.
docker 网络创建 mynetdocker run -d --net mynet --name container1 my_imagedocker run -it --net mynet --name container1 another_image
depends_on
表示开始顺序(隐含图片拉取顺序),这是links
的一个很好的副作用.
According to the Docker Compose's compose-file documentation:
depends_on
- Express dependency between services.links
- Link to containers in another service and also express dependency between services in the same way as depends_on.
I don't understand the purpose of linking to other containers so the difference between two options still seems quite difficult for me.
It would be much easier if there is an example, but I can't find any.
I noticed, when I link container B with container A then container B will be "pingable" inside container A's shell.
I ran ping B
inside container A's bash
and got result like this (just for reference, image from the Internet)
The post needs an update after the links
option is deprecated.
Basically, links
is no longer needed because its main purpose, making container reachable by another by adding environment variable, is included implicitly with network
. When containers are placed in the same network, they are reachable by each other using their container name and other alias as host.
For docker run
, --link
is also deprecated and should be replaced by a custom network.
docker network create mynet
docker run -d --net mynet --name container1 my_image
docker run -it --net mynet --name container1 another_image
depends_on
expresses start order (and implicitly image pulling order), which was a good side effect of links
.
这篇关于docker_compose.yml中links和depends_on的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!