1. Overview
1.概述
Docker containers run as isolated processes in our system. However, we usually want them to communicate and transfer information from one to another.
Docker容器在我们的系统中作为孤立的进程运行。然而,我们通常希望它们能够相互交流和传输信息。
In this tutorial, we’ll see the difference between Docker links and depends_on with some practical examples using Docker Compose.
在本教程中,我们将通过一些使用Docker Compose的实际例子来了解Docker links和depends_on之间的区别。
2. Docker Compose depends_on
2.Docker Composedepends_on
depends_on is a Docker Compose keyword to set the order in which services must start and stop.
depends_on是一个Docker Compose关键字,用于设置服务必须启动和停止的顺序。。
For example, suppose we want our web application, which we’ll build as a web-app image, to start after our Postgres container. Let’s have a look at the docker-compose.yml file:
例如,假设我们希望我们的 Web 应用程序(我们将作为 web-app 镜像构建)在我们的 Postgres 容器之后启动。让我们看一下docker-compose.yml文件。
services:
db:
image: postgres:latest
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5432:5432
web-app:
image: web-app:latest
ports:
- 8080:8080
depends_on:
- db
Docker will pull the images and run the containers based on the given dependencies. So, in this case, the Postgres container is the first in the queue to run.
Docker会根据给定的依赖关系来拉取镜像并运行容器。因此,在这种情况下,Postgres容器是队列中第一个要运行的。
However, there are limitations because depends_on doesn’t explicitly wait for dependencies to be ready.
然而,由于depends_on没有明确地等待依赖关系的准备,所以存在一些限制。
Let’s imagine our web application needs to run some migrations scripts at startup. If the database isn’t accepting connections, although the Postgres service has started correctly, we can’t execute any script.
假设我们的Web应用程序需要在启动时运行一些迁移脚本。如果数据库不接受连接,尽管Postgres服务已经正确启动,我们还是不能执行任何脚本。
However, we can avoid this if we control the startup or shutdown order using specific tools or our own managed scripting.
然而,如果我们使用特定的工具或自己的托管脚本来控制启动或关闭顺序,就可以避免这种情况。
3. Docker Compose links
3.Docker Compose的链接
links instructs Docker to link containers over a network. When we link containers, Docker creates environment variables and adds containers to the known hosts list so they can discover each other.
links指示Docker通过网络链接容器。当我们链接容器时,Docker会创建环境变量,并将容器添加到已知的主机列表中,以便它们能够发现对方。
We’ll check out a simple Docker example running a Postgres container and link it to our web application.
我们将查看一个简单的Docker例子,运行一个Postgres容器,并将其与我们的网络应用连接。
First, let’s run our Postgres container:
首先,让我们运行我们的Postgres容器。
docker run -d --name db -p 5342:5342 postgres:latest
Then, we link it to our web application:
然后,我们将其链接到我们的网络应用。
docker run -d -p 8080:8080 --name web-app --link db
Let’s convert our example to Docker Compose:
让我们把我们的例子转换为Docker Compose。
services:
db:
image: postgres:latest
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5432:5432
web-app:
images: web-app:latest
ports:
- 8080:8080
links:
- db
4. Docker Compose network
4.Docker组件网络
We can find Docker links still in use. However, Docker Compose deprecates it since version 2 because of the introduction of the network.
我们可以发现Docker links仍然在使用。然而,由于引入了网络,Docker Compose从第二版开始就废弃了它。。
This way, we can link applications with complex networking, for example, overlay networks.
这样,我们就可以将应用程序与复杂的网络联系起来,例如,覆盖式网络。
However, in a standalone application, we can typically use a bridge as the default when we don’t specify a network.
然而,在独立的应用程序中,当我们不指定网络时,我们通常可以使用桥作为默认。
Let’s remove links and replace it with the network while also adding a volume and environment variables for the database:
让我们删除links,用network取代它,同时也为数据库添加一个卷和环境变量。
services:
db:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5432:5432
volumes:
- db:/var/lib/postgresql/data
networks:
- mynet
web-app:
image:web-app:latest
depends_on:
- db
networks:
- mynet
ports:
- 8080:8080
environment:
DB_HOST: db
DB_PORT: 5432
DB_USER: postgres
DB_PASSWORD: postgres
DB_NAME: postgres
networks:
mynet:
driver: bridge
volumes:
db:
driver: local
5. Difference Between Docker links and depends_on
5.Docker links和depends_on之间的区别
Although they involve expressing dependencies, Docker links and depends_on have different meanings.
虽然涉及到表达依赖关系,但Docker links和depends_on有不同的含义。
While depends_on indicates the order in which the services must start and stop, the links keyword handles the communication of containers over a network.
虽然depends_on表示服务必须启动和停止的顺序,但links关键字处理容器在网络上的通信。
Furthermore, depends_on is a Docker Compose keyword, while we can similarly use links as a legacy feature of Docker.
此外,depends_on是Docker Compose的一个关键词,而我们同样可以使用links作为Docker的一个传统功能。
6. Conclusion
6.结语
In this article, we’ve seen the difference between Docker links and depends_on with Docker Compose examples.
在这篇文章中,我们已经通过Docker Compose的例子看到了Docker links和depends_on的区别。
depends_on tells Docker the order of running containers while links, or the network in newer versions of Docker Compose, sets a connection for containers over a network.
depends_on告诉Docker运行容器的顺序,而links,或Docker Compose较新版本中的network则为容器在网络上设置一个连接。
As always, we can find the source docker-compose.yml files of our examples over on GitHub.
一如既往,我们可以在GitHub上找到我们例子的源docker-compose.yml文件。