djangodockerdocker-for-mac

Can't connect to server running inside docker container (Docker for mac)


I have a docker container running on my system which i started using this command:

docker run -it  -v ~/some/dir -p 8000:80  3cce3211b735 bash

Now docker ps 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 am not able to view the page using either of these:

1.localhost:8000

2.127.0.0.1:8000

I do understand that my 8000 port is mapped to 80 port on the container. But why am i not able to access it. I am using docker for mac not docker toolbox. Please help and comment if you need any more info.


Solution

  • Okay so i found the solution to my problem. The issue was not in the docker port mapping. The actual problem is this line :

    python manage.py runserver 80
    

    This runs the server on 127.0.0.1:80 . The localhost inside the docker container is not the localhost on your machine . So the solution is running the server using this command :

    python manage.py runserver 0.0.0.0:80
    

    I was able to access the webpage after this. If you run into the same problem where you are not able to connect to the django server running inside your 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.