Skip to content

Commit

Permalink
Serve django's static files in production
Browse files Browse the repository at this point in the history
  • Loading branch information
norm committed Aug 2, 2021
1 parent bb5e0c8 commit bb7958a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion docker-compose.yml
Expand Up @@ -8,14 +8,16 @@ services:
- environ
depends_on:
- db
volumes:
- static:/static

nginx:
image: nginx:1.21.1
depends_on:
- app
volumes:
- baked:/baked
- ./nginx:/etc/nginx/templates
- static:/static

db:
image: postgres:13.3-alpine
Expand All @@ -29,3 +31,4 @@ services:
volumes:
postgres_data:
baked:
static:
2 changes: 1 addition & 1 deletion hasworn/settings.py
Expand Up @@ -155,8 +155,8 @@

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = '/static/static'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
Expand Down
4 changes: 4 additions & 0 deletions nginx/app.conf.template
Expand Up @@ -5,6 +5,10 @@ server {
resolver 127.0.0.11 valid=5s;
set $upstream app;

location /static/ {
root /static;
}

location / {
proxy_pass http://$upstream:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Expand Down
6 changes: 6 additions & 0 deletions restart.sh
Expand Up @@ -8,6 +8,7 @@ IP_ADDRESS='{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
function main {
fetch_docker_image
migrate_database
collect_static_files
start_new_image
remove_old_image
}
Expand All @@ -28,6 +29,11 @@ function migrate_database {
python manage.py migrate --noinput
}

function collect_static_files {
run_compose run --rm -T app \
python manage.py collectstatic --noinput
}

function start_new_image {
scale_app 2

Expand Down

0 comments on commit bb7958a

Please sign in to comment.