Is it possible to use both network default and outside external in docker-compose?

I have two services:

  • service A: It like a database service

  • service B: It like backend service

I want, service B can request to outside World (like curl google.com), and also connect to service A using alias (or something).
Because, I don’t want to public ip of service A to outside.
Is it posibile?

docker-compose.yml seem like that:

version: "3"

services:
  serviceB:
    build: ./serviceB
    networks:
      - outside
      - default
  serviceA:
    build: ./serviceA
    networks:
      - default

networks:
  outside:
    external: true

As long as your don’t expost ports explicitly, none of your services will expose a port to the outside. Currently none of your services would be accessible from the outside / host.

I think that you might have mixed up what an external network is. It does not mean that it’s your hosts network. An external network is a normal docker network, which already exists when you start your docker-compose file. This means that it won’t get created / destroyed when starting / stopping your compose file.

All you need is a network, like this:

services:
  serviceA:
    networks:
      - mynet

  serviceB:
    networks:
      - mynet

networks:
  mynet:

Now service A and service B can talk to eachother via their name (serviceA / serviceB)

A bridged or overlay network does not have a public ip - they are all private to the network.
Please share the result of docker network inspect outside. Is this a macvlan or an alias to the host network?