0

During working with docker where I dockerised Django PostgreSQL, I've entered in such problems as when I change some model and migrate it, after entering to the page, it says there is no such relationship in the database. After some research, I found that problem can be due to creating every time new migration and deleting the old. How can I fix this problem?

Below you can see my configurations

docker-compose-prod.yml


services:
  app:
    volumes:
      - static_data:/app/staticfiles
      - media_data:/app/mediafiles
    env_file:
      - django.env
      - words_az.env
      - words_en.env
    build:
      context: .
    ports:
      - "8000:8000"
    entrypoint: /app/script/entrypoint.sh
    command: sh -c "python manage.py collectstatic --no-input &&
                    gunicorn --workers=3 --bind 0.0.0.0:8000 django.wsgi:application"
    depends_on:
      - db

  nginx:
    build: ./nginx
    volumes:
      - static_data:/app/staticfiles
      - media_data:/app/mediafiles
    ports:
      - "80:80"
      - "443:443"

    depends_on:
      - app
      - flower

  db:
    image: postgres:14.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - db.env
    ports:
      - "5432:5432"

  redis:
    image: redis:alpine
    ports:
      - "6379:6379"

  worker:
    build:
      context: .
    command: celery -A django worker -l info
    env_file:
      - django.env
    depends_on:
      - db
      - redis
      - app

  flower:
    build: ./
    command: celery -A django flower --basic_auth=$user:$password --address=0.0.0.0 --port=5555 --url-prefix=flower
    env_file:
      - django.env
    ports:
      - "5555:5555"
    depends_on:
      - redis
      - worker

volumes:
  postgres_data:
  static_data:
  media_data:

Dockerfile

FROM python:3.9-alpine

ENV PATH = "/script:${PATH}"

COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache --virtual .tmp gcc g++ libc-dev linux-headers \
    && apk add --virtual build-deps gcc python3-dev musl-dev \
    && apk add postgresql \
    && apk add postgresql-dev \
    && pip install psycopg2 \
    && apk add jpeg-dev zlib-dev libjpeg \
    && pip install Pillow \
    && apk del build-deps
RUN pip install --upgrade pip
RUN pip install -r /requirements.txt
RUN apk del .tmp

RUN mkdir /app
COPY /src /app
RUN mkdir /app/staticfiles
COPY /script /app/script
RUN chmod +x /app/script/*

WORKDIR /app

COPY django.env /app



RUN adduser -D user
RUN chown -R user:user /app
RUN chown -R user:user /var
RUN chmod -R 755 /var/

RUN chmod +x script/entrypoint.sh

USER user

CMD ["/script/entrypoint.sh"]
  • When and how do you run migrations? What's in the `entrypoint.sh` script? Why do you need to override the `entrypoint:` and `command:` in the Compose setup? Do the recipes in [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) help you? – David Maze May 16 '22 at 13:18
  • @DavidMaze entrypoint is like this `code #!/bin/sh if [ "$DATABASE" = "postgres" ] then echo "Waiting for postgres..." while ! nc -z "$POSTGRES_HOST" "$POSTGRES_PORT"; do sleep 0.1 done echo "PostgreSQL started" fi python manage.py makemigrations --no-input python manage.py migrate --no-input python manage.py migrate users --no-input python manage.py migrate healthcare --no-input exec "$@" ` – Richard May 19 '22 at 16:42
  • @DavidMaze unfortunately the post below didn't help me – Richard May 19 '22 at 16:46
  • @DavidMaze what is recommended way to dockerize django and don't have problems with migrations ? Maybe i don't consider some problems in my models.py ? – Richard May 19 '22 at 16:47
  • In a more production-oriented environment I'd [run migrations manually](https://stackoverflow.com/a/49009769); in a purely developer setup I'd [put it into an entrypoint wrapper script](https://stackoverflow.com/a/55934863) (which looks much like what you tried to include in the previous comment). I'd expect the migrations to be checked into the source tree, and you should not need to run `./manage.py makemigrations` on every container startup. – David Maze May 19 '22 at 17:13
  • Thank you @DavidMaze for essential comment ! – Richard May 19 '22 at 20:01

0 Answers0