2

I am doing a django API with docker and postgresql, i set up a docker compose file to use a db network which is a postgresql image and an app network which is my django app. I don't know what is happening (i followed a tutorial) but my configuration isn't working, it give me an error on django that the database doesn't exist.

Here is the files:

docker-compose.yml

version: "3"

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes: # Any change made on the project will be reflected on the container
      - ./app:/app
    command: >
      sh -c "python manage.py wait_for_db &&
             python manage.py migrate &&
             python manage.py runserver 0.0.0.0:8000"
    environment:
      - DB_HOST=db
      - DB_NAME=recipe
      - DB_USER=postgres
      - DB_PASS=supersecretpassword
    depends_on:
      - db
  db:
    image: postgres:10-alpine
    environment:
      - POSTGRES_DB=recipe
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=supersecretpassword

Dockerfile

FROM python:3.7-alpine
LABEL maintainer="trolliama"

# Recommended - Don't Allow the python buffer the outputs
ENV PYTHONUNBUFFERED 1 

COPY ./requirements.txt /requirements.txt
# Dependencies to run postgresql
# apk it's the packege install of alpine like: apt
# apk add == apt install
RUN apk add --update --no-cache postgresql-client
RUN apk add --update --no-cache --virtual .tmp-build-deps \
    gcc libc-dev linux-headers postgresql-dev
RUN pip install -r /requirements.txt

RUN apk del .tmp-build-deps

RUN mkdir /app
# Changes for the app dir
WORKDIR /app 
COPY ./app /app

# By default the docker uses the root user to run the container so..
# Create a user with for just run the application
RUN adduser -D user
# Changes to the user
USER user

settings.py

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "HOST": os.environ.get("DB_HOST"),
        "NAME": os.environ.get("DB_NAME"),
        "USER": os.environ.get("DB_USER"),
        "PASSWORD": os.environ.get("DB_PASS"),
    }
}

requirements.txt

django>=2.1.3,<2.2.0
djangorestframework>=3.9.0, <3.10.0
psycopg2>=2.7.5,<2.8.0
flake8>=3.6.0,<3.7.0

So this is the config, in the tutorial i also set up this django custom command that just check if the database is up and exist (wait_for_db).

wait_for_db.py

import time

from django.db import connections
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    """Command to wait for DB"""

    def handle(self, *args, **options):
        self.stdout.write("Waiting for database...")
        db_conn = None
        while not db_conn:
            try:
                db_conn = connections["default"].cursor()
                self.stdout.write("db_conn: {}".format(db_conn.cursor()))
            except OperationalError:
                self.stdout.write("Database not available")
                time.sleep(1)

        self.stdout.write(self.style.SUCCESS("Database available"))

So when i remove this .cursor() off the connections["default"] i get this error when execute docker-compose up saying that i don't have a recipe db.

django.db.utils.OperationalError: FATAL:  database "recipe" does not exist

If i don't remove the cursor() it gets in a loop

So I checked that the docker compose db network was creating the db using docker-compose run db and going to the created container to get the databases and ther was the recipe db. Any ideas of what is happening?

2
  • Can you share what is inside your requirements.txt? Commented May 21, 2021 at 23:19
  • of course, i will edit Commented May 22, 2021 at 0:00

1 Answer 1

2

I managed to fix this with those commands:

$ docker-compose down
$ docker-compose up --force-recreate

Here is the link for the issue

Sign up to request clarification or add additional context in comments.

Comments

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.