Docker

How to Understand the Difference Between “Links” and “Depends_on” in docker-compose.yml

In the application deployment procedures, the Docker containers execute as isolated processes in the system. Therefore, there is often a requirement to enable the communication of the containers to share the information from one to another.

This write-up covers the following content:

How to Understand the Difference Between “Links” and “Depends_on” in docker-compose.yml?

The “links” and “depends_on” keywords are utilized in the Docker Compose file i.e., “docker-compose.yml” to establish the relationships between containers. However, they vary in the way of establishing relationships.

What is “links” in “docker-compose.yml” File?

The “links” keyword is utilized to link a container to the other container in the Compose file. It is such that it establishes a network connection between the linked/associated containers, enabling effective communication between them.

For instance, if there is a web application container that relies on the database container, the “links” keyword can be utilized to make sure that the web application is able to invoke the database.

Example: Applying “links” in “docker-compose.yml” File

Overview of the following demonstration that applies “links” in the discussed file:

services:
 db:
    image: postgres:latest
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - 5432:5432
  webapplication:
    images: web-app:latest
    ports:
      - 8080:8080
    links:
      - db

In this file, the “webapplication” service links to the “db” service, allowing it (the former service) to connect to the database.

Note: To build the “docker-compose.yml” file, the “docker compose up” cmdlet is used.

What is “depends_on” in the “docker-compose.yml” File?

On the other hand, the “depends_on” keyword specifies the order in which services are initiated. It is such that it does not establish a network connection between the services, but rather makes sure that a particular service initiates before another.

For instance, if the web application container relies on a database container, as discussed, the “depends_on” keyword makes sure that the database initiates before the web application since it is a prerequisite on which the other container i.e., web application depends.

Example: Applying “depends_on” in “docker-compose.yml” File

This demonstration uses the “depends_on” keyword to link the containers:

version: '3'

services:
  webapplication:
    build: .
    depends_on:
      - db
  db:
    image: postgres

According to this file code, it can be visualized that the “webapplication” service relies on the “db” service. Therefore, the “depends_on” keyword makes sure that the database initiates before the web application.

Conclusion

In the “docker-compose.yml” file, the “links” keyword links/associates the containers and allows the communication between them, whereas the “depends_on” keyword specifies the order in which the services initiate. However, both keywords establish the relationship between the containers. This blog stated the differences between “links” and “depends_on” in docker-compose.yml.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.