Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 334
feerlay Avatar asked Feb 18 '18 11:02

feerlay


People also ask

What does Docker compose override do?

The docker-compose. override. yml is the configuration file where you can override existing settings from docker-compose. yml or even add completely new services.

Does Docker compose expose ports?

Docker Compose exposes all specified container ports, making them reachable internally and externally from the local machine. Once again, in the PORTS column, we can find all the exposed ports. The value to the left of the arrow shows the host address where we can reach the container externally.

Does Docker compose command override entrypoint?

All About Docker Compose Override Entrypoint Entrypoint helps use set the command and parameters that executes first when a container is run. In fact, the command line arguments in the following command become a part of the entrypoint command, thereby overriding all elements mentioned via CMD.

Does Docker compose override Dockerfile?

Docker-compose command doesn't override Dockerfile CMD.


2 Answers

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)

like image 167
Rach Sharp Avatar answered Oct 19 '22 07:10

Rach Sharp


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.

like image 4
Fl_ori4n Avatar answered Oct 19 '22 09:10

Fl_ori4n