0

I know there have been similar questions on this site, but the answers to these are sadly outdated.

So I have a django application and i want to use postgres as the underlying database.
Additionally a want to separate both programs in separate dockers.
Now the docker docs have a way to do it, but sadly it seems to be outdated: link

The problem appears when i call manage.py migrate in the docker build function which is being run by the docker compose file. But i get the error that the host 'db' is unknown/invalid.

Compose excerpt:

services: 
    db:
        image: postgres
        restart: always
        volumes:
            - DataBase:/var/lib/postgresql/data
        environment:
            - POSTGRES_DB=postgres
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
        ports:
            - "5432:5432"

    app:
        build: FehlzeitErfassungProject

        restart: always
        volumes:
            - Logs:/app/logs
            - Backups:/app/Backups
            - Media:/app/MEDIA
        ports:
            - "5432:5432"
        
        depends_on: 
            - db

app dockerfile:

FROM ubuntu

WORKDIR /app

ADD ./requirements.txt ./

RUN apt-get update && \
    apt-get upgrade -y

# getting tzdata to shutup
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Berlin
RUN apt-get -y install tzdata
#installing all needed porgramms
RUN apt-get install -y uwsgi python3 python3-pip

RUN python3 -m pip install pip --upgrade
RUN python3 -m pip install -r requirements.txt

COPY ./ ./

RUN ./manage.py migrate

CMD ["uwsgi", "uwsgu.ini"]

PS: It seems to appear, that the other dockers are only launched when the app docker has already finished building

Edit: the database_settings:

DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.postgresql',
      'NAME': 'postgres',
      'USER': 'postgres',
      'PASSWORD': 'postgres',
      'HOST': '127.0.0.1',
      'PORT': 5432,
  }
}
  • I don't think the state of the world has really changed since questions like [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) or [How do you perform Django database migrations when using Docker-Compose?](https://stackoverflow.com/questions/33992867/how-do-you-perform-django-database-migrations-when-using-docker-compose) were asked. You can't connect to your database from the Dockerfile at all, and you need to run migrations at container startup, possibly via an entrypoint script. – David Maze Aug 25 '20 at 19:28
  • @David Thanks haven't found the second one, my current work around is as you described; I launch a shell script as a entry point doing all things I need to be done before startup, but I want it to be only once and not at every reboot, therefore running it in build would be more efficient. Then can I add the command the second one used in the command line in the docker-compose file that I only need to say docker compose up? – Hendiadyoin Aug 26 '20 at 20:13

2 Answers2

1

I think that your problem is in the file "settings.py" in the DB configuration for the HOST param, you need to have the name of the postgresql service that you have defined in the docker-compose file, in this case, is "db".

BTW I think that using constants in settings.py is bad behavior, it's better to define in the docker-compose file environmental variables.

In your case would be:

docker-compose.yml

services: 
    db:
        image: postgres
        restart: always
        volumes:
            - DataBase:/var/lib/postgresql/data
        environment:
            - POSTGRES_DB=postgres
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
        ports:
            - "5432:5432"

    app:
        build: FehlzeitErfassungProject

        restart: always
        volumes:
            - Logs:/app/logs
            - Backups:/app/Backups
            - Media:/app/MEDIA
        ports:
            - "5432:5432"
        environment:
            - DB_HOST=db
            - DB_NAME=postgres
            - DB_USER=postgres
            - DB_PASSWORD=postgres
        depends_on: 
            - db

Settings.py

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

Remember to import os in settings.py.

CarloSan
  • 11
  • 1
-1

binding both container port at same port for the host will not work. to access your database in other container you should link to it.

version: "3.0"
services: 
    db:
        image: postgres
        restart: always
        volumes:
            - DataBase:/var/lib/postgresql/data
        environment:
            - POSTGRES_DB=postgres
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
        ports:
            - "5432:5432"

    app:
        build: FehlzeitErfassungProject

        restart: always
        volumes:
            - Logs:/app/logs
            - Backups:/app/Backups
            - Media:/app/MEDIA
        ports:
            - "8000:8000" 
        links:
            - "db"
        depends_on: 
            - db
Naqib Hakimi
  • 874
  • 4
  • 15
  • You don't need `links:` at all in modern Docker, and they won't solve the problem described in the question. – David Maze Aug 25 '20 at 19:25