Not able to deploy my Django application on Google Cloud Run
i need some help with my django docker deployment, so ill show you what I currently have and I need your help now configuring it to run with nginx and gunicorn so I can eventually use this to deploy my application with google cloud run. I'm using a mac btw
okay so here is my directory:
Dockerfile carz docker-compose.yml nginx static
carszambia db.sqlite3 manage.py requirements.txt staticfiles
docker-compose.yml:
version: '3.8'
services:
web:
build:
context: .
command: gunicorn carszambia.wsgi:application --bind 0.0.0.0:8000
volumes:
- .:/usr/src/app
- static_volume:/usr/src/app/staticfiles
expose:
- "8000"
env_file:
- ./.env.dev
depends_on:
- db
nginx:
image: nginx:latest
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- static_volume:/usr/src/app/staticfiles
ports:
- "8001:80"
depends_on:
- web
db:
image: postgres:15
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=xxx_hidden
- POSTGRES_PASSWORD=xxx_hidden
- POSTGRES_DB=xxx_hidden
volumes:
postgres_data:
static_volume:
Dockerfile:
# pull official base image
FROM python:3.11.4-slim-buster
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
RUN apt-get update && apt-get install -y \
nginx \
&& rm -rf /var/lib/apt/lists/*
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
# configure nginx
COPY ./nginx/default.conf /etc/nginx/conf.d/
# Expose port for Nginx
EXPOSE 80
# Start Gunicorn and Nginx
CMD ["/bin/bash", "-c", "gunicorn carszambia.wsgi:application --bind 0.0.0.0:8000 & nginx -g 'daemon off;'"]
nginx/default.conf:
server {
listen 80;
location /static/ {
alias /usr/src/app/staticfiles/;
}
location / {
proxy_pass http://web:8000; # Use service name 'web' instead of 127.0.0.1
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
nginx/Dockerfile:
FROM nginx:latest
COPY .defult.conf /etc/nginx/conf.d/defult.conf
DEPLOYMENT ERROR: (gcloud.run.deploy) Revision 'carszambia-00010-9zf' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. Logs for this revision might contain more information.
Cloud Run uses Docker containers, but it does not support Docker Compose. If you're relying on that docker compose yaml to wire up your containers, it's not going to work. If you want to use a yaml file to store your service configuration, Cloud Run uses a Knative service as its native Yaml format.
Cloud Run also doesn't support disks, so unless you want to have a very slow database serving off of a GCS storage bucket or a very expensive database serving off of a $400/month NFS share, you probably should run that in Cloud SQL and use the built in connector to connect the database to your Cloud Run Service.