How to redirect traffic to a container's port when the container uses a custom network

Hello everyone,

I’d like to use docker-compose to deploy an application that will use many services. It seems a good idea to use multiple custom networks so that each service can only communicate with the relevant related services.
One of the service will run a web application on port 8000. I’d like this application to be available on the port 8000 of the host, (but only on localhost).

First, without using custom networks, I don’t understand why, out of the two following docker-compose.yml, one works and the other doesn’t.

Version A:

version: "3.7"
services:
  the-application:
    build:
      context: the-application
    ports:
      - 127.0.0.1:8000:8000

This doesn’t work. The connection is not refused. But it seems to hang at the ACK stage. (By the way, I tested the application in the container, the application works, it is set to serve requests from any interface or ip address, it is not a case of the application only accepting requests from localhost, the application is a uvicorn app and I set the host option to “0.0.0.0” so it accepts requests from anywhere).

However the following version works:

Version B:

version: "3.7"
services:
  the-application:
    network_mode: bridge
    build:
      context: the-application
    ports:
      - 127.0.0.1:8000:8000

So adding network_mode: bridge to the service definition makes the port redirection effective. I find it strange because I read everywhere that bridge is the default.

Now, the version with the custom network (it’s actually the version I started with, since that’s what I’m interested in).

Version C:

version: "3.7"
networks:
  the-network:
    driver: bridge
services:
  the-application:
    build:
      context: the-application
    ports:
      - 127.0.0.1:8000:8000
    networks:
      - the-network

This doesn’t work. The result is the same as with version A.
Is there a difference between “bridge driver” and “bridge network mode” ?

These are minimal examples. I’d like to use many networks at a later point. And I’m going to need to be able to reach some of the containers from the host, on localhost. Is it something that can be done using custom networks ? If so, how?

If it matters, I use Docker version 20.10.7 on a Ubuntu 20.04 dedicated server.

Any help would be appreciated. Thank you.