Django in Docker using PyCharm

django, docker, pycharm, python

Solution

Assuming you have the latest Docker (for Mac or for Windows) along with an updated version of PyCharm, you could achieve the port forwarding (binding) this way:

- Create a new run configuration

- Select your Docker server in the Deployment tab. If nothing shows, create a new one. Test that it actually works by clicking View > Tools Windows > Docker and connecting to the docker server. You should see the existing images and running containers.

- In the Container tab, make sure to add the right Ports bindings.

An important note

Make sure that you are running your Django server on `0.0.0.0:8000` and not `localhost:8000`

Problem

I'm trying to develop a database manager in Django and want to develop and deploy it in Docker. As my IDE, I'd like to continue using PyCharm, but I'm having trouble understanding how it interacts with Docker. I am new to Docker and its integration in PyCharm. My system runs Windows 10 and Docker for Windows. I already tried using PyCharm's remote interpreter, but I have to activate the port forwarding manually (using Kitematic), since PyCharm does somehow not forward the exposed port automatically. I also tried using a "Docker Deployment" run configuration. However, I can't get requests to `localhost:8000` to get through to the Django server. All I get are empty response errors. (Note: The bold issue was addressed in the accepted answer.) It would really help me to have an explanation of how PyCharm's two options (remote interpreter and docker deployment) really work and ideally have an up-to-date tutorial for setting up Django with it. Unfortunately I could only find outdated tutorials and JetBrain's help pages are either outdated or do not explain it in enough detail. Could someone help me out and guide me through this or point me to good resources?

Original source