[Fixed]-Why am I unable to run django migrations via the 'docker-compose run web' command?

46👍

docker-compose run creates new containers

You have already noticed the problem. When you use docker-compose run, a new container is created.

When you ran the first command (makemigrations), a new container was created, makemigrations ran, and the migration files were written to the (new) container’s filesystem.

When you ran the second command (migrate), another new container was created. The migration ran, but it had nothing to do. That’s because the migration files were not available – they were written in a different container than this new one.

You can solve this in a couple of ways.

Using docker-compose exec

First, you can do what you already did, but use docker-compose exec instead of run.

docker-compose exec web python manage.py makemigrations 
docker-compose exec web python manage.py migrate

exec will use the already-running container, rather than creating new containers.

Using an entrypoint script

Another option is to use an entrypoint script and run the migration there, before the server is started. This is the way to go if you’d prefer things to be more automatic.

Dockerfile:

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

entrypoint.sh:

#!/bin/sh
python manage.py makemigrations
python manage.py migrate
exec "$@"

docker-compose.yml (under ‘web’):

entrypoint: /entrypoint.sh

In this scenario, when the container starts, the entrypoint script will run, handle your migration, then hand off to the command (which in this case is Django runserver).

The new containers loop forever

As you noticed, the new containers stay running. That is normally unexpected, because you overrode the command with one that should exit (rather than stay running). However, in docker-compose.yml, you specified restart: always. So they will run the migration commands over and over, restarting each time the command exits.

3👍

Dan Lowe gave a very nice answer, but the entrypoint script was not working for me. The problem is that some “makemigrations” expect your input, for instance “yes”/”no”.

You can complement Dan Lowe answer with:

python manage.py makemigrations --noinput

instead of

python manage.py makemigrations

(This works at least for simple “yes”/”no” questions)

2👍

This awnser is a complement to Dan Lowe and Rexcirus responses.

To work well during de CodeBuild e Fargate I did some changes:

Dockefile:

COPY ./docker/entrypoint.sh /usr/local/bin/
COPY ./docker/entrypoint.sh /${projectName}/
# backwards compat
RUN ln -s usr/local/bin/entrypoint.sh /

ENTRYPOINT ["entrypoint.sh"]
CMD ["entrypoint.sh"]

./docker/entrypoint.sh

#!/bin/sh
python manage.py makemigrations --noinput
python manage.py migrate
python manage.py runserver 0.0.0.0:8000

Now everything is running OK.

0👍

python manage.py makemigrations

python manage.py migrate

#Crear usuario root

DJANGO_SUPERUSER_PASSWORD=*your-passwd* python manage.py createsuperuser --username *root* --email *youremail@gamil.com* --noinput

0👍

I have resolved this bu changing directory "myProject" path in docker-compose.yml and DockerFile

in Docket File

Pull base image

FROM python:3.7

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /myProject

# Install dependencies
COPY Pipfile Pipfile.lock /myProject/
RUN pip install pipenv && pipenv install --system

Copy project

COPY . /myProject/

in docker-compose.yml file

version: '3.7'

services:
  web:
    build: .
    command: python /myProject/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/myProject
    ports:
      - 8000:8000

Leave a comment