I'm trying to put my Django project into a docker image.
This is my Dockerfile
:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt -i https://pypi.douban.com/simple
ADD . /code/
EXPOSE 8000
Then I build and run this docker container:
docker run -i -t -p 8000:8000 e2 python3 manage.py runserver 8000
Performing system checks...
System check identified no issues (0 silenced).
July 25, 2017 - 16:28:00
Django version 1.11.3, using settings 'chaoYuBackend.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
But when I visit localhost:8000
or 127.0.0.1:8000
in Chrome, I got ERR_CONNECTION_REFUSED
.
This is what I got by executing docker ps
, it seems that the 8000 port has been exposed:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
71c0502de704 e2 "python3 manage.py..." 54 seconds ago Up 52 seconds 0.0.0.0:8000->8000/tcp gallant_boyd
When I run lsof -i :8000
, I got an error:
My docker version is 17.06.0-ce, build 02c1d87
, and I'm using MacOS 10.12.6.
It's there anything wrong in my Dockerfile
? How can I visit my Django site in browser?
Thanks in advance!
Change the building/running command of docker to:
docker run -i -t -p 8000:8000 e2 python3 manage.py runserver 0.0.0.0:8000
The problem is that you haven't specified the listening host, so it's set directly to the loopback interface 127.0.0.1
, that's why it's just accessible just from the container itself.
So when you set the host to the broadcast address 0.0.0.0
it will be accessible for everyone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With