skip to Main Content

I am trying to build and run django application with docker and docker-compose.
docker-compose build example_app and docker-compose run example_app run without errors, but when I go to http://127.0.0.1:8000/ page doesn’t open, I’m just getting "page is unavailable" error in the browser.
Here is my Dockeffile, docker-compose.yml and project structure

Dockerfile

FROM python:3.9-buster

RUN mkdir app
WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
COPY ./requirements_dev.txt /app/requirements_dev.txt
RUN pip install --upgrade pip
RUN pip install -r /app/requirements.txt

docker-compose.yml

version: '3'

services:
  example_app:
    image: example_app
    build:
      context: ../
      dockerfile: ./docker/Dockerfile
    command: bash -c "cd app_examples/drf_example && python manage.py runserver"
    volumes:
      - ..:/app
    ports:
      - 8000:8000

project structure:

──app
──app_examples/drf_example/
  ────manage.py
  ────api
  ────drf_example
──requirements.txt
──requirements_dev.txt
──docker/
  ────docker-compose.yml
  ────Dockerfile

3

Answers


  1. you need to expose port 8000 in your Docker file

    FROM python:3.9-buster
    
    EXPOSE 8000
    
    RUN mkdir app
    WORKDIR /app
    COPY ./requirements.txt /app/requirements.txt
    COPY ./requirements_dev.txt /app/requirements_dev.txt
    RUN pip install --upgrade pip
    RUN pip install -r /app/requirements.txt
    
    Login or Signup to reply.
  2. By default, Django apps bind to 127.0.0.1 meaning that they’ll only accept connections from the local machine. In a container context, the local machine is the container, so your app won’t accept connections from outside the container.

    To get it to accept connections from anywhere, you add the bind address to the runserver command. In your case, you’d change the command in your docker-compose.yml file to

    command: bash -c "cd app_examples/drf_example && python manage.py runserver 0.0.0.0:8000"
    
    Login or Signup to reply.
  3. In my case, I had to use the flag --service-ports when using docker compose run app instead of docker compose up, like this:

    docker compose run --service-ports app
    

    My docker-compose.yml:

    services:
      app:
        build:
          context: .
          dockerfile: Dockerfile.dev
        env_file: .env
        volumes:
          - .:/app
        ports:
          - "${DJANGO_BIND_PORT}:${DJANGO_BIND_PORT}"
        command: [
          "./scripts/start-development.sh"]
    

    My ./scripts/.env:

    DJANGO_BIND_PORT=8000
    

    My ./scripts/start-development.sh:

    set -e
    
    python manage.py makemigrations
    python manage.py migrate
    python manage.py seed_db --create-super-user
    
    python manage.py runserver 0.0.0.0:${DJANGO_BIND_PORT:-8000}
    

    Related documentation

    Related Docker compose git thread with more info

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search