Test Django Applications Using Docker Containers

Earlier I had an issue in which my machine wasn’t able to connect to the Django webserver (runserver) inside the container, even though I had the correct port published using Docker, so, this is the solution.

By default the app runs on 127.0.0.1:8000 and has ALLOWED_HOSTS = [] this means that the app will only allow connections from within the container but since we want to access to the application from our local machine we need to change the following:

Under <yourappname>/settings.py :

ALLOWED_HOSTS = [] to ALLOWED_HOSTS = [‘*’]

And when you run the application pass the following arguments:

# python manage.py runserver 0.0.0.0:8000

Instead of ‘binding’ to the default 127.0.0.1:8000 by using 0.0.0.0 it will bind to every interface (lo, ethX, etc).

Hope it helps!

Refs:

Leave a Reply

Your email address will not be published. Required fields are marked *