Docker

Write an awesome doc for Docker, Docker Compose, and everything else in this domain. You can find invaluable examples and explanations here. I am trying to keep it up to date and relevant too. BTW if you also have a cool idea, or example open a PR/issue/discussion.

View on GitHub

Docker Compose Service Configuration Merging

Solution:

Original Compose files
compose.ymlcompose-expose-port.yml
```yml services: app: build: . environment: PORT: 3000 network_mode: host ``` ```yml services: app: build: . ports: - 3000:3000 ```
  1. Remove network_mode: host from the default Compose file:
    services:
      app:
        build: .
        environment:
          PORT: 3000
      ports:
        - 3000:3000
    
  2. Create separate Compose files for network_mode: host, e.g. compose-host.yml:
    services:
      app:
        network_mode: host
    
  3. Then you can merge them like this: docker compose -f compose.yml -f compose-host.yml up --build -d.

    Or you can use docker compose up --build -d to create a network and expose the ports.

[!NOTE]

We do not care about the ports in the default compose file when we merge it with the compose-host.yml since it will be discarded by Docker. And if we run it on its own it will export the port as it should. So in short we flipped the script by changing how networking is handled.