Docker cannot access Webserver on host

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

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

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?