Patrick Farrar

Docker, Django, Pycharm Setup - Setting up to work together with debugging

development django python docker pycharm

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 #

  1. Open up the Django application you created above in Pycharm as a new project.

  2. In Pycharm, goto Preferences | Build, Execution, Deployment | Docker, and set Connect to Docker daemon with to Docker for Mac

    Connect to Docker Daemon

  3. Next, goto Preferences | Project: <project_name> | Project Interpreter and click the Add 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.

    Add Project Interpreter

  4. Select Docker Compose and make sure Configuration file matches the path to the docker-compose.yml in project and select web in service to run when it is started.

    Set docker compose configuration

  5. Click ok , then click apply. This should make it so your project interpreter is correctly set up

Setting up Run and Edit Configurations #

  1. In Pycharm, goto Run | Edit Configurations Click Add from plus sign in the left corner and select Django Server.

    Setting up run edit configuration

  2. Set correct configurations values - (These worked for me)

    • Set host to | Port 8000
    • Check and set Run browser (optional)
    • Set correct enviornment variables
    • Make sure Python interpreter matches Docker Compose interpreter

    Setting up run edit configuration project values

  3. 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.

← Home