Problem running wagtail(CMS for django) and postgresql in Docker Compose

33 Views Asked by At

File docker-compose.yml:

version: '3.9'
services:
  wagtail:
    build: ./wagtail/ra_pegas
    ports:
      - "8000:8000"
    volumes:
      - ./wagtail/ra_pegas:/app
    links:
      - postgres
  postgres:
    image: postgres:16.2
    ports:
      - "8001:5432"
    command:
      - "postgres"
      - "-c"
      - "psql"
      - "-c"
      - "CREATE USER postgres WITH PASSWORD 'postgres' CREATEDB;"
      - "-c"
      - "CREATE DATABASE postgres WITH OWNER postgres;"
      - "-c"
      - "GRANT ALL PRIVILEGES ON DATABASE postgres TO postgres;"
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - db:/var/lib/postgresql/data
volumes:
  db:
    driver: local

Database settings in wagtail:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "postgres",
        "USER": "postgres",
        "PASSWORD": "postgres",
        "HOST": "postgres",
        "PORT": "8001",
    }
}

When I run a project with host = postgres (postgresql container name), i get an error:

wagtail-1  | django.db.utils.OperationalError: could not translate host name "postgres" to address: Name or service not known
wagtail-1  | 
wagtail-1 exited with code 1

If i change host to 127.0.0.1 or localhost:

wagtail-1   | django.db.utils.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
wagtail-1   |   Is the server running on that host and accepting TCP/IP connections?
wagtail-1   | connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
wagtail-1   |   Is the server running on that host and accepting TCP/IP connections?

Also if I change the username, password or database name in the settings absolutely nothing changes, but command block in docker-compose.yml necessary because without it nothing works

When I tried to run the project without docker compose, or with docker compose but with sqlite instead of postgresql everything worked great

I also tried using networks but that also did not give any result

0

There are 0 best solutions below