4

EDIT: Docker does not seem to create my postgres database even though I have them defined in my .env file.

I've used docker-compose config, and it looks like Docker acknowledges my data:

environment:
    POSTGRES_DB: animemusicsorter
    POSTGRES_PASSWORD: root
    POSTGRES_USER: waifuszn

But when I run docker-compose up, I am given these errors:

postgres    | 2021-07-12 22:39:01.906 UTC [35] FATAL:  password authentication failed for user "waifuszn"
postgres    | 2021-07-12 22:39:01.906 UTC [35] DETAIL:  Role "waifuszn" does not exist.

I have tried removing volumes and removing postgres and re-creating them but that also has not given me any results.

docker-compose.yml

version: '3.5'

services:
  postgres:
    container_name: postgres
    image: postgres:11.6
    ports:
      - "5432:5432"
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
    volumes:
      - ~/srv/docker/template-postgres/data:/var/lib/postgresql/data:rw

  django:
    container_name: django
    build:
      context: .
      dockerfile: ./conf/django/Dockerfile.windows
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
    volumes:
      - ./back:/app:rw
    depends_on:
      - postgres
    env_file:
      - .env

  react:
    container_name: react
    build:
      context: .
      dockerfile: ./conf/react/Dockerfile
    command: yarn run start:dev
    ports:
      - "4000:4000"
    volumes:
      - ./front/src:/app/src:rw
      - ./front/public:/app/public:rw
skoleosho97
  • 173
  • 1
  • 2
  • 13
  • Are you using docker-compose.yml? If you do, can you share it please? – andrepz Jul 12 '21 at 18:19
  • I'm asking because you also need to pass your env file to your postgres service inside compose. – andrepz Jul 12 '21 at 18:22
  • 1
    Just added it; sorry if the indentation is messed up, the code block was being very difficult. Pretty sure I pass the .env file at the bottom of it though; unless I did it wrong. – skoleosho97 Jul 12 '21 at 18:25

3 Answers3

1

Check if your Postgres service passes the environment variables:

version: "3"

services:
    django:
        build: .
        restart: on-failure
        container_name: django
        command: bash -rc "python intento/manage.py makemigrations &&
                           python intento/manage.py migrate &&
                           gunicorn --limit-request-line 8190 --workers=3
                           --chdir /usr/src/app/src wsgi:application --bind 0.0.0.0:8000"
        volumes:
            - .:/usr/src/app
        expose:
            - 8000
        env_file:
            - ./.env
        depends_on:
            - pgdb
    pgdb:
        image: postgres
        restart: on-failure
        container_name: pgdb
        environment:
            POSTGRES_USER: ${DB_USER}
            POSTGRES_PASSWORD: ${DB_PASSWORD}
            POSTGRES_DB: ${DB_NAME}

EDIT: Just saw the example you gave, change the = in your Postgres environment properties with :

andrepz
  • 443
  • 6
  • 16
  • 1
    Why am I just seeing that LMAO, let's see if it'll work now. – skoleosho97 Jul 12 '21 at 18:27
  • Maybe you need to rebuild the PostgreSQL container? – andrepz Jul 12 '21 at 22:16
  • Doing that right now, will probably edit the original question if this doesn't work. I laerned that environmental variables will only be recognized on initial startup, though, so maybe a full reset is what I need. – skoleosho97 Jul 12 '21 at 22:37
  • Yeah, that didn't work either. Re-worded the original question; hopefully can get a different perspective on this issue. – skoleosho97 Jul 12 '21 at 23:05
1

As you stated .env file is a good practice for the setting the environment variables for your docker builds.
As per your question/issue :

I am not sure if I still need to create the database locally on my system, or whether Docker handles it automatically.

You will not have to create a database locally if you are also deploying a postgres container using docker-compose. If that's the case i would recommend that you attach both these container to a network of your choice and name the container so that the use of host name as postgres can be reached by django container. The .env file can be selectively provided to the container(probably your indentation shifted while pasting your docker-compose file) and the following docker-compose.yml file should look like this

   version: '3.5'

services:
  postgres:
    container_name: postgres
    image: postgres:11.6
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}
      - POSTGRES_USER=${DB_USER}
    volumes:
      - ~/srv/docker/template-postgres/data:/var/lib/postgresql/data:rw
    networks:
      - network_name

 django:
   container_name: django
   build:
     context: .
     dockerfile: ./conf/django/Dockerfile.windows
     command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
    volumes:
      - ./back:/app:rw
    depends_on:
      - postgres
    env_file:
      - .env
    networks:
      - network_name

networks:
  network_name:
    name: network_name
  

You might face permission issues for your user, so you might want to look into this issue that can guide you to create a Dockerfile for your database.

Loukik
  • 171
  • 1
  • 7
0

When you first run docker-compose up, Postgres will initialize the DB with the configured POSTGRES_* variables.

After that, if you changed one of these variables you need to manually reset them to the DB too via psql or execute docker-compose rm postgres; docker-compose up postgres to recreate the DB with new variables.

WARNING: docker-compose rm postgres will drop your current DB.

This answer recommends docker-compose down -v to wipe your volume either.

frost-nzcr4
  • 1,540
  • 11
  • 16