Django migrations failing in Docker container after adding new model

I am running a Django project inside a Docker container, and I am facing an issue with migrations after adding a new model to models.py.

When I try to apply the migrations, the system looks for the old migrations, but since I rebuild the app via Docker, those migrations are missing. When I try to force Django to ignore the old migrations, it attempts to create tables that already exist, and the process fails because of that.

If I allow it to run the migrations normally, it tries to re-apply the migrations that have already been executed, which leads to errors.

I need a way to: • Skip old migrations during the rebuild. • Ensure the new tables are created without conflicting with existing ones. • Prevent Django from reapplying migrations that are already done.

How can I resolve this migration issue in Docker? Any help would be appreciated!

Here’s what I have in my Dockerfile (now there is that problem with creating existing tables):

FROM python:3.11

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

# Vyčistíme migrace a vytvoříme nové při buildu image
RUN rm -rf api/migrations/* && \
    touch api/migrations/__init__.py 

# Instalace netcat pro kontrolu dostupnosti databáze
RUN apt-get update && apt-get install -y netcat-traditional

# Command který se spustí při startu kontejneru
CMD bash -c '\
    while ! nc -z db 5432; do \
        echo "Waiting for database..." && \
        sleep 1; \
    done && \
    echo "Database is up!" && \
    python manage.py makemigrations api && \
    python manage.py migrate && \
    python manage.py runserver 0.0.0.0:8000'
Вернуться на верх