Can't connect to django server running inside docker container (Docker for mac)
I have a docker container running on my system and I started using this command:
docker run -it -v ~/some/dir -p 8000:80 3cce3211b735 bash
Docker ps now lists this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
44de7549d38e 3cce3211b735 "bash" 14 minutes ago Up 14 minutes 22/tcp, 443/tcp, 8082/tcp, 0.0.0.0:8000->80/tcp hardcore_engelbart
Inside the container, I run my django app using the command: python manage.py runserver 80
But I cannot view the page using either of these:
1. localhost:8000
2. 127.0.0.1:8000
I understand that my port 8000 maps to 80 ports in the container. But why can't I access it. I am using docker for mac not docker toolbox. Please help and comment if you need more information.
source to share
Ok, so I found a solution to my problem. The problem is not the docker port mapping. The actual problem is this line:
python manage.py runserver 80
Starts the server at 127.0.0.1:80. The localhost inside the docker container is not the localhost on your machine. So the solution starts the server with this command:
python manage.py runserver 0.0.0.0:80
After that, I was able to access the webpage. If you are facing the same issue where you cannot connect to a django server running inside a docker container, you should try running the server on 0.0.0.0:port
. You will be able to access it in your browser using localhost:port
. Hope this helps someone.
source to share