docker compose override a ports property instead of merging it

14,375

Solution 1

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)

Solution 2

it isn't possible a the moment but I found quite good way to fix this issue using the command yq. You need to remove the ports from the original file.

Example:
Be careful this command will remove the nginx ports from your current docker-compose.yml (because of the -i option)

yq e -i 'del(.services.nginx.ports)' docker-compose.yml

You can execute this command on your deployment script or manually before your docker-compose up -d

There's also an open issue on docker-compose, that you may want to check once in a while.

Solution 3

Just keep the docker-compose.yml super simple and add the ports in another file docker-compose.develop.yml, then run it like docker-compose -f docker-compose.yml -f docker-compose.develop.yml up. This way you can separate it from your docker-compose.override.yml file.

So you will three files:

|- docker-compose.yml # no ports specified
|- docker-compose.override.yml # ports 8080:8080
|- docker-compose.develop.yml #ports 80:80

Refer to this post for longer explanation: https://mindbyte.nl/2018/04/04/overwrite-ports-in-docker-compose.html

Share:
14,375

Related videos on Youtube

feerlay
Author by

feerlay

Updated on September 16, 2022

Comments

  • feerlay
    feerlay over 1 year

    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.

  • Rach Sharp
    Rach Sharp about 6 years
    This doesn't solve their issue of the prod configuration receiving the concatenation of ports in the original and override file.
  • Fabio Formosa
    Fabio Formosa about 4 years
    @RachSharp, he suggested to remove ports from docker-compose.yml and put them only in docker-compose.override.yml. So when you concat docker-compose.yml and docker-compose.prod.yml`, you'll get only prod ports.
  • Rach Sharp
    Rach Sharp about 4 years
    Bit of a stretch, doesn't mention anything about removing ports from the docker-compose.yml or how to use port 8080 when not running on prod