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 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 -
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 -
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 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. If you're using Compose standalone, use ...
Questions
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github