Docker - Can't access Django server

扶醉桌前 提交于 2019-12-10 13:25:53

问题


I can not connect to django through the port in the container. I'm using this address: 0.0.0.0.:8000 and see: http://joxi.ru/Dr8MeGLhkBWnLm. I'm creating an image and a container with one command: 'docker-compose up -d'.

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
4fea50856eef        docker_web          "python manage.py ru…"   13 seconds ago      Up 11 seconds       0.0.0.0:8000->8000/tcp   docker_web_1

docker-compose.yaml

version: '3'

services:
  web:
    build:
      context: .
      dockerfile: /django.testsite/Dockerfile
    ports:
      - "8000:8000"

Dockerfile

FROM python:3

RUN easy_install pip
RUN pip install django==1.9.12
RUN pip install requests

ADD . /.
WORKDIR /django.testsite

CMD ["python", "manage.py", "runserver", "8000"]

How do I solve this issue?


回答1:


Your issue:

CMD ["python", "manage.py", "runserver", "8000"]

In order for it to work, you need to change it to:

CMD ["python", "manage.py", "runserver", "0.0.0.0", 8000"]

and finally, use http://127.0.0.1:8000/ in your browser.

Why? Well python thinks you're exposing it on 127.0.0.1 when in reality you want the eth0 address, since that can change, and we don't want to hard code it, we use 0.0.0.0 which means "ALL" interfaces.

Remember docker 127.0.0.1 IS NOT your host 127.0.0.1, each docker container has its own loopback.


To get it to work fully:

docker-compose down
docker rmi docker_web
docker-compose up -d

Or.. (i'm like 80% sure this is the syntax)

docker-compose down
docker-compose up -d --rebuild

If its really not cooperating, see docker ps -a and ensure there is NOTHING running. (ps. you can get rid of it using docker rm <id>; extra credit: docker ps -aq | xargs docker rm does it all for you ;) )

Extra: If you don't want to write out 0.0.0.0 you can simply write 0 -- does the same thing.



来源:https://stackoverflow.com/questions/49476217/docker-cant-access-django-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!