I've a docker image with django 3.1 and postgresql.
In the docker-compose.yml I wrote:
version: '3'
services:
app:
build:
context: .
ports:
- "8001:8001"
volumes:
- ./app:/app
command: >
sh -c "python manage.py runserver 0.0.0.0:8001"
environment:
- DB_HOST=db
- DB_NAME=app
- DB_USER=postgres
- DB_PASS=password
depends_on:
- db
db:
image: postgres:10-alpine
environment:
- POSTGRES_DB=app
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
In the Django app's settings.py I read the database password from the .txt file excluded from the .git
...
DB_PASSWORD = ''
with open('database_password.txt') as f:
DB_PASSWORD = f.read().strip()
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'HOST': os.environ.get('DB_HOST'),
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': DB_PASSWORD,
}
}
...
What is the best practice to make Django and Docker securely read the same password from the same place?
The most suitable information about this I found here: https://medium.com/swlh/setting-up-a-secure-django-project-repository-with-docker-and-django-environ-4af72ce037f0
The author offers to use the django-environ package for django. The docker part in this article descibed like this:
If you specifed a different user and password in the DATABASE_URL variable in the .env file above, you should include them here (although this will compromise the security of the database, as the docker-compose.yml file will be committed to the repository). When it comes time to deploy the project, create a separate docker-compose file that will not be committed.
And it's not very clear to me.