I am new to Docker so I'm probably missing something obvious but here goes. I am trying to test a simple Django app with docker postgres. All I want right now is to verify that the home page is working on localhost. Debug output window gives me the following:
web_1 | System check identified no issues (0 silenced).
web_1 | April 28, 2020 - 17:06:23
web_1 | Django version 3.0.5, using settings 'app.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
However when I go to 0.0.0.0:8000 I get an error that says the site can't be reached
"The webpage at http://0.0.0.0:8000/ might be temporarily down or it may have moved permanently to a new web address.
ERR_ADDRESS_INVALID
Here is my docker-compose.yml:
version: '3.7'
services:
web:
build: .
command: python /app/manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- 8000:8000
depends_on:
- db
db:
image: "postgres:latest"
ports:
- "5432:5432"
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
Here is my dockerfile:
# Pull base image
FROM python:3.8
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /app
# Install dependencies
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# Copy project
COPY . /app/
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Would greatly appreciate any insight or help with troubleshooting. Because there are no errors in the debug window I'm not really sure where to start.
Thanks!
Had the same issue. Solved it by using Iain Shelvington's comment:
Try http://localhost:8000/ or http://127.0.0.1:8000/
Apparently 0.0.0.0 in the Dockerfile maps to localhost in the browser.
Related
I'm having a problem using Pycharm to run a Docker container to debug a Django project on MacOs.
The Pycharm setup is working fine to run the Django project inside a Docker container. But when I try to debug I'm having the following issue:
/usr/local/bin/docker-compose -f /Users/claudius/Studies/Pycharm/test-api/docker-compose.yaml -f /Users/claudius/Library/Caches/JetBrains/PyCharm2021.2/tmp/docker-compose.override.1494.yml up --exit-code-from web --abort-on-container-exit web
Docker Compose is now in the Docker CLI, try `docker compose up`
mongo is up-to-date
postgres is up-to-date
Recreating test-api_web_1 ...
Attaching to test-api_web_1
Connected to pydev debugger (build 212.4746.96)
web_1 | /usr/local/bin/python: can't find '__main__' module in ''
test-api_web_1 exited with code 1
Aborting on container exit...
ERROR: 1
Process finished with exit code 1
To set up the Pycharm I did:
Add a Python interpreter with docker-compose and the container of the web application (Django).
Add a Django config for the project.
Add a Run/Debugger config.
UPDATE
As requested by #Thy and #DanielM that is the Dockerfile:
FROM python:3.9.1 AS backend
ARG DJANGO_ENV
ENV DJANGO_ENV=${DJANGO_ENV} \
# pip:
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
# poetry:
POETRY_VERSION=1.1.7 \
POETRY_VIRTUALENVS_CREATE=false
# Set work directory
WORKDIR /pysetup
COPY ./pyproject.toml ./poetry.lock* /pysetup/
RUN pip install "poetry==$POETRY_VERSION"
RUN poetry install --no-interaction --no-ansi
# Copy project
COPY . /pysetup/
And that is the docker-compose.yaml:
# docker-compose.yml
version: '3.8'
services:
db:
image: postgres:12.0-alpine
env_file:
.env
ports:
- "5432:5432"
container_name: postgres
mongodb:
image: mongo:5.0.5
env_file:
- .env
ports:
- "27017:27017"
container_name: mongo
web:
build: .
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
env_file:
- .env
volumes:
- .:/test-api-volume
ports:
- 8000:8000
stdin_open: true
tty: true
depends_on:
- db
- mongodb
Does someone have any suggestions?
After I did can't debug with Docker, I tried to debug using virtual environments and I did receive the same message:
Connected to pydev debugger (build 212.4746.96)
/usr/local/bin/python: can't find '__main__' module in ''
So a work colleague suggested updating the IDE Pycharm.
And it just worked. Now I can debug with Docker or using virtual envs.
In other words, the problem was with IDE Pycharm Professional- version 2021.1.1 running on MacOS Monterey.
I am getting an error when I run PostGIS and Django in Docker. I am trying to set up a Django project to use PostGIS database.
docker-compose.yml
version: '3'
services:
db:
image: postgis/postgis
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5432:5432
web:
build: .
command: bash -c "
python manage.py makemigrations
&& python manage.py migrate
&& python manage.py runserver 0.0.0.0:8000
"
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
error message:
web_1 | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "db" (192.168.192.2) and accepting
web_1 | TCP/IP connections on port 5432?
web_1 |
ht_djangoapp_web_1 exited with code 1
It's most likely a timing issue: your application is trying to connect to the database before the database is ready. The easiest solution here is probably to just set a restart policy on your application container, so that docker will restart it when it fails. You might as well get rid of the depends-on directive, because this is functionally useless: while docker knows when the database container is up, it knows nothing about the state of the database application itself.
web:
build: .
command: bash -c "
python manage.py makemigrations
&& python manage.py migrate
&& python manage.py runserver 0.0.0.0:8000
"
volumes:
- .:/code
ports:
- "8000:8000"
restart: on-failure
- db web:
build: .
command: bash -c "
python manage.py makemigrations
&& python manage.py migrate
&& python manage.py runserver 0.0.0.0:8000
"
volumes:
- .:/code
ports:
- "8000:8000"
restart: on-failure
An alternative solution is to put a script into your application container that explicitly waits for the database to respond before starting up the web application.
The problem with this solution is that you may want to be able to restart the database container (for example, to upgrade to a newer version of postgres), in which case your application will probably fail again, unless it has reconnect logic built in. Using the restart policy is probably the better choice.
I have a hello world Django project and i want to dockerize it. My OS is windows 8.1 and I'm using docker toolbox. Using volumes I could persist data in docker container and what I want to do is to sync the code in docker container with the code in my local host in the directory where my project code is stored and so far I couldn't do it.
Here is my docker-compose.yml:
version: '3.7'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- myvol1:/code
ports:
- 8000:8000
volumes:
myvol1:
and Dockerfile:
# Pull base image
FROM python:3.7
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY requirement.txt /code/
RUN pip install -r requirement.txt
# Copy project
COPY . /code/
without using volumes I can run my code in the container but the data is not persisted.
I'd be grateful for your help.
Maybe try
version: '3.7'
services:
web:
build: .
command: python manage.py runserver 127.0.0.1:8000
volumes:
- myvol1:/code
ports:
- 8000:8000
volumes:
myvol1:
I thought maybe changing to the localhost IP might help or the ports could also be changed following the format of
<port-number-host> : <port-number-container>
"your listening port : container's listening port"
The port might be busy, but these are things that I would troubleshoot and try.
My resources/references: Udemy Class from Bret Fisher
I am tried setting up django using docker but I am getting this error.
I have set up my Dockerfile and the docker-compose.yml.
docker-compose.yml
version: "3"
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install --no-cache-dir -r requirements.txt
COPY . /code/
This is the error I am getting.
Watching for file changes with StatReloader
web_1 | Exception in thread django-main-thread:
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.7/threading.py", line 917, in _bootstrap_inner
I think This particular error was fixed in django 2.1
You'll want to upgrade your requirements to be django>=2.1 to ensure you get this new version.
The comments on the commit indicate that this patch will not be backported to django 1.11.x which does not support python3
after change django version re-ran docker-compose build.
Per the FAQ, Django 1.11.x is not compatible with Python 3
Django 1.11.x reached end of mainstream support on December 2, 2017
and it receives only data loss and security fixes until its end of
life.
At firt time it worked totally fine but it is showing this issue now:
app$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
September 03, 2017 - 17:28:43
Django version 1.11.4, using settings 'aggregator.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
It says its running but I can't Access it on that address. What could have been possibly wrong?
my docker-compose file:
version: "2"
services:
postgres:
image: postgres:9.6
volumes:
- pgdata:/var/lib/data/postgres
backend:
build: .
command: gosu app bash
volumes:
- .:/app
- pyenv:/python
links:
- postgres:postgres
ports:
- 8000:8000
volumes:
pyenv:
pgdata:
For development, you can try setting this in settings.py:
ALLOWED_HOSTS = ['*']
and then start your server locally by running:
python manage.py runserver 0.0.0.0:8000
To answer your question of
What could have been possibly wrong?
A number of things:
The mapping of your parts is incorrect. Your application is mapping to another port other than 8000. Check which port your application is mapped to. Not by checking the output of python manage.py runserver, because that is not the command you run in your container. Check the logs of your container.
When your command gosu app bash is run, there is an error. Check the output of docker logs backend.
You could be running the docker-compose up -d command incorrectly.
Post the out put of the logs if you want more information.