Django runserver on Docker does not respond when opened in the browser - django

I am just a beginner at this
So when I run
sudo docker-compose run web python manage.py runserver
it shows
Starting thirddj_db_1 ... done
usr/local/lib/python3.6/site-packages/psycopg2/__init__.py:144:
UserWarning: The psycopg2 wheel package will be renamed from release
2.8; in order to keep installing from binary please use "pip install
psycopg2-binary" instead. For details see:
<http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py:144:
UserWarning: The psycopg2 wheel package will be renamed from release
2.8; in order to keep installing from binary please use "pip install
psycopg2-binary" instead. For details see:
<http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
Performing system checks...
System check identified no issues (0 silenced).
April 11, 2018 - 19:15:59
Django version 1.11.12, using settings 'composeexample.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
And then when I opened it in my browser, it shows the site cant be reached
But when i run
docker-compose up
it shows
web_1 | /usr/local/lib/python3.6/site-
packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel
package will be renamed from release 2.8; in order to keep installing
from binary please use "pip install psycopg2-binary" instead. For
details see: <http://initd.org/psycopg/docs/install.html#binary-
install-from-pypi>.
web_1 | """)
web_1 | /usr/local/lib/python3.6/site-
packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel
package will be renamed from release 2.8; in order to keep installing
from binary please use "pip install psycopg2-binary" instead. For
details see: <http://initd.org/psycopg/docs/install.html#binary-
install-from-pypi>.
web_1 | """)
web_1 | Performing system checks...
web_1 |
web_1 | System check identified no issues (0 silenced).
web_1 | April 11, 2018 - 19:21:41
web_1 | Django version 1.11.12, using settings
'composeexample.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
And then when I opened it in my browser, it works
so i want to know Why sudo docker-compose run web python manage.py runserver is not working and what is the difference between both commands.
and here is my docker-compose file
version: '3'
services:
db:
image: postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db

When you run the web container it's necessary run command with the service's ports enabled and mapped to the host.
Try this:
sudo docker-compose run --service-ports web python manage.py runserver

Related

IP error: Django app not deploying with docker

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.

Why does Django run server hang when using docker-compose up?

When I run docker-compose up, Django starts to load. It goes thru the about four steps. After it does a system check and identifies (0 issues), it stops at displaying the current date and time. This is the point where it just hangs. Nothing else happens.
C:\Users\********\Desktop\code\hello>docker-compose up
Creating hello_web_1 ... done
Attaching to hello_web_1
web_1 | Watching for file changes with StatReloader
web_1 | Performing system checks...
web_1 | System check identified no issues (0 silenced).
web_1 | August 21, 2019 - 17:33:11
When I do docker-compose down, the system exits Gracefully(lol). Then I run docker-compose logs and it shows the correct info:
C:\Users\********\Desktop\code\hello>docker-compose logs
Attaching to hello_web_1
web_1 | Watching for file changes with StatReloader
web_1 | Performing system checks...
web_1 | System check identified no issues (0 silenced).
web_1 | August 21, 2019 - 17:36:44
web_1 | Django version 2.2.3, using settings 'hello_project.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
web_1 | Watching for file changes with StatReloader
web_1 | Performing system checks...
web_1 | System check identified no issues (0 silenced).
web_1 | August 21, 2019 - 17:55:22
web_1 | Django version 2.2.3, using settings 'hello_project.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
Here is what my Dockerfile and docker-compose.yml look like:
#Dockerfile:
#Pull base image
From python:3.7-slim
#Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
#Set work directory
WORKDIR /code
#Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system
#Copy project
COPY . /code/
#*docker-compose.yml:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"```
note I'm using version 1 of docker-compose, that's why my web service is at the root of the file. I also omitted the version per Docker documentation.

Django uninstalled after installing django-debug-toolbar in docker container, how to install it without un installing django?

