Docker

What is the Difference Between “links” and “depends_on” in Docker Compose?

Docker Compose is a special tool for specifying and executing numerous containers using the Docker Compose file. It is a YAML file that defines a set of Docker containers to run them together as a single application or service. In a Docker Compose file, users may need to use “links” and “depends_on” to define the relationships between the different containers. These features allow users to define how the containers interact with each other and verify that they are started in the proper sequence.

This study will explain:

What are “links” in Docker Compose?

The “links” is a feature in Docker Compose that permits users to link one container to another so that they can communicate with each other. When the user defines a link between two containers, Docker Compose sets up a secure tunnel between them and passes information about the linked container as environment variables to the target container.

For example, we can link a Postgres container to our web application container using “links” in our docker-compose.yml file.

version: "3.9"

services:
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example_password
      POSTGRES_USER: example_user
    volumes:
      - db_data:/var/lib/postgresql/data/

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
    links:
      - db

volumes:
  db_data:

 
In this example, we have two services defined: db and web. The db service is a Postgres container, and the “web” service is a web application container that depends on the db container.

To link the two containers together, we use the “links” option under the web service. For this, specify “db” as the name of the container to link to, which tells Docker to set up networking between the two containers.

What is “depends_on” in Docker Compose?

The “depends_on” is a keyword in Docker Compose that defines the dependence connections between services. It is used to specify which services must be started before others, as well as the order in which they must be started. When one service depends on another service, Docker Compose ensures that the dependent service is started before the dependent service.

For example, if we have a web service that depends on a database service, we can utilize the “depends_on” to make sure that the web service will not start until the database service is ready.

version: "3.9"

services:
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example_password
      POSTGRES_USER: example_user
    volumes:
      - db_data:/var/lib/postgresql/data/

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      db:
        condition: service_healthy

volumes:
  db_data:

 
In the above, to ensure that the “web” service does not start until the “db” service is ready, use the “depends_on” option under the web service. For this, specify “db” as the name of the service to depend on, and add a condition of “service_healthy”. This tells Docker to wait for the “db” service to be in a healthy state before starting the web service.

Primary Difference Between “links” and “depends_on” in Docker Compose

The primary difference between “links” and “depends_on” is that “links” establish a link between two containers, whereas the “depends_on” specifies the sequence in which services need to be launched/started. The “links” allows a container to access another container’s services by setting an environment variable containing the linked container’s name and IP address. It can access the services of the linked container by name. On the other hand, “depends_on” ensures that the specified services are started before the service that depends on them. It does not establish communication between containers.

Conclusion

The “links” is a feature in Docker Compose that permits users to link one container to another and enable communication between them. On the other hand, “depends_on” is a keyword in Docker Compose that is used to specify the order in which the services must start and stop. This article has explained about “links” and “depends_on” and their differences.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.