docker_compose.yml 中的 links 和 depends_on 的区别 [英] Difference between links and depends_on in docker_compose.yml

查看:160
本文介绍了docker_compose.yml 中的 links 和 depends_on 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 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 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