docker-compose
service extension
Docker
container orchestration
DevOps

Extend service in docker-compose 3

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Understanding the Extend Service Feature in Docker Compose 3

Docker Compose is a tool that enables users to define and manage multi-container Docker applications. Within Docker Compose version 3, there's a highly useful feature: the ability to extend services. This article delves into the details of the extend feature and how it can be leveraged to build reusable, efficient configurations.

What is the Extend Service Feature?

The extend service feature in Docker Compose allows you to create reusable service configurations. By using the extends keyword, you can inherit configuration from other defined services or external YAML files. This promotes DRY (Don't Repeat Yourself) principles by minimizing redundant code and promoting reusability.

Key Benefits of Extend Service

  1. Code Reusability: Reduces the duplication of configuration code.
  2. Maintainability: Simplifies updates across multiple containers if changes are necessary.
  3. Consistency: Provides a consistent environment setup across different projects.

How to Use the Extend Service Feature

The functionality is invoked with the extends keyword in the docker-compose.yml file. Below is a sample to illustrate its usage:

  • ENVIRONMENT=production
  • "80:80"
  • LOG_LEVEL=debug
  • Service Inheritance: The target service inherits all configuration options from the referenced service in either the same or external file. All configurations such as image , environment , and volumes will be reused unless specifically overridden in the extending service.
  • File Reference: The file option is used to specify the file where the base service is defined. If not specified, Compose will look for the service in the same file.
  • List fields such as environment and volumes will be merged, not replaced. If you define new variables, they will be appended.
  • Scalar fields, like image , will be overridden if redefined.
  • File Context: If file is set, paths within the referenced file are relative to that file’s location.
  • Circular Extends: Define a service might not extend from a service that indirectly extends from it. Such configurations can lead to circular references.

Course illustration
Course illustration

All Rights Reserved.