--- 
canonical: 'https://mwop.net/blog/2025-04-04-docker-compose-override.html'
title: 'Overriding values when merging Docker Compose config'
author: "[Matthew Weier O'Phinney](https://mwop.net)"
created: '2025-04-04T08:32:57-05:00'
updated: '2025-04-04T08:32:57-05:00'
tags:
  - docker
  - til

---
I've recently started using the ability to merge Docker Compose files, as it reduces repetition when having different configuration for production from development.
This also helps reduce cases where configuration gets updated in one, but not the other, leading to issues when debugging why something runs in one environment but not another.

Generally, when overriding something in development, I'm overriding volumes and environment variables.
I started also using the fact that the `env_file` directive can take a _list_ of files, which allows segregating environment variables by context, allowing re-use of _specific_ variables between services, so that I don't go polluting services with ENV variables that it doesn't use.

The problem I ran into, however, is that on a particular service, I have production env variable files in a secure, global location, but in development, I use a single `.env` file.
When these get merged, because the value is a list, I was getting complaints in development that one or more env files did not exist.

Since I knew I wanted to keep _just_ the one entry in development, I needed a way to tell Compose to use _only_ the value in that file.

Turns out [this is well supported via the `!override` directive](https://docs.docker.com/reference/compose-file/merge/#replace-value):

```yaml
# In my development compose YAML:
services:
    app:
        env_file: !override
            - .env
```


