Setting up Django with docker - django

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.

Related

Why is CSS not working even I use 'runserver' command in Docker?

I am trying to develop a web application using Django in Docker.
I made a DockerFile and a docker-compose.yml file as below to check the code in the development environment.
Even though I'm using the runserver command in the file, when I start the container and access the localhost, the CSS for the admin page isn't working.
How should I change the codes to make it work?
Here are the codes:
Dockerfile
FROM python:3.8.3-alpine
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY . .
docker-compose.yml
version: '3.7'
services:
web:
build: ./django_project
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./django_project/:/usr/src/app/
ports:
- 8000:8000
docker desktop: 4.1.1
Python: 3.8
Django: 3.0

Docker-compose executes django twice

I am running in windows 10, and trying to set up a project via docker-compose and django.
If you are interested, It will take you 3 minutes to follow this tutorial and you will get the same error as me. docs.docker.com/samples/django –
When I run
docker-compose run app django-admin startproject app_settings .
I get the following error
CommandError: /app /manage.py already exists. Overlaying a project into an existing directory won't replace conflicting files.
Or when I do this
docker-compose run app python manage.py startapp core
I get the following error
CommandError: 'core' conflicts with the name of an existing Python module and cannot be used as an
app name. Please try another name.
Seems like the command is maybe executed twice? Not sure why?
Docker file
FROM python:3.9-slim
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update && apt-get install
RUN apt-get install -y \
libpq-dev \
gcc \
&& apt-get clean
COPY ./requirements.txt .
RUN pip install -r requirements.txt
RUN mkdir /app
WORKDIR /app
COPY ./app /app
Docker-compose
version: "3.9"
compute:
container_name: compute
build: ./backend
# command: python manage.py runserver 0.0.0.0:8000
# volumes:
# - ./backend/app:/app
ports:
- "8000:8000"
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
depends_on:
- db
Try running your image without any arguments, you are already using the command keyword in your docker-compose or just remove that line from the file.

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.

What is the docker command to run my Django server?