I was trying to install django debug toolbar in my container using
[shub#debian teamwave](task-details-api)$ docker exec -it teamwave_backend_1 pip install django-debug-toolbar Collecting django-debug-toolbar Downloading https://files.pythonhosted.org/packages/01/9a/3db232bd15882d90d3c53de1f34ce0a522327849593c9198899713267cfe/django_debug_toolbar-1.11-py2.py3-none-any.whl (201kB)
100% |████████████████████████████████| 204kB 426kB/s Collecting sqlparse>=0.2.0 (from django-debug-toolbar) Downloading https://files.pythonhosted.org/packages/ef/53/900f7d2a54557c6a37886585a91336520e5539e3ae2423ff1102daf4f3a7/sqlparse-0.3.0-py2.py3-none-any.whl Collecting Django>=1.11 (from django-debug-toolbar) Downloading https://files.pythonhosted.org/packages/61/cb/e3c6bfccdf23c48dd4ce014b96178aa048b9450739eaa5f11d4d23d9d5d6/Django-1.11.23-py2.py3-none-any.whl (6.9MB)
100% |████████████████████████████████| 7.0MB 544kB/s Requirement already satisfied: pytz in /usr/local/lib/python2.7/site-packages (from Django>=1.11->django-debug-toolbar) (2015.2) Installing collected packages: sqlparse, Django, django-debug-toolbar Found existing installation: sqlparse 0.1.15
Uninstalling sqlparse-0.1.15:
Successfully uninstalled sqlparse-0.1.15 Found existing installation: Django 1.8.18
Uninstalling Django-1.8.18:
I have tried running the container and installing the django
Starting teamwave_backend_1 ... done
Attaching to teamwave_backend_1
backend_1 | Traceback (most recent call last):
backend_1 | File "manage.py", line 8, in <module>
backend_1 | from django.core.management import execute_from_command_line
backend_1 | ImportError: No module named django.core.management
teamwave_backend_1 exited with code 1
I later fixed my django by docker-compose build but I want django-debug-toolbar too.
This is my docker-compose.yml
version: '3'
services:
db:
image: postgres:10-alpine
environment:
- POSTGRES_PASSWORD=dsdbadmin
- POSTGRES_DB=tm_v1.1
volumes:
- pgdata:/var/lib/postgresql/data
- ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
backend:
build: .
volumes:
- .:/backend
ports:
- "8000:8000"
depends_on:
- db
- redis
redis:
image: redis:4-alpine
volumes:
pgdata:
And this is my Dockerfile
FROM python:2
ENV PYTHONUNBUFFERED 1
WORKDIR /backend
ADD req.txt /backend/
RUN pip install --upgrade pip
RUN pip install -r req.txt
ADD . /backend/
ENTRYPOINT ["python", "manage.py", "runserver", "0.0.0.0:8000"]
EXPOSE 8000
I use docker-compose up to run my containers
installing the latest version from django-debug-toolbar will install Django-1.11.23 and uninstall older Django versions .
you may try to find the correct django-debug-toolbar for Django-1.8

Setting up Django with docker

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.

Heroku Django Gunicorn 'Foreman Start' error

I'm working through the Heroku's Django tutorial and I got all the way down to 'Using a different WSIG server'.
When I try to use gunicorn I get the following error:
requirements.txt
Django==1.4.1
distribute==0.6.28
dj-database-url==0.2.1
psycopg2==2.4.5
gunicorn==0.14.6
Procfile
web: gunicorn djtut.wsgi -b 0.0.0.0:$PORT
(venv) C:\Users\xxxx\Documents\Python\djtut>foreman check
valid procfile detected (web)
(venv) C:\Users\xxxx\Documents\Python\djtut>foreman start
10:53:05 web.1 | started with pid 5652
10:53:06 web.1 | exited with code 1
10:53:06 web.1 | Traceback (most recent call last):
10:53:06 system | sending SIGKILL to all processes
10:53:06 | File "C:\Users\xxxx\Documents\Python\djtut\venv\Scripts\
gunicorn-script.py", line 9, in <module>
(venv) C:\Users\xxxxx\Documents\Python\djtut>
Works fine using the dev server on Heroku. I'm on Windows 7. Any ideas? I suspect it is an OS issue?
thanks,
AP
Unfortunately, Gunicorn doesn't work on Windows.