5

I'm trying to setup pythons django inside a dockerized environment

Dockerfile

FROM python:3.5-slim
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
RUN pip install --upgrade pip
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
EXPOSE 8000

I build this to django-app.

docker-compose.yml

django:
  container_name: django
  image: django-app:latest
  ports:
    - "8000:8000"
  volumes:
    - ./config/startup.sh:/startup.sh
    - ./app:/code
  command: /bin/sh /startup.sh

startup.sh

#!/bin/sh
cd /code
django-admin.py startproject djagento
echo "Creating Project djagento"
cd djagento
python manage.py runserver

I am using ubuntu 14.04 LTS as my host system

docker exec -it django bash $curl 127.0.0.1:8000 is succesfull.

I am getting the ip of my container with docker inspect django

    "Gateway": "172.17.0.1",
    "IPAddress": "172.17.0.2"

But when trying to curl 172.17.0.1:8000 or curl 172.17.0.2:8000 or curl 127.0.0.1:8000 I am not able to get anything but connection reset

Edit

docker ps

baada462ae72        django-app:latest   "/bin/sh /startup.sh"   6 seconds ago       Up 5 seconds        0.0.0.0:8005->8000/tcp   django

*ON another Port, same result *

CONTAINER ID        IMAGE               COMMAND                 CREATED              STATUS              PORTS                    NAMES
a9d99ee563e5        django-app:latest   "/bin/sh /startup.sh"   About a minute ago   Up About a minute   0.0.0.0:8005->8000/tcp   django

Thanks

xhallix
  • 243
  • 2
  • 8

2 Answers2

9

For anyone stumbling upon this, you have to run the django server on 0.0.0.0:8000 inside the docker container, simply adding python django-admin runserver 0.0.0.0:8000 in my startup.sh fixed this for me

xhallix
  • 243
  • 2
  • 8
  • I also needed to do this, but I also had to add `ALLOWED_HOSTS = [ '*' ]` to my `settings.py` file in Django. – mherzog Jun 15 '23 at 00:28
1

Is there anything else on your machine using port 8000? Try changing the port to 8005 or 9005 etc on the host machine. I tend to hate using the same port in general just because of confusion it sometimes causes.

In your dockerfile use

ports:
   - '8005:8000'

instance.

After:

  • run docker-compose up -d
  • run docker ps
  • See if you see 0.0.0.0:8005->8000/tcp for the container under PORTS.

Then try curling on the IP on port 8005.

If that still doesn't work check your IPTables on your container. Have you locked port 8000 to local only by accident?

CogitoErgoSum
  • 522
  • 5
  • 13
  • I tried this but it is not working on another port as well, I updated the docker ps output above – xhallix Feb 16 '16 at 21:24
  • Maybe you can let me know how I check if I locked a port to my local machine? – xhallix Feb 16 '16 at 21:29
  • Try curling localhost:8005 see what it says. Note do this from your machine not within the container. Also though it looks like no ports are showing up in your docker ps which is the biggest problem. Can you paste your entire docker ps output? That doesn't seem like a complete copy. – CogitoErgoSum Feb 16 '16 at 21:33
  • me@box-VirtualBox:~/Documents/DockerImages/django-docker$ curl localhost:8005 curl: (56) Recv failure: Connection reset by peer me@box-VirtualBox:~/Documents/DockerImages/django-docker$ curl 127.0.0.1:8005 curl: (52) Empty reply from server – xhallix Feb 16 '16 at 21:36
  • I've updated docker ps this in my question above – xhallix Feb 16 '16 at 21:39
  • thanks for your help, this was more a django config issue then a docker one :) – xhallix Feb 16 '16 at 22:18