This is my Django Docker Commands Cheat Sheet.

This cheat sheet has useful commands that are part of a docker django workflow. I also have a regular Django Commands Cheat Sheet, so please check that out if you are interested.

This sheet will focus on commands using docker compose.

Most commands are prefaced by docker-compose exec which executes a command on an existing docker container.

If the docker container is not up, you can use docker-compose run instead which will spin up a container to run the command. I will include both versions to make it easier for people to copy and paste directly from this sheet.

Load database from json file
1
2
docker-compose exec <django_container> python manage.py loaddata db.json
docker-compose run <django_container> python manage.py loaddata db.json

This command will load data into your database from a json file.

Dump database to json file
1
2
docker-compose exec <django_container> python manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json
docker-compose run <django_container> python manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json

This command will dump your database into a json file.

Launch Django Shell
1
2
docker-compose exec <django_container> python manage.py shell
docker-compose run <django_container> python manage.py shell

This command will launch a shell that allows you to run python code with the django context loaded.

Making Migrations
1
2
docker-compose exec <django_container> python manage.py makemigrations
docker-compose run <django_container> python manage.py makemigrations

This command will generate migration files based on changes to your models.

Making Named Migrations
1
2
docker-compose exec <django_container> python manage.py makemigrations --name <migration_name>
docker-compose run <django_container> python manage.py makemigrations --name <migration_name>

This command will let you specify a name for migrations when you generate them.

Restart containers
1
docker-compose restart

This command lets you restart all running docker containers.

Clear out old images when out of space
1
docker rmi $(docker images -q)

This command is really useful when you run out of space or simply want to remove old images.

Clear out old images when out of space
1
docker system prune --all

This command will remove pretty much everything including networks, dangling/unused images, and build cache. Volumes will kept in tact however, which is often the desired behavior.

I will continue to upgrade this cheat sheet with more docker and django commands overtime so please check back.

If you are interested in django, please check out this article about How To Upgrade To Django 2.

Have a great day!