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

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

问题描述

根据Docker Compose的



对不起,我的英文不好:(

解决方案

此答案适用于docker-compose 版本2



当您使用depends_on 时,您仍然可以访问数据



如果您查看docker文档 Docker Compose和Django ,你sti可以这样访问数据库:

  version:'2'
services:
db:
image:postgres
web:
build:。
命令: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 

码头检查d54cf8a0fb98 | grep HostPort

您可能会发现

 HostPort:32777

这意味着您可以从本地主机端口32777(容器中的3306)连接数据库。但是,每次重新启动或卸下容器时,此端口都将更改。因此,您可以使用链接确保您始终连接到数据库,而不必知道哪个端口。

  web:
链接:
- db

depends_on



我发现一个很好的博客来自giorgio ferraris Docker-compose.yml:从V1到V2


当docker-组合执行V2文件,它将自动在文件中定义的所有容器之间建立一个网络,每个容器将立即使用docker-compose.yml文件中定义的名称来引用其他的。



所以我们不需要链接了;链接用于启动我们的数据库容器和我们的Web服务器容器之间的网络通信,但是这已经通过docker-compose




更新



depends_on



服务之间的依赖关系有两个效果:




  • docker-compose up will按照依次启动服务。在以下示例中,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




注意:depends_on不会等待db和redis在启动Web之前准备就绪直到他们开始。如果您需要等待服务准备就绪,请参阅控制启动顺序以了解有关此问题的更多信息和解决问题的策略。



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. Please help me get to understand those twos.

Thank you in advance!

Edit: So I noticed, when I link container B with container A then container B will be (what is the word?) 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)

Sorry for my broken English :(

解决方案

This answer is for docker-compose version 2

You can still can access the data when you use depends_on

If you look at docker docs Docker Compose and Django, you still can access the database like this:

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

What is the different between links and depends_on

links:

When you create a container for database, for example:

docker run -d --name=test-mysql --env="MYSQL_ROOT_PASSWORD=mypassword" -P mysql

docker inspect d54cf8a0fb98 |grep HostPort

And you may find

"HostPort": "32777"

This means you can connect the database from your localhost port 32777(3306 in container).But this port will change every time you restart or remove the container. So you can use links to make sure you will always connect to the database and don't have to know which port it is.

web:
  links:
   - db

depends_on

I found a nice blog from giorgio ferraris Docker-compose.yml: from V1 to V2

When docker-compose execute 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.

And

So we don’t need links anymore; links was used to start a network communication between our db container and our web-server container, but this is already done by docker-compose

Update

depends_on

Express dependency between services, which has two effects:

  • docker-compose up will start services in dependency order. In the following example, db and redis will be started before web.
  • docker-compose up SERVICE will automatically include SERVICE’s dependencies. In the following example, docker-compose up web will also create and start db and redis.

Simple example:

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.

这篇关于docker_compose.yml中的链接和depends_on之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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