2

My django application works fine when I run it locally but it doesn't work when I run it on a container. Is it a port problem? When I run it as a local application (without docker) I point it to localhost:8000/polls and it works like a charm. But I get "The localhost page isn’t working localhost didn’t send any data. ERR_EMPTY_RESPONSE" when I run the container. The application is pretty much verbatim from the Django tutorial at https://docs.djangoproject.com/en/1.10/intro/tutorial01/

O/S Windows 10
Python 3.6
Docker 17.03.0-ce

UPDATED to include docker inspect output (the container ip address doesn't work either):

C:\Users\jonathan>docker inspect xenodochial_swanson [ { "Id": "0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f14775dfd506a7a3f", "Created": "2017-03-21T03:27:42.432908Z", "Path": "/bin/sh", "Args": [ "-c", "python /usr/src/toplevel/manage.py runserver" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 3415, "ExitCode": 0, "Error": "", "StartedAt": "2017-03-21T03:27:42.933502Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:18be11864bbd096c71343d6621712d2dfed06a3e0bef0d72bea3e3bd97d94509", "ResolvConfPath": "/var/lib/docker/containers/0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f14775dfd506a7a3f/resolv.conf", "HostnamePath": "/var/lib/docker/containers/0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f14775dfd506a7a3f/hostname", "HostsPath": "/var/lib/docker/containers/0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f14775dfd506a7a3f/hosts", "LogPath": "/var/lib/docker/containers/0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f14775dfd506a7a3f/0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f14775dfd506a7a3f-json.log", "Name": "/xenodochial_swanson", "RestartCount": 0, "Driver": "overlay2", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": [ "472a4b94af64b347ff34e6d98875bf16e183d04974d7497f7c6d68469398d70e" ], "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": { "8000/tcp": [ { "HostIp": "", "HostPort": "8000" } ] }, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 30, 120 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": null, "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DiskQuota": 0, "KernelMemory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": -1, "OomKillDisable": false, "PidsLimit": 0, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0 }, "GraphDriver": { "Name": "overlay2", "Data": { "LowerDir": "/var/lib/docker/overlay2/61d1577ad2925d86ae68c5bb2140aa7b85f4d2849866a4875a8a50f35726c7af-init/diff:/var/lib/docker/overlay2/70d4d830664d3a9da7f1c4061421924fb27c547e771292961296b6f068549d94/diff:/var/lib/docker/overlay2/efcad5a908608693833608bb6874cd198c625e9f7dbef72a3a2165fea42c4189/diff:/var/lib/docker/overlay2/8553d226e111b5d9837656d1bfef7c6e4aa7a02280d154824db3b05dded594d9/diff:/var/lib/docker/overlay2/de3b36b0203b5d17ecd88106a54ae87fe49d88fb1bec04cf5641c1a9d92051fd/diff:/var/lib/docker/overlay2/9ff4cd59d0321627d952a01c2e97f09e9b0cee6b58b69ac1ef89ce1012fc3ca0/diff:/var/lib/docker/overlay2/52161c423deed2bc1a58fbb5252332c0bf68ebe4f8843ded1679f1599d9a09b6/diff:/var/lib/docker/overlay2/ccf0d30cc04d29769faaaf0d3abc4c822ddea6caa62ff21a67629b74fdf5ccea/diff:/var/lib/docker/overlay2/9213e08c956581cdca5f99efa2632dfb1c2eb86684f7d0b4138c86a8121f43ca/diff:/var/lib/docker/overlay2/eca56289a13e584416695b55c77907dbe40e24e726d672f5a5cf40be95521b27/diff:/var/lib/docker/overlay2/52b015f1e0d4bf4b00ec4336091f8bc6c49c1407c6f270e56aa5c2931dd18994/diff", "MergedDir": "/var/lib/docker/overlay2/61d1577ad2925d86ae68c5bb2140aa7b85f4d2849866a4875a8a50f35726c7af/merged", "UpperDir": "/var/lib/docker/overlay2/61d1577ad2925d86ae68c5bb2140aa7b85f4d2849866a4875a8a50f35726c7af/diff", "WorkDir": "/var/lib/docker/overlay2/61d1577ad2925d86ae68c5bb2140aa7b85f4d2849866a4875a8a50f35726c7af/work" } }, "Mounts": [], "Config": { "Hostname": "0394ca73fb06", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": true, "AttachStderr": true, "ExposedPorts": { "8000/tcp": {} }, "Tty": true, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "LANG=C.UTF-8", "GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D", "PYTHON_VERSION=3.6.0", "PYTHON_PIP_VERSION=9.0.1" ], "Cmd": [ "/bin/sh", "-c", "python /usr/src/toplevel/manage.py runserver" ], "ArgsEscaped": true, "Image": "django-polls:latest", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "ab70160de420857c339a85aeb99a72533b2fd382606e08e39921dc3efcece6da", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "8000/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "8000" } ] }, "SandboxKey": "/var/run/docker/netns/ab70160de420", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "e52b41debee6e478c638440cdf930c1a63f7a2c5a588010cd0f47fb1eea47b05", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "d494fc6cb4c7c4af4bb936d8a89087bce1883e64cfad5c34c52775125dc13555", "EndpointID": "e52b41debee6e478c638440cdf930c1a63f7a2c5a588010cd0f47fb1eea47b05", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02" } } } } ]

Dockerfile:

FROM python:3.6
EXPOSE 8000
COPY ./ /usr/src/
RUN pip install -r /usr/src/requirements.txt
RUN pip install /usr/src/django-polls/dist/django-polls-0.1.tar.gz
CMD python /usr/src/toplevel/manage.py runserver 

Running the Container (no visible problems):

C:\temp\djangopoc>docker run -p 8000:8000 -t django-polls
Performing system checks...
System check identified no issues (0 silenced).
March 21, 2017 - 01:05:05
Django version 1.10.6, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Troubleshooting info? (notice the port says 0.0.0.0:8000) problem?:

C:\temp\djangopoc>docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d28b7c336664        django-polls        "/bin/sh -c 'pytho..."   About an hour ago   Up About an hour    0.0.0.0:8000->8000/tcp   xenodochial_boyd

In case it's helpful to diagnose, Build Instructions and output:

C:\temp\djangopoc>docker build -t django-polls:latest .
Sending build context to Docker daemon 4.948 MB
Step 1/6 : FROM python:3.6
3.6: Pulling from library/python
Digest: sha256:438208801c4801efbf8b0e318ff6548460b27bd1fbcb7bb188273d13871ab43f
Status: Downloaded newer image for python:3.6
 ---> a1782fa44ef7
Step 2/6 : EXPOSE 8000
 ---> Running in cde33b9d347d
 ---> 34ebaa1cfd3c
Removing intermediate container cde33b9d347d
Step 3/6 : COPY ./ /usr/src/
 ---> a6b591e2fd0b
Removing intermediate container e45a71d06c9a
Step 4/6 : RUN pip install -r /usr/src/requirements.txt
 ---> Running in 9d995a9129fb
Collecting appdirs==1.4.2 (from -r /usr/src/requirements.txt (line 1))
...
Collecting numpy>=1.7.0 (from pandas==0.19.2->-r /usr/src/requirements.txt (line 23))
  Downloading numpy-1.12.1-cp36-cp36m-manylinux1_x86_64.whl (16.8MB)
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/site-packages (from pyramid==1.8.2->-r /usr/src/requirements.txt (line 28))
Installing collected packages: appdirs, six, python-dateutil, arrow, docutils, colorama, PyYAML, jmespath, botocore, s3transfer, pyasn1, rsa, awscli, chardet, binaryornot, click, MarkupSafe, Jinja2, jinja2-time, whichcraft, poyo, future, cookiecutter, decorator, Django, funcsigs, hupper, mysql-connector, pyparsing, packaging, pytz, numpy, pandas, PasteDeploy, zope.deprecation, translationstring, WebOb, venusian, zope.interface, repoze.lru, pyramid
  Running setup.py install for arrow: started
    Running setup.py install for arrow: finished with status 'done'
  Running setup.py install for PyYAML: started
    Running setup.py install for PyYAML: finished with status 'done'
...
  Running setup.py install for repoze.lru: started
    Running setup.py install for repoze.lru: finished with status 'done'
Successfully installed Django-1.10.6 Jinja2-2.9.5 MarkupSafe-0.23 PasteDeploy-1.5.2 PyYAML-3.12 WebOb-1.7.1 appdirs-1.4.2 arrow-0.10.0 awscli-1.11.55 binaryornot-0.4.0 botocore-1.5.18 chardet-2.3.0 click-6.7 colorama-0.3.7 cookiecutter-1.5.1 decorator-4.0.11 docutils-0.13.1 funcsigs-1.0.2 future-0.16.0 hupper-0.4.2 jinja2-time-0.2.0 jmespath-0.9.1 mysql-connector-2.1.4 numpy-1.12.1 packaging-16.8 pandas-0.19.2 poyo-0.4.0 pyasn1-0.2.3 pyparsing-2.1.10 pyramid-1.8.2 python-dateutil-2.6.0 pytz-2016.10 repoze.lru-0.6 rsa-3.4.2 s3transfer-0.1.10 six-1.10.0 translationstring-1.3 venusian-1.0 whichcraft-0.4.0 zope.deprecation-4.2.0 zope.interface-4.3.3
 ---> dbc28b5002d5
Removing intermediate container 9d995a9129fb
Step 5/6 : RUN pip install /usr/src/django-polls/dist/django-polls-0.1.tar.gz
 ---> Running in 27d40c4a746a
Processing /usr/src/django-polls/dist/django-polls-0.1.tar.gz
Installing collected packages: django-polls
  Running setup.py install for django-polls: started
    Running setup.py install for django-polls: finished with status 'done'    
Successfully installed django-polls-0.1
 ---> 1771f5ff2479
Removing intermediate container 27d40c4a746a
Step 6/6 : CMD python /usr/src/toplevel/manage.py runserver
 ---> Running in d754737737ec
 ---> 18be11864bbd
Removing intermediate container d754737737ec
Successfully built 18be11864bbd
SECURITY WARNING: You are building a Docker image from Windows against a
 non-Windows Docker host. All files and directories added to build context
 will have '-rwxr-xr-x' permissions. It is recommended to double check and
 reset permissions for sensitive files and directories.
Jonathan
  • 491
  • 5
  • 19
  • i guess your container must be using docker machine which has different ip, try to check using `docker-machine ip default`, where `default` is docker-machine name – iamdeowanshi Mar 21 '17 at 02:40
  • Try opening your Preferred browser with 0.0.0.0:8000 – julian salas Mar 21 '17 at 02:44
  • tried 0.0.0.0:8000 and it didn't work. also tried docker-machine ip [default] . pretty much anything I type there (machine name, ip address, 0:0:0:0, 128.0.0.1) all says "Host does not exist" – Jonathan Mar 21 '17 at 03:10

3 Answers3

0

When running application on docker machine you should be using docker-machine ip instead of localhost

use : docker-machine ip default to get docker machine ip, where default is your machine

iamdeowanshi
  • 1,039
  • 11
  • 19
  • I'm not sure what I'm doing with this command, but I tried it and it got an error. I tried this for 0.0.0.0 and 128.0.0.1 Same error C:\temp\djangopoc>docker-machine ip 192.168.1.90 Host does not exist: "192.168.1.90". – Jonathan Mar 21 '17 at 03:00
  • if you are running `docker run -p 8000:8000 -t django-polls`, then use `192.168.1.90:8000` – iamdeowanshi Mar 21 '17 at 03:06
  • `docker-machine ls` should list the docker machines – iamdeowanshi Mar 21 '17 at 03:07
  • when I type that it would appear as though I don't have any machines? Nothing is returned. C:\temp\djangopoc>docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS – Jonathan Mar 21 '17 at 03:14
  • i guess you don't have any machine created on your system, check more on docker machine – iamdeowanshi Mar 21 '17 at 03:17
  • check this https://docs.docker.com/machine/drivers/hyper-v/#environment-variables-and-default-values – iamdeowanshi Mar 21 '17 at 03:18
0

Have you tried looking at the logs inside of the container running your django application? To get into the container use docker exec -it django-polls bash.

boulderz
  • 133
  • 1
  • 8
  • Tried it and got this feedback: C:\temp\djangopoc>docker exec -it django-polls bash Error response from daemon: No such container: django-polls – Jonathan Mar 21 '17 at 03:05
  • Did you do it when your container was running? Whatever your image name is when you run `docker ps` is what you should use in place of `django-polls` – boulderz Mar 21 '17 at 03:26
  • ah, got it. I'm in the shell. The app doesn't do any logging, but I executed the test algorithms from the shell and it passed all of the tests. – Jonathan Mar 21 '17 at 04:26
0

Maybe you could try to use the following command:

CMD python /usr/src/toplevel/manage.py runserver 0.0.0.0:8000

【ref1】What is the difference between 0.0.0.0, 127.0.0.1 and localhost?