skip to Main Content

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)

4

Answers


  1. 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.

    Login or Signup to reply.
  2. 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
    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
  4. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search