Skip to content

Commit

Permalink
Deployment setup: Update Django settings to use environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-myskov committed Nov 14, 2023
1 parent 4216444 commit 7de7565
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ services:
# command to run when the container starts
command: >
sh -c 'python manage.py runserver 0.0.0.0:8000'
environment:
- DEBUG=1
```
- Build the docker image
```bash
Expand Down Expand Up @@ -342,7 +344,6 @@ DATABASES = {
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASS'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT'),
}
}
```
Expand Down Expand Up @@ -709,7 +710,6 @@ services:
- DB_NAME=${DB_NAME}
- DB_USER=${DB_USER}
- DB_PASS=${DB_PASS}
- DB_PORT=${DB_PORT}
- SECRET_KEY=${DJANGO_SECRET_KEY}
- ALLOWED_HOSTS=${DJANGO_ALLOWED_HOSTS}
depends_on:
Expand Down Expand Up @@ -738,3 +738,16 @@ volumes:
postgres-data:
static-data:
```

#### Update Django settings to use environment variables
```
SECRET_KEY = os.environ.get('SECRET_KEY', 'changeme')
DEBUG = bool(int(os.environ.get('DEBUG', 0)))
ALLOWED_HOSTS = []
ALLOWED_HOSTS.extend(
filter(
None,
os.environ.get('ALLOWED_HOSTS', '').split(','),
)
)
```
11 changes: 8 additions & 3 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-m0mui7moly5t#jbn74mxr35o&c%ijo5s3xd7n5d$m^mun^kv$r'
SECRET_KEY = os.environ.get('SECRET_KEY', 'changeme')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = bool(int(os.environ.get('DEBUG', 0)))

ALLOWED_HOSTS = []
ALLOWED_HOSTS.extend(
filter(
None,
os.environ.get('ALLOWED_HOSTS', '').split(','),
)
)


# Application definition
Expand Down Expand Up @@ -89,7 +95,6 @@
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASS'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT'),
}
}

Expand Down
4 changes: 4 additions & 0 deletions commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ $ docker volume ls
```bash
$ docker volume rm <volume_name>
```
- Down containers of specific docker-compose.yml file
```bash
$ docker compose -f <docker-compose-deploy.yml> down
```

## Run commands inside the container
- Using `docker compose run` command
Expand Down
3 changes: 1 addition & 2 deletions docker-compose-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ services:
- DB_NAME=${DB_NAME}
- DB_USER=${DB_USER}
- DB_PASS=${DB_PASS}
- DB_PORT=${DB_PORT}
- SECRET_KEY=${DJANGO_SECRET_KEY}
- ALLOWED_HOSTS=${DJANGO_ALLOWED_HOSTS}
depends_on:
Expand All @@ -33,7 +32,7 @@ services:
depends_on:
- app
ports:
- 80:8000
- 8000:8000
volumes:
- static-data:/vol/static

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
- DB_NAME=devdb
- DB_USER=devuser
- DB_PASS=postgres
- DEBUG=1

db:
image: postgres:13-alpine
Expand Down

0 comments on commit 7de7565

Please sign in to comment.