Notes

Passwords, secrets and environment variables inside Docker

Edit on GitHub

Docker

You can define environment variables inside docker-compose.yml file to be used in wp-config.php. You define them under the environment block and use them with getenv("")

 1wordpress:
 2  image: wp-img
 3  restart: always
 4  environment:
 5    WORDPRESS_DB_HOST: mysql
 6    WORDPRESS_DB_NAME: myawesomedb
 7    WORDPRESS_DB_USER: myawesomeuser
 8    WORDPRESS_DB_PASSWORD: oBw^^(&&ASJHGasb
 9    DOMAIN_CURRENT_SITE: my.cooldomain.com
10  volumes:
11    - /home/blah/wordpress:/var/www/html
12  depends_on:
13    - mysql
 1// wp-config.php
 2
 3/** The name of the database for WordPress */
 4define('DB_NAME', getenv("WORDPRESS_DB_NAME"));
 5
 6/** MySQL database username */
 7define('DB_USER', getenv("WORDPRESS_DB_USER"));
 8
 9/** MySQL database password */
10define('DB_PASSWORD', getenv("WORDPRESS_DB_PASSWORD"));
11
12/** MySQL hostname */
13define('DB_HOST', getenv("WORDPRESS_DB_HOST"));

You can also use a .env file and reference values from that inside your docker-compose.yml

 1wordpress:
 2  image: wordpress:5-fpm-alpine
 3  depends_on:
 4    - db
 5  container_name: wordpress
 6  restart: unless-stopped
 7  # make sure to add path to env file
 8  env_file: .env
 9  # we refence .env variables like `$MYSQL_USER`
10  environment:
11    - WORDPRESS_DB_HOST=db:3306
12    - WORDPRESS_DB_USER=$MYSQL_USER
13    - WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD
14    - WORDPRESS_DB_NAME=wordpress