I have created a simple app with Django, Python3, and Docker on Windows:
Dockerfile
FROM python:3.7.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
docker-compose.yml
version: '3.2.22'
services:
analyzer:
build:
context: ./analyser
command: bash -c "python ./analyser/manage.py migrate && python ./analyser/manage.py runserver 8000"
volumes:
- .:/code
ports:
- "8000:8000"
restart: always
In my case, I don’t need a DB. So I haven’t added the details of it and ‘depends_on’. I have read a few similar questions like this which says check the filename. My file names are ‘Dockerfile’ and ‘docker-compose.yml’. I think I don’t have any issues with filenames. I have tried like this ‘python ./analyser/manage.py runserver 0.0.0.0:8000’ But that also dosn’t seem to works
Looks like the Docker is starting but from my browser, I get this response
[+] Building 0.0s (0/0) docker:default
[+] Running 1/1
✔ Container version_1-analyser-1 Recreated 0.1s
Attaching to version_1-analyser-1
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
December 18, 2023 - 10:33:49
Django version 3.2.22, using settings 'analyser.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
But when I use the link in the browser it shows this:
localhost didn’t send any data.
ERR_EMPTY_RESPONSE
I am confused about what is that I am missing. I have little experience in docker for all those I followed the same procedure it worked. Please help
2
Answers
Ensure that the port 8000 on the Docker container is correctly mapped to the port 8000 on your local machine in the docker-compose.yml file. You have it set up correctly with "8000:8000"
It looks like both your
Dockerfile
anddocker-compose.yml
configuration looks correct to me.There is onle one problem that I spotted is the Django application you are running inside the docker container starts up the web-server on local interface, i.e
127.0.0.1
. That’s make it impossible to your browser to reach the server from your host machine.The easy fix is to start the Django on
0.0.0.0:8000
address instead. Don’t forget to restart the docker container by runningdocker compose restart analyzer
after you make any changes.It is possible that there is some other issues that need to be investivated. Don’t hesitate to update your questions with information as it come.