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
you need to expose port 8000 in your Docker file
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 toIn my case, I had to use the flag
--service-ports
when using docker composerun
app instead of docker composeup
, like this:My docker-compose.yml:
My ./scripts/.env:
My ./scripts/start-development.sh:
Related documentation
Related Docker compose git thread with more info