What is the difference between links and depends_on in docker_compose.yml?

Better Stack Team
Updated on April 14, 2023

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 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.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

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