I am trying to learn Django (a python framework for developing web) in docker container recently. After running the docker with port redirect
sudo docker run -p 8000 -t -i centos/django /bin/bash #in the docker now cd mysite python manage.py runserver
The output of Django server is
System check identified no issues (0 silenced). September 24, 2014 - 08:52:05 Django version 1.7, using settings 'oddjobs.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Then I use command sudo docker ps to find out the port number for host machine:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 08d01206d676 centos/oddjobs:run "/bin/bash" About an hour ago Up About an hour 0.0.0.0:49198->8000/tcp kickass_pare
but when using curl 127.0.0.1:49198 in host machine it just report “Connection Refused”
After searching in google, I only find one article which seems useful for me. But my problem is still there after I follow its steps for solution. With no choice, I have to read the documents of docker carefully and do my experiment step by step.
First, I run a nc-server in docker:
sudo docker run -p 8000 -t -i centos/django /bin/bash #in the docker now nc -l 8000 #listening on port 8000
Then using nc 127.0.0.1 8000 in host. It failed too. Why can’t nc client connect to server in docker even I followed the document of docker?After running netstat in docker, I find out the answer: my centos image is centos7 and the ‘nc’ in it will listen on ipv6 address by default. If anyone want to listen on ipv4 address, it should type in
nc -l 8000 -4
Now, the nc client could connect to server now.
But how to run Django server in ipv4 address? This article tells me the way. Now, it seems everything is ok. I start Django again with python manage.py runserver 127.0.0.1:8000 but it still could not be connected with nc client in host. Oh, the ip “127.0.0.1” and “0.0.0.0” is very different so I should run Django like:
python manage.py 0.0.0.0:8000
The browser in host could access the Django example site now.