Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

I am trying to bind django app with postgresql database.

version: "3.9"

services:
  db:
    image: postgis/postgis:13-3.1
    restart: always
    expose:
      - "5432"
    ports:
      - "5432:5432"
    volumes:
      - postgresql-data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: postgres
      POSTGRES_DB: mydb
      POSTGRES_PASSWORD: postgres
      POSTGRES_HOST: localhost

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

volumes:
  postgresql-data:

With just a Compose file nothing is running. So, what else did you execute to start this?

If you’re asking if the web container can access the database in the db container using localhost:5432, then: no, it cannot. With the above docker-compose.yml file the database will be running in another container, with DNS-name db, so Django will need to connect to db:5432. (And ports is not needed for that; that is only required when you want to access the database from the host instead of from another container in the same Compose setup.)

2 Likes