dockernginxdocker-compose

docker compose override a ports property instead of merging it


My docker compose configs look like this:

docker-compose.yml

version: '3.5'

services:
    nginx:
        ports:
            - 8080:8080

docker-compose.prod.yml

version: '3.5'

services:
    nginx:
        ports:
            - 80:80

Now, when I run command: docker-compose -f docker-compose.yml -f docker-compose.prod.yml up the nginx exposes on host machine two ports: 8000 and 80, because it merges ports properties:

version: '3.5'

services:
    nginx:
        ports:
            - 8080:8080
            - 80:80

Is there a way to override it? I want to expose only port 80.


Solution

  • This behaviour is documented at https://docs.docker.com/compose/extends/#adding-and-overriding-configuration

    For the multi-value options ports, expose, external_links, dns, dns_search, and tmpfs, Compose concatenates both sets of values

    Since the ports will be the concatenation of the ports in all your compose files, I would suggest creating a new docker-compose.dev.yml file which contains your development port mappings, removing them from the base docker-compose.yml file.

    As Nikson says, you can name this docker-compose.override.yml to apply your development configuration automatically without chaining the docker-compose files. docker-compose.override.yml will not be applied if you manually specify another override file (e.g. docker-compose -f docker-compose.yml -f docker-compose.prod.yml)