You just have to log into your running docker container and run your commands.

  1. Build your stack : docker-compose build -f path/to/docker-compose.yml
  2. Launch your stack : docker-compose up -f path/to/docker-compose.yml
  3. Display docker running containers : docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
3fcc49196a84        ex_nginx          "nginx -g 'daemon off"   3 days ago          Up 32 seconds       0.0.0.0:80->80/tcp, 443/tcp   ex_nginx_1
66175bfd6ae6        ex_webapp         "/docker-entrypoint.s"   3 days ago          Up 32 seconds       0.0.0.0:32768->8000/tcp       ex_webapp_1
# postgres docker container ...
  1. Get the CONTAINER ID of you django app and log into :
docker exec -t -i 66175bfd6ae6 bash
  1. Now you are logged into, then go to the right folder : cd path/to/django_app

  2. And now, each time you edit your models, run in your container : python manage.py makemigrations and python manage.py migrate

I also recommend you to use a docker-entrypoint for your django docker container file to run automatically :

  • collecstatic
  • migrate
  • runserver or start it with gunicorn or uWSGI

Here is an example (docker-entrypoint.sh) :

#!/bin/bash

# Collect static files
echo "Collect static files"
python manage.py collectstatic --noinput

# Apply database migrations
echo "Apply database migrations"
python manage.py migrate

# Start server
echo "Starting server"
python manage.py runserver 0.0.0.0:8000
Answer from Louis Barranqueiro on Stack Overflow
Top answer
1 of 10
189

You just have to log into your running docker container and run your commands.

  1. Build your stack : docker-compose build -f path/to/docker-compose.yml
  2. Launch your stack : docker-compose up -f path/to/docker-compose.yml
  3. Display docker running containers : docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
3fcc49196a84        ex_nginx          "nginx -g 'daemon off"   3 days ago          Up 32 seconds       0.0.0.0:80->80/tcp, 443/tcp   ex_nginx_1
66175bfd6ae6        ex_webapp         "/docker-entrypoint.s"   3 days ago          Up 32 seconds       0.0.0.0:32768->8000/tcp       ex_webapp_1
# postgres docker container ...
  1. Get the CONTAINER ID of you django app and log into :
docker exec -t -i 66175bfd6ae6 bash
  1. Now you are logged into, then go to the right folder : cd path/to/django_app

  2. And now, each time you edit your models, run in your container : python manage.py makemigrations and python manage.py migrate

I also recommend you to use a docker-entrypoint for your django docker container file to run automatically :

  • collecstatic
  • migrate
  • runserver or start it with gunicorn or uWSGI

Here is an example (docker-entrypoint.sh) :

#!/bin/bash

# Collect static files
echo "Collect static files"
python manage.py collectstatic --noinput

# Apply database migrations
echo "Apply database migrations"
python manage.py migrate

# Start server
echo "Starting server"
python manage.py runserver 0.0.0.0:8000
2 of 10
97

I use this method:

services:
  web:
    build: .
    image: uzman
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "3000:3000"
      - "8000:8000"
    volumes:
      - .:/code
    depends_on:
      - migration
      - db
  migration:
    image: uzman
    command: python manage.py migrate --noinput
    volumes:
      - .:/code
    depends_on:
      - db

Using docker hierarchy we made, the service migration runs after setting up the database and before running the main service. Now when you run your service docker will run migrations before running the server; look that the migration server is applied over the same image as the web server, it means that all migrations will be taken from your project, avoiding problems.

You avoid making entry points or whatever other thing this way.

🌐
Baeldung
baeldung.com › home › docker › performing django database migrations with docker-compose
Performing Django Database Migrations With Docker-Compose | Baeldung on Ops
February 21, 2025 - $ docker-compose run web python manage.py makemigrations books && docker-compose run web python manage.py migrate · These steps enable the proper detection of the model changes and the correct application of the migrations. For this reason, the database remains in sync with the Django models while running inside Docker.
Discussions

