Django: Running management commands inside a Docker container

Okay, so we have dockerized our django app and we need to run a command for some task. How do we do that? Simple, we have to locate the container that runs the django app, login and then run the command.

Locate The Container

It’s very likely that our app uses multiple containers to compose the entire system. For exmaple, I have one container running MySQL, one container running Redis and another running the actual Django app. If we want to run commands, we have to login to the one that runs Django.

While our app is running, we can find the running docker containers using the docker ps command like this:

In my case, I am using Docker Compose and I know my Django app runs using the crawler_web image. So we note the name of the container. In the above example, that is – crawler_web_1.

Nice, now we know which container we have to login to.

Logging Into The Container

We use the name of the container to login to it, like this:

The command above will connect us to the container and land us on a bash shell. Now we’re ready to run our command.

Running the command

We cd into the directory if necessary and then run the management command.


  • docker ps to list running containers and locate the one
  • docker exec -it [container_name] bash to login to the bash shell on that container
  • cd to the django project and run python [command]

3 Comments Django: Running management commands inside a Docker container

    1. maSnun

      That can be done if I need to run it once. I often need to run collectstatic, migrate and other tasks. Instead of running with exec, I prefer running them from a bash shell.

  1. Nick

    It’s probably best to run collectstatic and migrations and anything else in a separate container, with it’s own command.


    create a directory with the various scripts you wanna run. init, init_static, init_celery, etc…

    Then your logs become much cleaner. docker run --name my_app_static foo/bar /path/to/scripts/

    Then you know when the the process is done by looking at the exit code of the container. Exit 0, everything was perfect, exit anyting else, error look at the log and figure out what happened.

    Also you wanna think about the stability of your app, by deploying code that depends on a migration before you’ve deployed the migration. This way you deploy your migrations first, then you can roll release your app containers slowly starting new containers and shutting down the old ones.


Leave a Reply

Your email address will not be published. Required fields are marked *