Home > other >  localhost doesn't work when I dockerize postgres with Django
localhost doesn't work when I dockerize postgres with Django

Time:12-09

My system is Ubuntu 22.04.1 LTS.

I was going through the Django for professionals book and follow all line by line but somehow when i dockerize my django project with postgres it just doesn't run localhost properly, with sqlite all works fine.

Dockerfile

# Pull base image
FROM python:3.10.6-slim-bullseye
# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# Copy project
COPY . .

docker-compose.yml

version: "3.9"

services:
 web:
  build: .
  command: python /code/manage.py runserver 0.0.0.0:8000
  volumes:
   - .:/code
  ports:
   - 8000:8000
  depends_on:
   - db
 db:
  image: postgres:13
  volumes:
   - postgres_data:/var/lib/postgresql/data/
  environment:
   - "POSTGRES_HOST_AUTH_METHOD=trust"

volumes:
 postgres_data:

django.settings.py

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "postgres",
        "USER": "postgres",
        "PASSWORD": "postgres",
        "HOST": "db",  # set in docker-compose.yml
        "PORT": 5432,  # default postgres port
    }
}

When I run docker-compose up that shows and seems like all must be fine, but somehow it just doesn't work:

Attaching to ch3-postgresql_web_1, ch3-postgresql_db_1
web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  | 
web_1  | System check identified no issues (0 silenced).
db_1   | 
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   | 
db_1   | 2022-12-05 11:58:21.572 UTC [1] LOG:  starting PostgreSQL 13.9 (Debian 13.9-1.pgdg110 1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
db_1   | 2022-12-05 11:58:21.574 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2022-12-05 11:58:21.575 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2022-12-05 11:58:21.577 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2022-12-05 11:58:21.580 UTC [26] LOG:  database system was shut down at 2022-12-05 11:58:17 UTC
db_1   | 2022-12-05 11:58:21.584 UTC [1] LOG:  database system is ready to accept connections

And when I go to the url 127.0.0.1:8000:

This site can’t be reached
The webpage at http://127.0.0.1:8000/ might be temporarily down or it may have moved permanently to a new web address.
ERR_SOCKET_NOT_CONNECTED

If i wait some time and then check docker-compose logs, this messages appears

psycopg2.OperationalError: connection to server at "db" (172.21.0.2), port 5432 failed: Connection timed out
web_1  |    Is the server running on that host and accepting TCP/IP connections?

I spend a lot of hours of searching for info about this problem but nothing did help. I try even to reinstall different versions of Postgres and docker.

CodePudding user response:

Simply try this:

change this:

volumes:
   - postgres_data:/var/lib/postgresql/data/

To this:

volumes:
   - postgres_data:/var/lib/postgresql/data #removed forward slash from here.

Try above thing and see if it solves that problem.

Note: after making above changes don't forget to re-build docker-compsoe

CodePudding user response:

You will need to add more environment variables to make the PostgreSQL work.

Please refer to https://hub.docker.com/_/postgres "Environment Variables"

The only variable required is POSTGRES_PASSWORD, the rest are optional.

db:
    image: postgres:13
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
      - POSTGRES_HOST_AUTH_METHOD=trust

And, you are encouraged to make your docker compose file more secure:

db:
    image: postgres:13
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - .env.dev

.env.dev

POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
POSTGRES_HOST_AUTH_METHOD=trust
  • Related