Daily Django with Docker

Commands for common tasks

Albert O'Connor
Bungalow Garage
3 min readFeb 22, 2019

--

There are many posts about how to setup Docker and Django for the first time. I wanted to focus on what happens next. How to manage Django, view output, and debug interactively; tasks which are more obvious when you run Django directly on your command line and the output is right there in front of you.

I am going to assume you are using docker-compose and are running multiple containers.

What is going on right now?

Similar to git status, ps is a go-to command for understanding the state of your world. It shows which containers are running, or not running, and what ports are mapped.

When you run your containers with docker-compose up -d, all of your output is hidden, since -dis for daemon. This is why some people prefer to drop the -d. I find the output from all the containers at once can be overwhelming.

Instead, you can use the log command to see the output of a specific container. Use -f to tail the log so you can see the output in real-time. Note you can’t interact with it. Hit Control-C to stop tailing the log, your container will keep running.

Updating a requirement

You have made a change to your requirements.txt file which you want to make sure gets applied in your Django based images.

There are likely shortcuts, but this is my sure-fire way to make sure things are rebuilt.

Running Django Commands

Django provides a number of handy commands from migrating the database to interacting with the shell. To run them in your container you have two choices, docker-compose run and docker-compose exec.

run will launch a new container and run the command. exec will try to run it in an existing container. If no container is running it will give an error.

These days I find myself leaning on exec more. The downside is your Django container needs to be running, but docker-compose ps will tell you if that is true or not.

This will run Django core management command and output the commands available. You can run shell or migrate easily:

Debugging into Django

First add import pdb; pdb.set_trace(), or breakpoint() if you are using Python 3.7+, to your code. Assuming your Django container is already running, the easiest way I have found so far is to attach to the running container. You have to use the core docker command since docker-compose does not support this yet.

You will need the container id which you can get from docker ps. This shows you information about all docker images running on your machine rather than just the ones defined in a local docker-compose.yml file. Once you have it, attaching is pretty straightforward.

Now you can see output and interact. Warning: Control-C will terminate the container. You can use Control-P Control-Q to detach, but who is going to remember that?

This concludes our little tour of commands you need day to day to use Django with Docker.

One last tip, when you get tired of typing docker-compose you can alias it to dc or something similar. dc is apparently an arbitrary precision calculator you will lose access to.

Let me know on Twitter what your favorite ways to Docker with Django are!

Enjoy working with Docker and Django? Come join our growing team: https://bungalow.com/careers. Don’t see a role that is a perfect fit, please apply to our dream job!

--

--