Unable to access exposed container through localhost

Hi, I have the following docker compose:

volumes:
  dev-db-data:

networks:
  internal:

services:
  database:
    image: postgres:16-alpine
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - 5432:5432
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
    restart: unless-stopped
    volumes:
      - dev-db-data:/var/lib/postgresql/data
      - ./infrastructure/postgresql/initdb:/docker-entrypoint-initdb.d:ro
    networks:
      - internal

  mailpit:
    image: axllent/mailpit
    container_name: mailpit
    restart: unless-stopped
    ports:
      - 8025:8025
      - 1025:1025
    environment:
      MP_MAX_MESSAGES: 5000
      MP_SMTP_AUTH_ACCEPT_ANY: 1
      MP_SMTP_AUTH_ALLOW_INSECURE: 1
    networks:
      - internal

However, when I do docker compose up I’m unable to access postgres, or any other container, using localhost. But the connection do work if I use the internal container direct ip.

Honestly I have no clue on how to debug or fix this, did anyone had to deal with that?

Not, sure is relevant, but here is my routing table:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    600    0        0 wlp170s0
172.42.0.0      0.0.0.0         255.255.255.0   U     0      0        0 docker0
172.42.1.0      0.0.0.0         255.255.255.0   U     0      0        0 br-f3cd4b7b7da2
192.168.1.0     0.0.0.0         255.255.255.0   U     600    0        0 wlp170s0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0 

And if I do docker ps I can see:

CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS                    PORTS                                                      NAMES
153debcbdb22   postgres:16-alpine   "docker-entrypoint.s…"   36 minutes ago   Up 17 minutes             0.0.0.0:5432->5432/tcp                                     database
d32348d8a849   axllent/mailpit      "/mailpit"               36 minutes ago   Up 17 minutes (healthy)   0.0.0.0:1025->1025/tcp, 0.0.0.0:8025->8025/tcp, 1110/tcp   mailpit

Not 100% sure this is the cause but try stopping your firewall with sudo systemctl stop firewall. Sorry, i don’t have details or a fix but i think i had something like this happen once with a docker container, and the firewall was the issue, so i just temporarily stopped it.