6

Seems like a common error. I followed every possible suggestion on Stackoverflow(adding links, network etc), but it makes no difference in my case. I am running on Windows.

My Dockerfile:

#Pull base image.
FROM python:3.9.5-slim

#Usefull to get logs
ENV PYTHONUNBUFFERED 1

#Make local dir
RUN mkdir -p /app

#set as the working directory
WORKDIR /app

#https://luis-sena.medium.com/creating-the-perfect-python-dockerfile-51bdec41f1c8
COPY requirements.txt .

#now copy all the files in this directory to \code
ADD . .

#https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/
RUN apt-get update && \
 apt-get install -y postgresql-server-dev-all gcc python3-dev musl-dev && \
 pip install -r requirements.txt && \
 adduser --disabled-password --no-create-home app

USER app

#CMD python manage.py runserver
CMD gunicorn --bind 0.0.0.0:8000 app.wsgi:application -k eventlet

Next, my docker-compose.yml:

services:
  web:
    build: .
    ports:
      - "8000:8000"
    volumes:
      - .:/app
    links:
      - db:db
    container_name: heritageapi_web_1
    depends_on:
      - db
    networks:
      - heritage-network

  db:
    image: postgres:13.3
    container_name: db
    restart: always
    networks:
      - heritage-network
    environment:
      POSTGRES_DATABASE: admin
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
      POSTGRES_ROOT_PASSWORD: root
    volumes:
      - .dbdata:/var/lib/postgres
    ports:
      - "32768:5432"

networks:
  heritage-network:
    driver: bridge

My .env:

DB_NAME=admin 
DB_USER=root 
DB_PASSWORD=root 
DB_HOST=db 
DB_PORT=5432

Finally my settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'HOST': os.getenv('DB_HOST'),
        'PORT': os.getenv('DB_PORT'),
    }
}
1
  • Shouldn't that be: os.getenv('POSTGRES_DATABASE')? Also the other env variables should be changed? Commented Jul 27, 2021 at 16:44

1 Answer 1

3

I don't see .env file in web container. It should be something like that:

services:
  web:
   depends_on:
    - db
   env_file:
    - .env.dev

If everything works then enter running docker container with docker-compose exec and print

python manage.py shell
>> from django.conf import settings
>> settings.DB_HOST
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you reply. my .env file is "transfered" via volumes: as I understand. Btw, I just observed that this error I am getting is only via Pycharm. When I ssh into docker container, there is no issue. I am able to perform migrations etc. May be the issue is with Pycharm.
Try to set .env in Pycharm manually, not via files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.