Search code examples
dockerdocker-composedocker-network

How to join the default bridge and user defined bridge with docker-compose v3?


I have a docker-compose yml with several services which are in a user defined network (on bridge mode). I want to connect one on my service on the default docker bridge in order to expose a port to the outside world.

To summarize, there is a concrete example of what I want to do:

services:
  service1:
    networks:
      - my_network

  service2:
    networks:
      - my_network
      - bridge
    ports:
      8080:8080

As described here I tried the following, but it resulted in this error:

ERROR: for app network-scoped alias is supported only for containers in user defined networks.

networks:
  my_network:
    driver: bridge
  bridge:
    external: true

I also tried the solution proposed in How to join the default bridge network with docker-compose v2?, but you can't mix the keyword network_mode: bridge and the keyword networks in a docker compose.

I also tried to remove the reference to the default bridge network in the docker-compose.yml, and add my service2 container to the default bridge after starting the services, such as:

docker-compose up
docker network connect bridge service2

This approach works and I can access my exposed port on service2 from the outside world, I end up with :

docker inspect service2


"Networks": {
    "bridge": {
        ...
     },
     "my_network": {  
          ...
      }
}

But I don't want to execute the docker network connect bridge service2 each time I want to start my services.

I know that my question is very similar with this one: How to use the host network, and any other user-defined network together in Docker-Compose?, except that I don't want to use the host mode.

Is there any way to connect a service on a user defined network and the default bridge at the same time?


Solution

  • I think you dont have to be on the default bridge network to publish ports from container to your machine.