Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 on 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 .

like image 639
Sourav Prem Avatar asked May 20 '17 19:05

Sourav Prem


People also ask

Can I SSH into my Docker container?

The SSH method works fine for Docker containers, too. That said, you can SSH into a Docker container using Docker's built-in docker exec . If you do not need an interactive shell, you can also use the docker attach command to connect the host's stdin and stdout to the running container and execute remote commands.


1 Answers

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.

like image 93
Sourav Prem Avatar answered Oct 01 '22 23:10

Sourav Prem