Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access 8000 port of a Django project in docker

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:

lsof result

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!

like image 995
awmleer Avatar asked Oct 19 '25 13:10

awmleer


1 Answers

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.

like image 104
Tenma Kenzo Avatar answered Oct 22 '25 01:10

Tenma Kenzo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!