Don't see Django in Docker container

2019-03-05 12:35发布

I want to run Django in a simple Docker container.

First I built my container with Docker-file. There wasn't anything special in it (only FROM, RUN and COPY commands)

Then I ran my container with command

docker run -tid -p 8000:8000 --name <container_name> <image>

Entered my container:

docker exec -it <container_name> bash

Ran Django server:

python manage.py runserver

Got:

Starting development server at http://127.0.0.1:8000/

But when I go to 127.0.0.1:8000 I see nothing:

The 127.0.0.1 page isn’t working

There are no Nginx or other working servers.

What am I doing wrong?

Update 1 (Dockerfile)

FROM ubuntu:16.04
MAINTAINER Max Malyshev <user>
COPY . /root
WORKDIR /root
RUN apt-get update
RUN apt-get install python-pip -y
RUN apt-get install postgresql -y
RUN apt-get install rabbitmq-server -y
RUN apt-get install libpq-dev python-dev -y
RUN apt-get install npm -y
RUN apt-get install mongodb -y
RUN pip install -r requirements.txt

标签: django docker
3条回答
我想做一个坏孩纸
2楼-- · 2019-03-05 12:46

You need to expose port 8000 in your Dockerfile and run a WSGI server like gunicorn. If you follow the steps here you should be good... https://semaphoreci.com/community/tutorials/dockerizing-a-python-django-web-application

查看更多
Summer. ? 凉城
3楼-- · 2019-03-05 12:56

I agree with Niklaus9 comments. If I could suggest an enhancement try

python manage.py runserver [::]:8000 

The difference is that [::] supports ipv6 addresses.

I also noticed some packages for mongodb. If you want to test and dev locally you can create docker containers and use docker compose to test your app on your machine before deploying to dev/stage/prod environment.

You can find out more about how to set up a Django app linked to a database backend in docker on this tutorial http://programmathics.com/programming/docker/docker-compose-for-django/ (Disclaimer: I am the creator of that website)

查看更多
迷人小祖宗
4楼-- · 2019-03-05 13:03

The problem is that you're exposing the development server to 127.0.0.1 inside your Docker container, not on the host OS. If you access another console to your container and do a http request to 127.0.0.1:8000 it will work.

The key is to make sure the Docker container exposes the development server to all IPv4 addresses, you can do this by using 0.0.0.0 instead of 127.0.0.1.

Try running the following command to start your Django development server instead:

python manage.py runserver 0.0.0.0:8000

Also, for further inspiration, you can check out this working Dockerfile for hosting a Django application with the built-in development server https://github.com/Niklas9/django-unixdatetimefield/blob/master/Dockerfile.

查看更多
登录 后发表回答