Hi! In this post i’m going to show you how to debug your Django code with Docker.

In a Django project with 3 images (django, postgres and redis) you can't debug your code.

Our project run without any issue:

$ docker-compose up Starting redis_1 ... done Starting postgres_1 ... done Starting celeryworker_1 ... done Starting django_1 ... done Starting flower_1 ... done Starting celerybeat_1 ... done Attaching to redis_1, postgres_1, celerybeat_1, django_1, celeryworker_1, flower_1 ... ... django_1 | Performing system checks... django_1 | django_1 | System check identified no issues (0 silenced). django_1 | August 25, 2019 - 02:29:42 django_1 | Django version 2.0.9, using settings 'config.settings.local' django_1 | Starting development server at http://0.0.0.0:8000/ django_1 | Quit the server with CONTROL-C.

But, if we put a breakpoint into our code (import pdb; pdb.set_trace()), we get this kind of error:

django_1 | Performing system checks... django_1 | django_1 | > /app/config/urls.py(12)<module>() django_1 | -> path(settings.ADMIN_URL, admin.site.urls), django_1 | (Pdb) django_1 | Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f959fef4400> django_1 | Traceback (most recent call last): django_1 | File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper ... ... django_1 | File "/app/config/urls.py", line 12, in <module> django_1 | path(settings.ADMIN_URL, admin.site.urls), django_1 | File "/app/config/urls.py", line 12, in <module> django_1 | path(settings.ADMIN_URL, admin.site.urls), django_1 | File "/usr/local/lib/python3.6/bdb.py", line 51, in trace_dispatch django_1 | return self.dispatch_line(frame) django_1 | File "/usr/local/lib/python3.6/bdb.py", line 70, in dispatch_line django_1 | if self.quitting: raise BdbQuit django_1 | bdb.BdbQuit

What do we have to do to resolve this? We need to run django server in other terminal instance:

These are the steps:

1- Open other terminal instance 2- Run docker-compose ps to view running containers÷ $ docker-compose ps django_1 /entrypoint /start Up 0.0.0.0:8000->8000/tcp postgres_1 docker-entrypoint.sh postgres Up 5432/tcp redis_1 docker-entrypoint.sh redis ... Up 6379/tcp

3- Run docker rm -f django_1 to remove containers 4- Then, run docker-compose run --rm --service-ports django: ``` $ docker-compose run --rm --service-ports django Starting postgres_1 ... done PostgreSQL is available Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: No migrations to apply. Performing system checks...

System check identified no issues (0 silenced). August 25, 2019 - 02:54:59 Django version 2.0.9, using settings 'config.settings.local' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C ```

5- Now, you can put a breakpoint! Save the changes and... ``` Performing system checks...

/app/config/urls.py(12)() -> path(settings.ADMIN_URL, admin.site.urls), (Pdb) ```

I hope you find it useful!