Docker, Django, Pycharm Setup - Setting up to work together with debugging
Why? #
The why here is that running Django in a Docker container is a common thing for development and many Python developers find themselves using Pycharm as an IDE of choice. When I was exploring this setup personally it took me some time sifting through articles and different readme docs - so I hope at minimum the walkthrough can help benefit someone and save some time.
Assumptions #
Django #
I found that the official documentation on the Docker website for setting up a Django project was spot on for this here is a link to it. The steps are straightforward and the end of running through that tutorial you should have a Docker container running with a local Django application inside.
This is where things got tricky for me.
Setting up Pycharm for Running and Debugging Django application in Docker #
This assumes you have Pycharm installed and the Docker Plugin for Pycharm as well.
Setting up Project Interpreter #
-
Open up the Django application you created above in Pycharm as a new project.
-
In Pycharm, goto
Preferences | Build, Execution, Deployment | Docker
, and setConnect to Docker daemon with
toDocker for Mac
-
Next, goto
Preferences | Project: <project_name> | Project Interpreter
and click theAdd
from the gear icon in top right corner. This will open a menu where you will add the correct Docker container as the project interpreter. -
Select
Docker Compose
and make sure Configuration file matches the path to thedocker-compose.yml
in project and selectweb
in service to run when it is started. -
Click ok , then click apply. This should make it so your project interpreter is correctly set up
Setting up Run and Edit Configurations #
-
In Pycharm, goto
Run | Edit Configurations
ClickAdd
from plus sign in the left corner and selectDjango Server.
-
Set correct configurations values - (These worked for me)
- Set host to 0.0.0.0 | Port 8000
- Check and set
Run browser
(optional) - Set correct enviornment variables
- Make sure Python interpreter matches
Docker Compose
interpreter
-
Run the Configuration - from top right click play icon to start environment. This should start up the web and db services in docker and open a new browser window with the docker application running in it. Clicking the bug icon - will run application in debug mode and that should allow for hitting breakpoints in the codebase.
This should now give you a Django project running inside of a Docker Container that s configured with Pycharm to have debugging working which is very nice. Being able to set breakpoints and see how code is running in realtime is very powerful.
I hope this helps someone. This setup took me several hours to figure out and things may change. Here are a list of the resource I used to figure out this process.
- Docker Doc - Quickstart: Compose and Django - https://docs.docker.com/compose/django/
- PyCharm Docs - Configure an interpreter using Docker Compose - https://www.jetbrains.com/help/pycharm/using-docker-compose-as-a-remote-interpreter.html#example
- PyCharm Docs - Docker- https://www.jetbrains.com/help/pycharm/docker.html#interacting-with-containers