Reputation: 9123
I am creating a dockercompose file. I want to set up a project with this file. This dockercompose will generate 2 docker containers: One for mysql and the other for apache2. What i want to do is to automated everything and i want to generate an unique mysql password. Is it possible to do that ?
Thanks
Upvotes: 5
Views: 20199
Reputation: 1853
You can set a random password for mysql via the environment variable MYSQL_RANDOM_ROOT_PASSWORD
.
This is an optional variable. Set to yes to generate a random initial password for the root user (using pwgen). The generated root password will be printed to stdout (GENERATED ROOT PASSWORD: .....).
https://github.com/mysql/mysql-docker#mysql_random_root_password
Docker Compose Environment Variables:
https://docs.docker.com/compose/environment-variables/
This is a sample alternative strategy that would allow you to use a random password for both MySQL and Django (or any other image).
You will need to install pwgen
on your host system with brew, apt, or yum, and then generate a password setting it to an environment variable. You can rerun this anytime to change the password.
export DB_PASSWORD=`pwgen -Bs1 12`
Simplified docker-compose.yml
:
version: '2'
services:
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: db_name
MYSQL_USER: user
MYSQL_PASSWORD: user_password
web:
image: django
volumes:
- ./django_admin.py:/app
expose:
- "8000"
links:
- db
environment:
DB_PASSWORD: ${DB_PASSWORD}
In your django_admin.py
, you can now use the DB_PASSWORD
env variable:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'root',
'PASSWORD': os.environ.get('DB_PASSWORD', ''),
'HOST': 'host'
}
}
Upvotes: 9