The Purpose of the `depends_on` Option in Docker Compose

The depends_on option in Docker Compose is used to specify dependencies between services. It allows you to define the order in which services should be started and stopped, ensuring that dependent services are available before a service attempts to start. This is particularly useful in multi-container applications where certain services rely on others to function correctly.

1. Overview of `depends_on`

When you define a service in a docker-compose.yml file, you can use the depends_on option to indicate that the service depends on one or more other services. Docker Compose will ensure that the dependent services are started before the service that declares the dependency.

2. Basic Usage of `depends_on`

The syntax for using depends_on is straightforward. Here’s an example of a docker-compose.yml file that defines two services: a web application and a database:

version: '3.8'

services:
web:
image: nginx:latest
ports:
- "8080:80"
depends_on:
- db

db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: mydb

In this example:

  • The web service depends on the db service.
  • When you run docker-compose up, Docker Compose will start the db service first, followed by the web service.

3. Important Considerations

While depends_on ensures that the specified services are started in the correct order, it does not wait for the dependent services to be "ready." For example, in the above configuration, Docker Compose will start the MySQL database container, but it does not guarantee that the database is fully initialized and ready to accept connections before starting the Nginx web server.

Handling Service Readiness

To handle service readiness, you may need to implement additional logic in your application or use tools like wait-for-it or wait-for scripts. These tools can help ensure that a service waits for another service to be fully ready before proceeding.

4. Example with Service Readiness

Here’s an example of how you might modify the web service to wait for the db service to be ready:

version: '3.8'

services:
web:
image: nginx:latest
ports:
- "8080:80"
depends_on:
- db
command: /bin/sh -c "while ! nc -z db 3306; do sleep 1; done; nginx -g 'daemon off;'"

db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: mydb

In this example:

  • The command for the web service uses a shell command to check if the MySQL database is ready by attempting to connect to it on port 3306.
  • Once the database is ready, the Nginx server starts.

5. Conclusion

The depends_on option in Docker Compose is a valuable feature for managing service dependencies in multi-container applications. By specifying the order in which services should start, you can ensure that your application components are initialized correctly. However, it is important to remember that depends_on does not handle service readiness, so additional measures may be necessary to ensure that dependent services are fully operational before starting dependent services.