docker compose override a ports property instead of merging it docker compose override a ports property instead of merging it nginx nginx

docker compose override a ports property instead of merging it


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)


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.


I've faced the same problem. The proposed solution with docker-compose.override.yml sounds pretty well and is also an official one.

Although for some of my own projects I've applied the erb template engine to make docker-compose.yml.erb file compile for multiple environments. In short I use:

COMPOSE_TEMPLATE_ENV=production erb docker-compose.yml.erb > docker-compose.ymlCOMPOSE_TEMPLATE_ENV=production erb docker-compose.yml.erb > docker-compose-production.yml

And then I can use the ENV['COMPOSE_TEMPLATE_ENV'] in my template and also the syntax of ERB, so only one file to configure and no worries about piplening them properly. Here's the short post article I've written about it