How to access host, from inside docker container in simple compose setup?

Hi there.
New to Docker and Devcontainers.

I’m trying to configure a MongoDB container and a development container. I would like:

  1. The app container to be able to communicate to MongoDB on localhost:27001
  2. I would also like to connect to MongoDB from the host machine, with a tool like MongoDB Compass.
  3. The setup should work on Windows and Mac OS.
  4. The app container should be able to connect to a service running on the host OS (does not work)

I have the following:

services:
  app:
    build:
      context: ..
      dockerfile: .devcontainer/Dockerfile
    volumes:
      - ..:/workspace:cached
    init: true

    # Overrides default command so things don't shut down after the process ends.
    command: sleep infinity

    # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
    network_mode: service:db
    # Uncomment the next line to use a non-root user for all processes.
    # user: node

  db:
    # Based on https://stackoverflow.com/q/67020325/3694288
    image: mongo:7
    restart: unless-stopped
    volumes:
      - mongodb-data:/data/db
      - ./mongo-scripts:/docker-entrypoint-initdb.d/
      - type: bind
        source: ./mongod.conf
        target: /etc/mongod.conf
    command: '--bind_ip_all --replSet rs0'

volumes:
  mongodb-data: null

How can I improve this configuration so that I can access the host from inside the app container (my process there is running on port 3001)? No matter what I try, I just get Connection refused inside the container.

update
I found a fix for the problem.If I updated the host service to listen on 127.0.0.1 instead of localhost, I could successfully connect with http://host.docker.internal:3001. I’m unsure why, but leaving it here if you are stuck with the same problem.