diff --git a/README.md b/README.md index b9998e87e8c54de0c3e32fe2f0dd3008279f9931..f5be0b002355907c97d8064a2e41479d94dc1917 100644 --- a/README.md +++ b/README.md @@ -8,19 +8,15 @@ ## Environment -### Backend environment - File `.env`: ``` SECRET_KEY=foobarbarfoo ALLOWED_HOSTS=* DEBUG=1 -SQL_DATABASE=sizakat -SQL_USER=postgres -SQL_PASSWORD=postgres -SQL_HOST=db -SQL_PORT=5432 +POSTGRES_DB=sizakat +POSTGRES_USER=postgres +POSTGRES_PASSWORD=postgres BACKEND_PORT=8000 ``` @@ -36,22 +32,12 @@ BACKEND_PORT=8000 https://docs.djangoproject.com/en/3.0/ref/settings/#debug -- `SQL_*` +- `POSTGRES_*` Pengaturan untuk database + https://github.com/docker-library/docs/tree/master/postgres#environment-variables + - `BACKEND_PORT` Port untuk service backend. Digunakan dalam `docker-compose` - -### Database environment - -File `.db.env` - -``` -POSTGRES_DB=sizakat -POSTGRES_USER=postgres -POSTGRES_PASSWORD=postgres -``` - -https://github.com/docker-library/docs/tree/master/postgres#environment-variables diff --git a/docker-compose.yml b/docker-compose.yml index baffad9a59d890b955f5ad70001914e0a5fbed06..c1527a1ba37f8615efacb1248dc67ed61ebfa98a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,24 +1,41 @@ version: '3.7' services: + nginx: + image: nginx:stable + restart: always + ports: + - ${BACKEND_PORT:-8000}:80 + volumes: + - ./nginx/default.conf:/etc/nginx/conf.d/default.conf + - staticfiles:/var/www/html/static/ + depends_on: + - backend + backend: build: context: ./ + restart: always command: gunicorn sizakat.wsgi --bind 0.0.0.0:8000 --log-file - volumes: - ./:/app/backend/ - ports: - - ${BACKEND_PORT:-8000}:8000 + - staticfiles:/app/backend/staticfiles/ env_file: - .env + environment: + POSTGRES_HOST: db + POSTGRES_PORT: 5432 depends_on: - db + db: image: postgres:12.0-alpine + restart: always volumes: - postgres_data:/var/lib/postgresql/data/ env_file: - - .db.env + - .env volumes: postgres_data: + staticfiles: diff --git a/entrypoint.sh b/entrypoint.sh index 74186565ad7e8552075b966657b8d0a1bc46285f..84f6d6e30057bcbe6d3f08eb19a201b6257c5354 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,16 +1,17 @@ #!/bin/sh -if [ "$DATABASE" = "postgres" ] +if [ -n "$POSTGRES_DB" ] then echo "Waiting for postgres..." - while ! nc -z $SQL_HOST $SQL_PORT; do + while ! nc -z $POSTGRES_HOST $POSTGRES_PORT; do sleep 0.1 done echo "PostgreSQL started" fi +python manage.py collectstatic --no-input python manage.py migrate exec "$@" diff --git a/nginx/default.conf b/nginx/default.conf new file mode 100644 index 0000000000000000000000000000000000000000..4b5e6f7824de8d6b87d48144b37cd23fee6fb690 --- /dev/null +++ b/nginx/default.conf @@ -0,0 +1,15 @@ +server { + listen 80; + + location /static/ { + root /var/www/html/; + } + + location / { + proxy_pass http://backend:8000; + proxy_set_header Host $http_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; + } +} diff --git a/sizakat/settings.py b/sizakat/settings.py index 73d73c05e57c5902d3b055ef67b5b30ce3ecaa9c..a3e25ad37cf703ea6f99f07e6d4ff0f3cbddde2d 100644 --- a/sizakat/settings.py +++ b/sizakat/settings.py @@ -91,15 +91,15 @@ DATABASES = { } } -if 'SQL_DATABASE' in os.environ: +if 'POSTGRES_DB' in os.environ: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', - 'NAME': os.environ.get('SQL_DATABASE'), - 'USER': os.environ.get('SQL_USER'), - 'PASSWORD': os.environ.get('SQL_PASSWORD'), - 'HOST': os.environ.get('SQL_HOST'), - 'PORT': os.environ.get('SQL_PORT'), + 'NAME': os.environ.get('POSTGRES_DB'), + 'USER': os.environ.get('POSTGRES_USER'), + 'PASSWORD': os.environ.get('POSTGRES_PASSWORD'), + 'HOST': os.environ.get('POSTGRES_HOST'), + 'PORT': os.environ.get('POSTGRES_PORT'), } }