10

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Anthony
  • 138
  • 3
  • 10

1 Answers1

0

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

Wie
  • 422
  • 4
  • 20
  • 5
    Yes, you are right, thank you for your answer, you solved my problem. But what if someone still wants to connect a service to both a user defined bridge and the default bridge ? – Anthony Apr 17 '19 at 12:06
  • 1
    As Anthony said, this case is exactly what i need. I need my services to be in both - default one and user defined. – Payalord Sep 06 '20 at 02:53
  • 3
    @Anthony, it looks like that is not possible, see https://github.com/docker/compose/issues/3012 and https://github.com/docker/compose/issues/3476 – Stefan Sep 13 '20 at 19:34
  • wow, you posted what should have been a comment as an answer. – lasec0203 Jul 27 '22 at 01:02