5

I am trying to run my Django app (Nginx, Gunicorn) in docker.

But for request http://167.99.137.32/admin/ I have error: (full log https://pastebin.com/0f8CqCQM)

onnection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Address not available
    Is the server running on that host and accepting TCP/IP connections?

I was trying answers from Can't run the server on Django (connection refused) but didn't solve my problem

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'lk_potok_2',
        'USER': 'postgres',
        'PASSWORD': 'post222',
        'HOST': 'localhost',
        'PORT': 5432,
    },

docker-compose.yml

version: '3.9'

services:
  django:
    build: . # path to Dockerfile
    command: sh -c "gunicorn --bind 0.0.0.0:8000 potok.wsgi:application"
    volumes:
      - .:/project
      - static:/project/static
    expose:
      - 8000
    environment:
      - DATABASE_URL=postgres://postgres:post222@localhost:5432/lk_potok_2"
      - DEBUG=1

  db:
    image: postgres:13-alpine
    volumes:
      - pg_data:/var/lib/postgresql/data/
    expose:
      - 5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=post222
      - POSTGRES_DB=lk_potok_2

  nginx:
    image: nginx:1.19.8-alpine
    depends_on:
      - django
    ports:
      - "80:80"
    volumes:
      - static:/var/www/html/static
      - ./nginx-conf.d/:/etc/nginx/conf.d

volumes:
    pg_data:
    static:

nginx-conf.nginx

upstream app {
    server django:8000;
}

server {
    listen 80;
    server_name 167.99.137.32;

    location / {
        proxy_pass http://django:8000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /static/ {
        alias /var/www/html/static/;
    }
}

I was trying sudo systemctl start postgresql and sudo systemctl enable postgresql (the same error)

Alex
  • 562
  • 1
  • 6
  • 25

4 Answers4

16

The postgres database is no longer running at localhost. In your case (since you named the container db) it is db.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'lk_potok_2',
        'USER': 'postgres',
        'PASSWORD': 'post222',
        'HOST': 'db',
        'PORT': 5432,
    },

I don't really see why you would add this in here:

environment:
      - DATABASE_URL=postgres://postgres:post222@localhost:5432/lk_potok_2"

since you don't use it in your settings.py. But here it wil also have to be db instead of localhost.

--EDIT--

Explanation as why docker can recognise the other containers can be found here.

DrummerMann
  • 692
  • 4
  • 9
  • I replaced 'localhost' with 'db'. For this change I have error: django.db.utils.ProgrammingError: relation "django_session" does not exist http://167.99.137.32/admin/ – Alex Jan 08 '22 at 16:16
  • 1
    This means that the database connection now works. But that error is related to the absence of tables in your database. How does your Dockerfile look? Do you execute migrations on startup? Will you do them manually? – DrummerMann Jan 08 '22 at 16:17
  • thank you python manage.py migrate helped me. Could you please link me to the details how django understands that 'db' is correct database url? – Alex Jan 08 '22 at 16:24
  • 1
    This is a Docker thing, Django just uses it. I edited my aswer with the `Docker` documentation link – DrummerMann Jan 08 '22 at 16:31
  • This fixed my issue using docker compose with a db and web service trying to get postgres to connect. – orb Mar 17 '23 at 21:05
1

If you are using Linux environment make sure to set the host with the name of your database service, suppose my database service is like this:

 db:
      image: postgres:13.0-alpine
      volumes:
       - postgres_data:/var/lib/postgresql/data/
      env_file:
       - ./config/dev.env.db
      
      ports:
          - "5432:5432"

On my environment host will be db if you are using windows/mac make sure you are using for the

host : host.docker.internal

you should also map the database server port in your dockercompose.yml file by default 5432:5432

Madhav Dhungana
  • 476
  • 4
  • 13
0

I did the following

  1. We get which application occupies the port: lsof -i tcp:5432
  2. Using the Kill command, we terminate the process that occupies port 5432
Vadim
  • 171
  • 1
  • 6
0

I had this issue when i forget to add -p 5432:5432

Check that you explicitly specify the port that will be used by the container.

docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=p4SSW0rd postgres
ahmnouira
  • 1,607
  • 13
  • 8