1
votes

I am running a django/postgresql in docker container. When run docker-compose.yml, the postgresql service will start and listen for a moment then it will be shut-down with LOG:

"listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" then "database system was shut down".

I have read it somewhere that it is receiving a postmaster fast shutdown request but I don't know how to solve this. I have tried to change ports and other PostgreSQL env variables without success.

Here is my .ylm for docker-compose

version: '3'

volumes:
  postgres_data_local: {}
  postgres_backup_local: {}

services:
   django:
     build:
       context: .
       dockerfile: ./compose/local/django/Dockerfile
     depends_on:
       - postgres
       - redis
    volumes:
      - .:/app
    env_file: .env
    ports:
      - "8000:8000"
    command: /start.sh

postgres:
  image: postgres:10.1-alpine 
  build:
    context: .
    dockerfile: ./compose/production/postgres/Dockerfile
  volumes:
    - postgres_data_local:/var/lib/postgresql/data
    - postgres_backup_local:/backups
  env_file: .env

redis:
  image: redis:3.0
  ports:
    - '6379:6379'

My .env file is somehow like

# PostgreSQL conf
POSTGRES_PASSWORD=p3wrd
POSTGRES_USER=postgres
POSTGRES_DB=postgres
POSTGRES_HOST=127.0.0.1 #have tried localhost, 0.0.0.0 etc
POSTGRES_PORT=5432

DATABASE_URL= postgresql://postgres:[email protected]:5432/postgres

# General settings
READ_DOT_ENV_FILE=True
SETTINGS_MODULE=config.settings.test
SECRET_KEY=Sup3rS3cr3tP@22word
DEBUG=True
ALLOWED_HOSTS=*

# URL for Redis
REDIS_URL=redis://127.0.0.1:6379
1

1 Answers

2
votes

Why are you setting POSTGRES_HOST to 127.0.0.1 or variants thereof? That means "localhost", which in a container means "the local container". Postgres isn't running inside the django container so that won't work.

Since you're using docker-compose, your containers are all running in a user-defined network. This means that Docker maintains a DNS server for you that maps service names to container ip addresses. In other words, you can simply set:

POSTGRES_HOST=postgres
DATABASE_URL= postgresql://postgres:p3wrd@postgres:5432/postgres