What is the difference between links and depends_on in docker_compose.yml?
Both links
and depends_on
are used in a Docker Compose file (docker-compose.yml
) to define relationships between containers. However, they differ in the way they establish these relationships.
Links
links
is used to link a container to another container in the same Compose file. It creates a network connection between the linked containers, allowing them to communicate with each other. For example, if you have a web application container that depends on a database container, you can use links
to ensure that the web application can access the database.
Here's an example of using links
in a Compose file:
version: '3'
services:
web:
build: .
links:
- db
db:
image: postgres
In this example, the web
service links to the db
service, allowing it to connect to the Postgres database.
depends_on
depends_on
, on the other hand, is used to define the order in which services are started. It does not create a network connection between the services, but rather ensures that one service starts before another. For example, if you have a web application container that depends on a database container, you can use depends_on
to ensure that the database is started before the web application.
Here's an example of using depends_on
in a Compose file:
version: '3'
services:
web:
build: .
depends_on:
- db
db:
image: postgres
In this example, the web
service depends on the db
service, ensuring that the database is started before the web application.
Summary
In summary, links
is used to link containers and enable communication between them, while depends_on
is used to define the order in which services are started.
-
How to view log output using docker-compose run?
You can start Docker compose in detached mode and attach yourself to the logs of all containers later. If you're done watching logs you can detach yourself from the logs output without shutting dow...
Questions -
How to make Docker Compose wait for container X before starting Y?
You can make Docker Compose wait for a container X to be fully up and running before starting container Y by using the depends_on option in your docker-compose.yml file. The depends_on option allow...
Questions -
How to restart a single container with docker-compose?
To restart a single container using Docker Compose, you can use the docker-compose restart command, followed by the name of the service you want to restart. Here's an example: docker-compose restar...
Questions -
What's the difference between Docker Compose and Kubernetes?
Docker Compose and Kubernetes are both container orchestration tools, but they differ in several ways: Scope and complexity Docker Compose is a simpler tool designed to manage a single Docker host ...
Questions
We are hiring.
Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.
Help us in making the internet more reliable.

Help us with developer education and get paid.

Reliability is the
ultimate feature
Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.
Explore Better Stack
