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.

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 us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build 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.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github