I'm trying to Dockerize my local Django/MySql setup. I have this directory and file structure ...
apache
docker-compose.yml
web
- manage.py
- venv
- requirements.txt
- ...
Below is the docker-compose.yml file I'm using ...
version: '3'
services:
web:
restart: always
build: ./web
expose:
- "8000"
links:
- mysql:mysql
volumes:
- web-django:/usr/src/app
- web-static:/usr/src/app/static
#env_file: web/venv
environment:
DEBUG: 'true'
command: [ "python", "./web/manage.py runserver 0.0.0.0:8000" ]
mysql:
restart: always
image: mysql:5.7
environment:
MYSQL_DATABASE: 'maps_data'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'chicommons'
# You can use whatever password you like
MYSQL_PASSWORD: 'password'
# Password for root access
MYSQL_ROOT_PASSWORD: 'password'
ports:
- "3406:3406"
expose:
# Opens port 3406 on the container
- '3406'
volumes:
- my-db:/var/lib/mysql
volumes:
web-django:
web-static:
my-db:
However when I run
docker-compose up
I get errors like the below
maps_web_1 exited with code 2
web_1 | python: can't open file './web/manage.py runserver 0.0.0.0:8000': [Errno 2] No such file or directory
maps_web_1 exited with code 2
maps_web_1 exited with code 2
web_1 | python: can't open file './web/manage.py runserver 0.0.0.0:8000': [Errno 2] No such file or directory
maps_web_1 exited with code 2
Is there another way I'm supposed to be referencing the manage.py file?
Edit: Added info requested in comments ...
FROM python:3.7-slim
RUN apt-get update && apt-get install
RUN apt-get install -y libmariadb-dev-compat libmariadb-dev
RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc \
&& rm -rf /var/lib/apt/lists/*
RUN python -m pip install --upgrade pip
COPY requirements.txt requirements.txt
RUN python -m pip install -r requirements.txt
COPY . .
As others suggested, this is most probably because of running the manage.py runserver from a wrong directory or something very similar to this.
You are not using WORKDIR directive in your Dockerfile, at all. It is much safer if you do use them. Change your Dockerfile and docker-compose.yml files as below, and you problem should be solved.
Dockerfile
FROM python:3.7-slim
RUN apt-get update && apt-get install
RUN apt-get install -y libmariadb-dev-compat libmariadb-dev
RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc \
&& rm -rf /var/lib/apt/lists/*
RUN python -m pip install --upgrade pip
RUN mkdir -p /app/
WORKDIR /app/
COPY requirements.txt requirements.txt
RUN python -m pip install -r requirements.txt
COPY . /app/
docker-compose.yml
version: '3'
services:
web:
restart: always
build: ./web
expose:
- "8000"
links:
- mysql:mysql
volumes:
- web-django:/usr/src/app
- web-static:/usr/src/app/static
#env_file: web/venv
environment:
DEBUG: 'true'
command: [ "python", "manage.py", "runserver", "0.0.0.0:8000" ]
...
Notice
You should be able to fix the problem by simply deleting web from your command for running the server. That's because when you are building the Dockerfile, you are inside the web directory. So when you do COPY . . you are copying contents inside web directory, and not the web directory itself. Actually, your file structure inside the docker image, should look something similar to this:
- root
- home
- var
- ...
- manage.py
- venv
- requirements.txt
- ...
In the command: directive, if you're using the array syntax, you're responsible for breaking up the command into words. As you've shown it you're running the equivalent of python "manage.py runserver 0.0.0.0:8000" at the shell prompt, and it's dutifully considering the entire command and options as the filename of a script to be run, including spaces. If you break this up into single words it will work better
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
But there's not really a reason to specify this in docker-compose.yml at all. This is the default command you'd want to run to launch the container no matter how you ran it, so it should be the default command in your image's Dockerfile
...
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
You don't need links: at all on modern Docker (Docker Compose automatically sets up inter-container networking for you). You definitely don't want to mount named volumes over your application code: this hides what's in your image, and (since you've told Docker this is critical user data) it forces Docker to use an old version of your application if you try to update your image.
That leaves you with a simpler docker-compose.yml file:
version: '3'
services:
web:
restart: always
build: ./web
ports: # to access the container from outside
- "8000:8000"
environment:
DEBUG: 'true'
mysql:
restart: always
image: mysql:5.7
environment:
MYSQL_DATABASE: 'maps_data'
MYSQL_USER: 'chicommons'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- "3406:3306" # second port is always container-internal port
volumes:
- my-db:/var/lib/mysql
volumes:
my-db:
Let us try to debug this error:
maps_web_1 exited with code 2
web_1 | python: can't open file './web/manage.py runserver 0.0.0.0:8000': [Errno 2] No such file or directory
Looks like either code is not not copied to container (named 'web') or command is triggered from root/home directory, where manage.py is not accessible.
1. Is the code available on container? How to check?
Usually, docker will just commands in container execute and exit unless there is unfinished running task (like server running in background).
To stop exiting and enable debugging it, let us add a running command, so that you can login to container and see if code is present.
command: tail -f /dev/null #trick to keep the docker alive for debug mode.
docker-compose.yml
web:
restart: always
build: ./web
expose:
- "8000"
links:
- mysql:mysql
volumes:
- web-django:/usr/src/app
- web-static:/usr/src/app/static
#env_file: web/venv
environment:
DEBUG: 'true'
command: tail -f /dev/null #trick to keep the docker alive for debug mode.
Login to container 'web', from command line run docker exec -it web bash
Check if project files are present, now you can run python manage.py runserver 8000 command manually. If it works, then we can be sure of that the server can be run on container. Now, we can analyse initial working directory.
If code is present, check why manage.py is not found? Is the working directory set? meaning, does the container know what is the base directory to run command?
Specify which is the working directory, in Dockerfile, before you copy the project files in to container.
Dockerfile in web directory
ENV PYTHONUNBUFFERED 1
ARG PROJ_DIR=/usr/project/web
RUN mkdir -p $PROJ_DIR
WORKDIR $PROJ_DIR
COPY . $WORKDIR
docker-compose.yml
restart: always
build: ./web
expose:
- "8000"
links:
- mysql:mysql
volumes:
- web-django:/usr/src/app
- web-static:/usr/src/app/static
#env_file: web/venv
environment:
DEBUG: 'true'
command: python manage.py runserver 0.0.0.0:8000 #note this command is triggered from $WORKDIR that we set in Dockerfile.
I think this should resolve the issue or help you to figure out the problem.

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