django and nginx is not working on docker - python

i am a beginner in the docker and i want to deploy my django project
using nginx and postgres on vps using docker so I create a dockerfile and docker-compose but it is not working it means that postgres is on the port but django and nginx is not working i don't have any idea can you help me
my dockerfile
FROM python:3.8-slim-buster
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update && apt-get install -y build-essential libpq-dev
RUN rm -rf /var/lib/apt/lists/*
COPY . .
RUN pip install --upgrade pip && pip install -r requirements.txt
my docker compose
version: '3.8'
services:
database:
container_name: database
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
volumes:
- postgres:/var/lib/postgresql/data
restart: always
app:
build:
context: .
container_name: django-app
command: >
sh -c "python3 manage.py migrate &&
gunicorn config.wsgi:application --bind 0.0.0.0:8000"
volumes:
- static:/usr/src/app/static
- media:/usr/src/app/media
depends_on:
- database
environment:
- DEBUG=False
- ALLOWD_HOST=*
- DATABASE-NAME=postgres
- DATABASE-USER=postgres
- DATABASE-PASSWORD=postgres
- DATABASE-HOST=database
- DATABASE-PORT=5432
nginx:
image: nginx
container_name: nginx
ports:
- "80:80"
volumes:
- ./nginx:/etc/nginx/conf.d
- static:/var/www/static
- media:/var/www/media
volumes:
postgres:
static:
media:

I presume you want to serve Django static files in Nginx (reverse proxy feature in that case).You lack binding Nginx with Gunicorn served port and static file catalogue here.
So you will need to configure nginx to do that in the conf file.
For example. file default.conf
upstream backend {
server app:8000;
}
server {
listen 80;
# Django admin (if implemented)
location /admin {
proxy_pass http://backend;
autoindex off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
# Django static assests.
location /static/ {
autoindex off;
alias /usr/src/app/staticfiles/; # if thats where you copied your app files in docker container
}
Also will need static files to be configured in settings.py
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
static files regards catalogue in project root.
You will also have to make some dirs in Dockerfile for Django before you will copy project - in order to avoid error in collectstatic.
$WORKDIR your_workdir_path_in_docker_cotainer
# Make static files dirs in order to avoid error from collectstatic.
RUN mkdir $WORKDIR/staticfiles && \
mkdir $WORKDIR/staticfiles/admin && \ # if you implemented - to keep css/js
mkdir $WORKDIR/staticfiles/rest_framework #if you using DRF - to keep css/js
Last thing is to add add collectstatic command in you Docker-compose section where Gunicorn is started like.
command: >
sh -c "python manage.py migrate --noinput &&
python manage.py collectstatic --no-input &&
gunicorn config.wsgi:application --bind 0.0.0.0:8000"
Maybe its worthy to create separate Dockerfile for Nginx
FROM nginx:1.21-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY your_path_for_file/default.conf /etc/nginx/conf.d
CMD ["nginx", "-g", "daemon off;"] # To start Nginx
You can start that aditional container in Docker-compose by specifying path to Dockerfile
dockerfile: your_path_to_nginx_dockerfile/Dockerfile.nginx

Related

nginx django static cannot fully serve my files(403 permission denied)

I've been dealing with nginx for about 1 week. I have 4 services that I set up with docker, django postgresql fastapi and nginx, but nginx does not serve django's static files. I'm facing 403 error. I tried the solutions of all similar error fields, but it doesn't work (file permission granting file path control) below I am sharing the files I use please help.
docker-compose.yml:
django_gunicorn:
build: .
command: gunicorn sunucu.wsgi:application --bind 0.0.0.0:7800 --workers 3
volumes:
- ./static:/root/Kodlar/kodlar/static
env_file:
- .env
environment:
- DATABASE_URL="**"
ports:
- "7800"
depends_on:
- db
nginx:
build: ./nginx
volumes:
- ./static:/root/Kodlar/kodlar/static
ports:
- "80:80"
depends_on:
- django_gunicorn
volumes:
static_files:
Django Docker File:
FROM python:3.8-slim-buster
WORKDIR /app
ENV PYTHONUNBUFFERED=1
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
RUN python manage.py migrate --no-input
RUN python manage.py collectstatic --no-input
CMD ["gunicorn", "server.wsgi:application", "--bind", "0.0.0.0:7800","--workers","3"]
Django Settings py:
STATIC_URL = '/static/'
STATIC_ROOT = '/root/Kodlar/kodlar/static/'
DEBUG = False
Nginx Docker File:
FROM nginx:1.19.0-alpine
COPY ./default.conf /etc/nginx/conf.d/default.conf
Nginx Conf File:
upstream django {
server django_gunicorn:7800;
}
server {
listen 80;
server_name mydomain.com;
error_page 404 /404.html;
location = /404.html {
root /root/Kodlar/kodlar/templates;
internal;
}
if ($host != 'mydomain.com') {
return 404;
}
location / {
proxy_pass http://django;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static/ {
alias /root/Kodlar/kodlar/static/;
}
}
I want my django service to run actively with gunicorn and serve static files in nginx

Can't serve Django static files with NGINX

I'm trying to deploy a Django and React website using gunicorn, nginx and docker.
I can't get nginx to read my static files for example the django admin panel. I already ran python manage.py collecstatic and the files are in recommendations-be/backend/static
Here is the docker-compose.yml file:
version: '3'
services:
backend:
build:
context: ./recommendations-be
command: gunicorn backend.wsgi:application --bind 0.0.0.0:8000 --timeout 0
ports:
- "8000:8000"
volumes:
- static:/django/backend/static
frontend:
build:
context: ./recommendations-fe
volumes:
- react_build:/react/build
nginx:
image: nginx:latest
ports:
- "80:8080"
volumes:
- ./nginx/nginx-setup.conf:/etc/nginx/conf.d/default.conf:ro
- react_build:/var/www/react
- static:/django/backend/static
depends_on:
- backend
- frontend
volumes:
static:
react_build:
Here is my nginx conf file:
upstream api {
server backend:8000;
}
server {
listen 8080;
location / {
root /var/www/react;
}
location /api/ {
proxy_pass http://api;
proxy_set_header Host $http_host;
}
location /static/ {
alias /django/backend/static;
}
}
Here's the Dockerfile in backend directory recommendations-be:
FROM python:3.10.8-slim-buster
ENV PYTHONUNBUFFERED 1
WORKDIR /django
COPY requirements.txt requirements.txt
RUN pip install --upgrade pip --no-cache-dir
RUN pip install -r requirements.txt --no-cache-dir
COPY . .
And the django settings.py:
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "backend", "static")
Here is the file structure in my project: file structure
In your nginx configuration, please make the following change:
location /static/ {
alias /static/
}
Then, in your docker-compose.yml file, in the volume definitions of Nginx, please replace the line
- static:/django/backend/static
with the following:
- static:/static
Nginx should now be able to serve your static files.

docker nginx django gunicorn not serving static files in production

Trying to deploy website with nginx + gunicorn + docker + django. But ngingx isn't serving static files. Following are the configurations:
Django project structure
settings file production.py
STATIC_URL = "/static/"
"""STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)"""
STATIC_ROOT = "/app/forex/static/admin/"
Docker file for nginx
FROM nginx:1.19.0
COPY ./default.conf /etc/nginx/conf.d/default.conf
nginx configurations
upstream django {
server website:8000;
}
server {
listen 80;
client_max_body_size 100M;
proxy_set_header X-Forwarded-Proto $scheme;
location / {
proxy_pass http://django;
}
location /media/ {
alias /app/media/;
}
location /static/ {
alias /app/forex/static/admin/;
}
}
Gunicorn docker file
FROM python:3
ADD requirements.txt /app/requirements.txt
ADD . /app
WORKDIR /app
EXPOSE 8000:8000
RUN pip install --upgrade pip && pip install -r /app/requirements.txt
RUN python manage.py collectstatic --no-input --settings=forex.settings.production
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "forex.wsgi:application", "DJANGO_SETTINGS_MODULE=forex.settings.production"]
docker-compose.yml
services:
website:
build:
context: .
dockerfile: Dockerfile.app
env_file:
- env
container_name: website_container_8
nginx:
build: ./nginx
volumes:
- static:/app/forex/static/admin/
ports:
- "80:80"
depends_on:
- website
volumes:
static:
FROM nginx container, it isn't copying static files.
What do I need to change to make it working?
Your files are located at your website container, you need to share it, with the nginx container:
services:
website:
build:
context: .
dockerfile: Dockerfile.app
env_file:
- env
container_name: website_container_8
volumes:
- static:/app/forex/static/admin/ #<-- you want to share this
nginx:
build: ./nginx
volumes:
- static:/app/forex/static/admin/ #<-- with this folder
ports:
- "80:80"
depends_on:
- website
volumes:
static: #<-- you can do it through this

Static files not served when using docker + django + nginx

I am trying to dockerize a django project and use nginx to serve static files. Everything seems to be working except the static files are not served. When I go to the admin page, always get Not Found: /django_static/rest_framework/css/prettify.css for files in django_static folder.
folder structure:
backend
server
apps
django_static
media
...
docker
backend
Dockerfile
wsgi-entrypoint.sh
nginx
production
default.conf
development
default.conf
Dockerfile
Here is my docker-compose:
services:
nginx:
build: ./docker/nginx
ports:
- 1234:80
volumes:
- ./docker/nginx/production:/etc/nginx/conf.d
- django_static:/app/backend/server/django_static
- media:/app/backend/server/media
depends_on:
- backend
restart: "on-failure"
backend:
build:
context: .
dockerfile: ./docker/backend/Dockerfile
entrypoint: /app/docker/backend/wsgi-entrypoint.sh
ports:
- "8001:8001"
volumes:
- django_static:/app/backend/server/django_static
- media:/app/backend/server/media
expose:
- 8001
volumes:
django_static:
media:
dockerfile for django:
FROM python:3.7-slim
WORKDIR /app
ADD ./backend/requirements.txt /app/backend/
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
RUN pip install gunicorn
RUN pip install -r backend/requirements.txt
ADD ./backend /app/backend
ADD ./docker /app/docker
wsgi-entrypoint.py
#!/bin/sh
until cd /app/backend/server
do
echo "Waiting for server volume..."
done
until ./manage.py migrate
do
echo "Waiting for db to be ready..."
sleep 2
done
./manage.py collectstatic --noinput
gunicorn server.wsgi --bind 0.0.0.0:8001 --workers 1 --threads 1
#./manage.py runserver 0.0.0.0:8003
nginx docker file:
FROM nginx:1.19.0-alpine
RUN rm /etc/nginx/conf.d/default.conf
CMD ["nginx", "-g", "daemon off;"]
nginx config file:
upstream glf {
server 0.0.0.0:8001;
}
server {
listen 80;
location / {
proxy_pass http://0.0.0.0:8001;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /django_static/ {
alias /app/backend/server/django_static/;
}
location /media/ {
alias /app/backend/server/media/;
}
}
settings.py
MEDIA_URL = '/media/'
STATIC_URL = '/django_static/'
STATIC_ROOT = BASE_DIR / 'django_static'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Any hint is appreciated!

serving flask via nginx and gunicorn in docker

Playing around with flask I would like to get a real setup up and running in docker. This means flask should be served via nginx and gunicorn. I set up a sample code repository https://github.com/geoHeil/pythonServing but so far can't get nginx to work properly.
Flask is served on application:5000, docker should resolve application to its respective name.
Nginx config is as follows:
server {
listen 8080;
server_name application;
charset utf-8;
location / {
proxy_pass http://application:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
which looks good to me. So far I cant find the problem.
edit
compose file is here. Command to start was
docker-compose build
docker-compose up
version: '2'
services:
application:
restart: always
build: ./application
command: gunicorn -w 4 --bind :5000 wsgi:application
links:
- db
expose:
- "5000"
ports:
- "5000:5000"
nginx:
restart: always
build: ./nginx
links:
- application
expose:
- 8080
ports:
- "8880:8080"
Your nginx config file is in a wrong location.
Steps to fix:
sudo docker-compose down
Delete nginx image:
sudo docker images
sudo docker rmi
REPOSITORY TAG IMAGE ID CREATED SIZE
pythonserving_nginx latest 152698f13c7a About a minute ago 54.3 MB
sudo docker rmi pythonserving_nginx
Now change the nginx Dockerfile:
FROM nginx:1.11.8-alpine
MAINTAINER geoheil
ADD sites-enabled.conf /etc/nginx/conf.d/sites-enabled.conf
Please note the location of nginx config.
Now try this docker-compose file (Using User-defined Networks):
version: '2'
services:
application:
restart: always
build: ./application
command: gunicorn -w 4 --bind :5000 wsgi:application
networks:
- testnetwork
expose:
- "5000"
ports:
- "5000:5000"
db:
restart: always
image: postgres:9.6.1-alpine
networks:
- testnetwork
ports:
- "5432:5432"
environment:
- POSTGRES_USER=d
- POSTGRES_PASSWORD=d
- POSTGRES_DB=d
volumes:
- ./postgres:/var/lib/postgresql
nginx:
restart: always
build: ./nginx
networks:
- testnetwork
expose:
- 8080
ports:
- "8880:8080"
networks:
testnetwork:
And Bring up containers:
sudo docker-compose up
Browse to http://localhost:8880
Smaple docker file
FROM python:3.5
RUN apt-get update
RUN apt-get install -y --no-install-recommends \
libatlas-base-dev gfortran nginx supervisor
RUN pip3 install uwsgi
COPY ./requirements.txt /project/requirements.txt
RUN pip3 install -r /project/requirements.txt
RUN useradd --no-create-home nginx
RUN rm /etc/nginx/sites-enabled/default
RUN rm -r /root/.cache
COPY nginx.conf /etc/nginx/
COPY flask-site-nginx.conf /etc/nginx/conf.d/
COPY uwsgi.ini /etc/uwsgi/
COPY supervisord.conf /etc/
COPY /app /project
WORKDIR /project
CMD ["/usr/bin/supervisord"]

Categories

Resources