docker_compose.yml 中的链接和 depends_on 之间的区别
Difference between links and depends_on in docker_compose.yml
根据 Docker Compose 的 compose-file documentation:
depends_on
- 表示服务之间的依赖关系。
links
- Link 到另一个服务中的容器,并且 表达服务之间的依赖关系 与 depends_on 相同。
我不明白 linking 到其他容器的目的,所以两个选项之间的区别对我来说似乎仍然很困难。
如果有例子就容易多了,但是我找不到。
我注意到,当我 link 容器 B 和容器 A 时,容器 B 将 "pingable" 在容器 A 的 shell 中。
我运行ping B
在容器A的bash
里面得到了这样的结果(仅供参考,图片来自网络)
[2016 年 9 月更新]:此答案适用于 docker 撰写文件 v1(如下面的示例撰写文件所示)。对于 v2,请参阅@Windsooon 的其他答案。
[原回答]:
文档里说的很清楚了。 depends_on
decides the dependency and the order of container creation and links
不仅会这些,还会
Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.
例如,假设以下 docker-compose.yml
文件:
web:
image: example/my_web_app:latest
links:
- db
- cache
db:
image: postgres:latest
cache:
image: redis:latest
使用 links
,web
中的代码将能够使用 db:5432
访问数据库,假设端口 5432 在 db
图像中公开。如果使用 depends_on
,这是不可能的,但容器的启动顺序是正确的。
此答案适用于 docker-compose 版本 2,它也适用于 版本 3
使用depends_on时仍然可以访问数据。
如果你查看 docker 文档 Docker Compose and Django,你仍然可以像这样访问数据库:
version: '2'
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
链接和depends_on有什么区别?
链接:
当您为数据库创建容器时,例如:
docker run -d --name=test-mysql --env="MYSQL_ROOT_PASSWORD=mypassword" -P mysql
docker inspect d54cf8a0fb98 |grep HostPort
你可能会发现
"HostPort": "32777"
这意味着您可以从本地主机端口 32777(容器中为 3306)连接数据库,但每次重新启动或删除容器时此端口都会更改。因此,您可以使用链接来确保您始终连接到数据库,而不必知道它是哪个端口。
web:
links:
- db
depends_on:
我从 Giorgio Ferraris 找到了一个不错的博客 Docker-compose.yml: from V1 to V2
When docker-compose executes V2 files, it will automatically build a network between all of the containers defined in the file, and every container will be immediately able to refer to the others just using the names defined in the docker-compose.yml file.
和
So we don’t need links anymore; links were used to start a network communication between our db container and our web-server container, but this is already done by docker-compose
更新
depends_on
表示服务之间的依赖关系,有两个作用:
docker-compose up
将按依赖顺序启动服务。在下面的例子中,db和redis会先于web启动。
docker-compose up SERVICE
将自动包含 SERVICE 的依赖项。在下面的例子中,docker-compose up web也会创建并启动db和redis。
简单示例:
version: '2'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
Note: depends_on will not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.
post 在 links
选项被弃用后需要更新。
基本上,不再需要 links
,因为它的主要目的是通过添加环境变量使另一个容器可以访问,隐式包含在 network
中。当容器放置在同一个网络中时,它们可以使用容器名称和其他别名作为主机相互访问。
对于 docker run
,--link
也已弃用,应由自定义网络代替。
docker network create mynet
docker run -d --net mynet --name container1 my_image
docker run -it --net mynet --name container1 another_image
depends_on
表示开始顺序(和隐含的图像拉取顺序),这是 links
.
的一个很好的副作用
我认为这个问题的答案需要根据 v1.27.0 中首次引入的新 Docker compose 规范进行更新,该规范现在允许 depends_on
的长格式:
https://github.com/compose-spec/compose-spec/blob/master/spec.md#long-syntax-1
在这个长格式中,您可以指定要等待服务启动、正常运行或成功完成。
Docker 如果您在该服务上生成 health_check
,则 compose 知道该服务是健康的:
https://github.com/compose-spec/compose-spec/blob/master/spec.md#healthcheck
我建议阅读文档中的示例以了解更多详细信息,请参阅上面的链接!
举个简单的例子,这是我在用于集成测试的撰写文件中使用的内容:
services:
cloud-broker:
image: my.docker.registry/activemq-artemis:latest
healthcheck:
test: ["CMD-SHELL", "wget http://localhost:8161/ --delete-after --tries=3 2> /dev/null"]
interval: 10s
timeout: 5s
retries: 5
postgresql:
image: postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
environment:
POSTGRES_PASSWORD: "<my-secret>"
POSTGRES_USER: "postgres"
POSTGRES_DB: "postgres"
# This service calls a script to create an empty database and the service-user
postgresql-setup:
image: postgres
depends_on:
postgresql:
condition: service_healthy
restart: "no"
volumes:
- "./scripts:/scripts"
environment:
PGPASSWORD: "<my-secret>"
entrypoint: "psql -U postgres -d postgres -h postgresql -f /scripts/create-db.sql"
my-business-service:
image: my.docker.registry/my-business-service:latest
depends_on:
cloud-broker:
condition: service_healthy
postgresql-setup:
condition: service_completed_successfully
根据 Docker Compose 的 compose-file documentation:
depends_on
- 表示服务之间的依赖关系。links
- Link 到另一个服务中的容器,并且 表达服务之间的依赖关系 与 depends_on 相同。
我不明白 linking 到其他容器的目的,所以两个选项之间的区别对我来说似乎仍然很困难。
如果有例子就容易多了,但是我找不到。
我注意到,当我 link 容器 B 和容器 A 时,容器 B 将 "pingable" 在容器 A 的 shell 中。
我运行ping B
在容器A的bash
里面得到了这样的结果(仅供参考,图片来自网络)
[2016 年 9 月更新]:此答案适用于 docker 撰写文件 v1(如下面的示例撰写文件所示)。对于 v2,请参阅@Windsooon 的其他答案。
[原回答]:
文档里说的很清楚了。 depends_on
decides the dependency and the order of container creation and links
不仅会这些,还会
Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.
例如,假设以下 docker-compose.yml
文件:
web:
image: example/my_web_app:latest
links:
- db
- cache
db:
image: postgres:latest
cache:
image: redis:latest
使用 links
,web
中的代码将能够使用 db:5432
访问数据库,假设端口 5432 在 db
图像中公开。如果使用 depends_on
,这是不可能的,但容器的启动顺序是正确的。
此答案适用于 docker-compose 版本 2,它也适用于 版本 3
使用depends_on时仍然可以访问数据。
如果你查看 docker 文档 Docker Compose and Django,你仍然可以像这样访问数据库:
version: '2'
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
链接和depends_on有什么区别?
链接:
当您为数据库创建容器时,例如:
docker run -d --name=test-mysql --env="MYSQL_ROOT_PASSWORD=mypassword" -P mysql
docker inspect d54cf8a0fb98 |grep HostPort
你可能会发现
"HostPort": "32777"
这意味着您可以从本地主机端口 32777(容器中为 3306)连接数据库,但每次重新启动或删除容器时此端口都会更改。因此,您可以使用链接来确保您始终连接到数据库,而不必知道它是哪个端口。
web:
links:
- db
depends_on:
我从 Giorgio Ferraris 找到了一个不错的博客 Docker-compose.yml: from V1 to V2
When docker-compose executes V2 files, it will automatically build a network between all of the containers defined in the file, and every container will be immediately able to refer to the others just using the names defined in the docker-compose.yml file.
和
So we don’t need links anymore; links were used to start a network communication between our db container and our web-server container, but this is already done by docker-compose
更新
depends_on
表示服务之间的依赖关系,有两个作用:
docker-compose up
将按依赖顺序启动服务。在下面的例子中,db和redis会先于web启动。docker-compose up SERVICE
将自动包含 SERVICE 的依赖项。在下面的例子中,docker-compose up web也会创建并启动db和redis。
简单示例:
version: '2'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
Note: depends_on will not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.
post 在 links
选项被弃用后需要更新。
基本上,不再需要 links
,因为它的主要目的是通过添加环境变量使另一个容器可以访问,隐式包含在 network
中。当容器放置在同一个网络中时,它们可以使用容器名称和其他别名作为主机相互访问。
对于 docker run
,--link
也已弃用,应由自定义网络代替。
docker network create mynet
docker run -d --net mynet --name container1 my_image
docker run -it --net mynet --name container1 another_image
depends_on
表示开始顺序(和隐含的图像拉取顺序),这是 links
.
我认为这个问题的答案需要根据 v1.27.0 中首次引入的新 Docker compose 规范进行更新,该规范现在允许 depends_on
的长格式:
https://github.com/compose-spec/compose-spec/blob/master/spec.md#long-syntax-1
在这个长格式中,您可以指定要等待服务启动、正常运行或成功完成。
Docker 如果您在该服务上生成 health_check
,则 compose 知道该服务是健康的:
https://github.com/compose-spec/compose-spec/blob/master/spec.md#healthcheck
我建议阅读文档中的示例以了解更多详细信息,请参阅上面的链接!
举个简单的例子,这是我在用于集成测试的撰写文件中使用的内容:
services:
cloud-broker:
image: my.docker.registry/activemq-artemis:latest
healthcheck:
test: ["CMD-SHELL", "wget http://localhost:8161/ --delete-after --tries=3 2> /dev/null"]
interval: 10s
timeout: 5s
retries: 5
postgresql:
image: postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
environment:
POSTGRES_PASSWORD: "<my-secret>"
POSTGRES_USER: "postgres"
POSTGRES_DB: "postgres"
# This service calls a script to create an empty database and the service-user
postgresql-setup:
image: postgres
depends_on:
postgresql:
condition: service_healthy
restart: "no"
volumes:
- "./scripts:/scripts"
environment:
PGPASSWORD: "<my-secret>"
entrypoint: "psql -U postgres -d postgres -h postgresql -f /scripts/create-db.sql"
my-business-service:
image: my.docker.registry/my-business-service:latest
depends_on:
cloud-broker:
condition: service_healthy
postgresql-setup:
condition: service_completed_successfully