makemigration and migrate doesn't work docker production
What happened? Hello, I don't know if it's a bug, but I have a problem with makemigrations and docker, I use django helpdesk on a project, I had to add a new model and new package django. W... More on github.com
🌐 github.com
7
December 11, 2019
Migration files created outside project directory (Docker container)
I have a strange problem in a Django container where makemigrations creates the migration files in /usr/local/lib/python3.*/site-packages/*/migrations/ instead of the usual project’s directory (which is bind mounted to the host). When I run makemigration on the host, it creates the migration ... More on forum.djangoproject.com
🌐 forum.djangoproject.com
0
0
August 18, 2021
Running DB migrate within dockerfile
We typically use entrypoint scripts and at the end call supervisord to start nginx/daphne. More on reddit.com
🌐 r/django
6
0
December 15, 2021
Running Django makemigrations Docker container - Stack Overflow
I have two Docker containers, one for MySQL and one for Django. I modify a model that requires a migration, and therefore the need to run makemigrations: what do I do such that I end up with a 000X... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/django › making migrations for a dockerized django app
r/django on Reddit: Making migrations for a dockerized django app
January 20, 2021 -

Hey everyone,

I have been trying to setup a django app using this guide:

https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/

And it was a great resource to get everything up and running. However i have problem now when it comes to migrations. Say i add a model, add data into my db, and later i change the model. Now i can run makemigrations using the exec method on the container, but when i shut down my migrations are not stored in my local files i save to my git project, instead they are lost as i spin down the container.

Does anyone know how to solve this, how do you makemigrations is such a setup where you run two dockerized django/postgres dev/prod environments?

🌐
GitHub
github.com › zoidbergwill › docker-django-migrations-example › blob › master › Makefile
docker-django-migrations-example/Makefile at master · zoidyzoidzoid/docker-django-migrations-example
December 11, 2019 - Simple Django site with migrations for demonstrating Kubernetes - docker-django-migrations-example/Makefile at master · zoidyzoidzoid/docker-django-migrations-example
Author   zoidyzoidzoid
🌐
Medium
medium.com › analytics-vidhya › django-with-docker-and-docker-compose-python-part-2-8415976470cc
Django with Docker and Docker Compose (Python part 2) | by Tariqul Islam | Analytics Vidhya | Medium
December 31, 2019 - And For the docker environment, using a Postgresql database for migration. To Create Migration, migration command would like that below · > python manage.py makemigrations [module_name] I create the model in the main module, so the migration command will be like below · > python manage.py makemigrations main · After Complete the command the folder structure will be like below · django-docker └── app │ ├── main │ │ ├── migrations │ │ │ ├── __inti__.py │ │ │ ├── 0001_initial.py ·
🌐
GitHub
github.com › simonw › til › blob › main › docker › docker-compose-for-django-development.md
til/docker/docker-compose-for-django-development.md at main · simonw/til
Already has a Dockerfile used for the production deployment, but needed a separate one for the development environment · Makes extensive use of Django migrations (over 100 and counting)
Author   simonw
🌐
GitHub
github.com › pydanny › cookiecutter-django › issues › 2360
makemigration and migrate doesn't work docker production · Issue #2360 · cookiecutter/cookiecutter-django
December 11, 2019 - Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them. e@vmi:~/django$ docker-compose -f production.yml run --rm django python manage.py migrateStarting e_redis_1 ...
Author   scwall
Find elsewhere
🌐
OneUptime
oneuptime.com › home › blog › how to run django migrations with docker compose
How to Run Django Migrations with Docker Compose
January 25, 2026 - # Makefile .PHONY: help up down migrate shell test help: @echo "Available commands:" @echo " make up - Start all services" @echo " make down - Stop all services" @echo " make migrate - Run database migrations" @echo " make shell - Open Django shell" @echo " make test - Run tests" up: docker compose up -d down: docker compose down migrate: docker compose run --rm web python manage.py migrate makemigrations: docker compose run --rm web python manage.py makemigrations shell: docker compose run --rm web python manage.py shell test: docker compose run --rm web python manage.py test logs: docker compose logs -f web
🌐
Django Forum
forum.djangoproject.com › using django
Migration files created outside project directory (Docker container) - Using Django - Django Forum
August 18, 2021 - I have a strange problem in a Django container where makemigrations creates the migration files in /usr/local/lib/python3.*/site-packages/*/migrations/ instead of the usual project’s directory (which is bind mounted to the host). When I run makemigration on the host, it creates the migration ...
🌐
Reddit
reddit.com › r/django › running db migrate within dockerfile
r/django on Reddit: Running DB migrate within dockerfile
December 15, 2021 -

I asked a question few weeks ago regarding how when using an external DB I am fine using dockerfile to just run the web server image and not worry about the DB image. I'm writing the dockerfile for the same and I was wondering where I should put the migrate command I understand that there can only be one CMD command within a dockerfile So my thoughts immediately went to

Having one RUN command to migrate the DB and the CMD command to run the server.

Using the && operator within the CMD command in shell form and run both.

I've also seen an entry point script being used sometimes.

What are the tradeoffs between these options and if I'm just running a migrate command do I bother with an entry point file or is it necessary.

Would love to get some help on the same.

🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Django makemigrations in Doker - Visual Studio Marketplace
February 26, 2024 - Extension for Visual Studio Code - This extension allows you to execute the makemigrations and migrate Django's commands of an application running inside a container without having to write commands in the terminal.
🌐
Stack Overflow
stackoverflow.com › questions › 70840872 › running-django-makemigrations-docker-container
Running Django makemigrations Docker container - Stack Overflow
The only option that comes to mind is to run docker exec -it CONTAINER_NAME python ./manage.py makemigrations APP_NAME. Two problems with this: It only works the first time. Successive reruns of makemigrations claim there are no changes.
🌐
GitHub
github.com › morninj › django-docker
GitHub - morninj/django-docker: A framework for deploying Django projects on Docker
$ docker run -ti -p 80:80 -v $(pwd):/code --env DJANGO_PRODUCTION=false <yourname>/django-docker /bin/bash · Then, start the database server and invoke initialize.sh: ... This will call python manage.py makemigrations and prompt you if necessary. It will create the necessary migration files.
Starred by 354 users
Forked by 103 users
Languages   Python 60.4% | Shell 27.7% | HTML 5.4% | Nginx 5.2% | CSS 1.3% | Python 60.4% | Shell 27.7% | HTML 5.4% | Nginx 5.2% | CSS 1.3%
Top answer
1 of 5
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.

2 of 5
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)

🌐
Simon Willison
til.simonwillison.net › docker › docker-compose-for-django-development
Docker Compose for Django development | Simon Willison’s TILs
Already has a Dockerfile used for the production deployment, but needed a separate one for the development environment · Makes extensive use of Django migrations (over 100 and counting)
🌐
Django Forum
forum.djangoproject.com › using django
Why command “python manage.py migrate” after success work lock console? - Using Django - Django Forum
April 2, 2021 - I try to solve following task. I have Django app and I want to start it using Docker. As ‘db’ I add a container with PostgreSQL or MySQL. In my case I need run “python manage.py migrate” before “python manage.py runserver”, so I wrote in docker-compose.yml file in fiels “command” folloving string:sh -c “sleep 20 && python manage.py migrate --noinput && python manage.py runserver 0.0.0.0:8000 --noreload”. But after applying all migrations command “python manage.py migrate” doesn’t stop and doesn’...
🌐
GitHub
github.com › thorgate › django-project-template › issues › 6
Question: how to make migrations on each docker-compose up · Issue #6 · thorgate/django-project-template
February 10, 2018 - I am getting the below error, but how do I automatically "python manage.py migrate" before runserver from docker-compose? You have unapplied migrations; your app may not work properly until they are applied. @ #
Author   am1ru1
🌐
GitHub
gist.github.com › brunojppb › 138285919a03655843c3dd593f4a9b0a
django migration commands using docker compose · GitHub
# django = cocker compose service name $ docker-compose exec -u $UID django ./manage.py makemigrations