1. The problem I’m having:
I am able to serve staticfiles on local host but I cannot serve media files.
2. Error messages and/or full log output:
Please DO NOT REDACT any information except credentials.
compose exec web ls -l /app/media/rooms/
total 27616
-rwxr-xr-x 1 root root 239936 Feb 11 23:07 3-KPW-DOUBLE-KILIVIEW-2.jpg
-rwxr-xr-x 1 root root 402145 Jan 22 21:50 DSC_8615_1.jpg
-rwxr-xr-x 1 root root 13814798 Jan 31 16:01 DSC_8615_3Een2SF.JPG
-rwxr-xr-x 1 root root 13814798 Jan 31 16:07 DSC_8615_Vnteo7Z.JPG
3. Caddy version:
alpine
→
4. How I installed and ran Caddy:
docker-compose up -d
[+] Running 4/4
Network shimba_default Created 0.1s
Container django-postgres Started 1.6s
Container django-app Started 1.8s
Container django-caddy-app Started
a. System environment:
b. Command:
PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.
c. Service/unit/compose file:
services:
db:
image: postgres:latest
container_name: django-postgres
restart: always
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
env_file:
- .env
ports:
- '5432:5432'
volumes:
- pg_data:/var/lib/postgresql/data
web:
build: .
container_name: django-app
restart: always
command: gunicorn --bind 0.0.0.0:8000 website.wsgi:application
ports:
- "8000:8000"
environment:
- DJANGO_DEBUG=${DEBUG}
- DJANGO_SECRET_KEY=${SECRET_KEY}
- DJANGO_ALLOWED_HOSTS=${DJANGO_ALLOWED_HOSTS}
- DJANGO_CSRF_TRUSTED_ORIGINS=${DJANGO_CSRF_TRUSTED_ORIGINS}
- DATABASE_ENGINE=${DB_ENGINE}
- DATABASE_NAME=${DB_NAME}
- DATABASE_USER=${DB_USER}
- DATABASE_PASSWORD=${DB_PASSWORD}
- DATABASE_HOST=${DB_HOST}
- DATABASE_PORT=${DB_PORT}
env_file:
- .env
depends_on:
- db
volumes:
- ./staticfiles:/app/staticfiles # Ensure collectstatic stores files here
- ./media:/app/media
caddy:
image: caddy:alpine
restart: always
container_name: django-caddy-app
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- ./static:/srv/static # Static files volume
- ./media:/srv/media # Media files volume
- caddy_data:/data # SSL certificates storage
- caddy_config:/config # Caddy configuration
depends_on:
- web
volumes:
pg_data:
caddy_data:
caddy_config:
networks:
app_network:
d. My complete Caddy config:
shimbagreenlodge.net{
# Enable compression
encode gzip zstd
# Serve static and media files correctly
root * /srv
file_server
# Reverse proxy all other requests to Django
@notStatic {
not path /static/* /media/*
}
reverse_proxy @notStatic web:8000
}