Extend service in docker-compose 3
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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
- Code Reusability: Reduces the duplication of configuration code.
- Maintainability: Simplifies updates across multiple containers if changes are necessary.
- 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, andvolumeswill be reused unless specifically overridden in the extending service. - File Reference: The
fileoption 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
environmentandvolumeswill 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
fileis 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.
Related reading
- external-ip remains pending on controller-service after installation of Nginx Ingress Controller on AWS EKS
- External DNS Configure it in all namespaces
- failed calling webhook vingress.elbv2.k8s.aws
- Failed create pod sandbox rpc error code Unknown desc NetworkPlugin cni failed to set up pod network
- Externalising Spring Boot properties when deploying to Docker
- Failed to pull image pull access denied , repository does not exist or may require ''docker login''
- Failed to download OpenAPI error with Kubernetes deployment
- Failed to find data source Please deploy the application as per the deployment section of Structured Streaming + Kafka Integration Guide

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.