Chris Mitchell Online

Chris blogging about cloud, IT, and programming. What could go wrong?

Drupal Environment Variables

2018-10-25

In our hosting environment we use a core Drupal 8 Docker container replicated across Amazon EC2 instances with Elastic Container Service. Each of these sites is technically a fully functional independent site with its own domain name, 3 seperate environments, and independent Drupal 8 configuration. So how do we achieve these different site requirements with a single Drupal 8 core Docker container? We can start by using environment variables.

Docker Compose

Docker Compose allows us to launch a docker container from an image using a .yml file to define the configuration.

Here's a sample docker-compose.yml used to launch a website in our environment. Specifically see the drupal -> environment section to see environment variables.

docker-compose.yml
version: "2.1" services: drupal_db: image: mysql:5.7 container_name: drupal_db environment: MYSQL_ROOT_PASSWORD: drupal drupal: image: drupal_core:latest container_name: drupal ports: - 8088:80 environment: DB_NAME: "drupal" DB_USER: "root" DB_PASS: "drupal" DB_HOST: "drupal_db" S3_BUCKET: "" S3_ROOT_FOLDER: "local" S3FS_ACCESS_KEY: "" S3FS_SECRET_KEY: "" SITE_NAME: "Drupal Core - LOCAL" API_KEY: "" links: - drupal_db

When the docker container is lauched via docker-compose these variables will be avaialbe as ENV variables in your hosting environment. These can be accessed in PHP via getenv("env_var").

Consuming ENV with settings.php

Include a local.settings.php to extend settings.php. use getenv() to pull the ENV variables into PHP and then apply to Drupal configuration.

Database Settings
//Database $dbName=getenv("DB_NAME"); $dbUser=getenv("DB_USER"); $dbPass=getenv("DB_PASS"); $host=getenv("DB_HOST"); //apply those variables $databases['default']['default'] = array ( 'database' => $dbName, 'username' => $dbUser, 'password' => $dbPass, 'prefix' => '', 'host' => $host, 'port' => '', 'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql', 'driver' => 'mysql', );

S3 Settings
//S3 $s3Bucket=getenv("S3_BUCKET"); $s3RootFolder=getenv("S3_ROOT_FOLDER"); $s3AccessKey=getenv("S3FS_ACCESS_KEY"); $s3SecretKey=getenv("S3FS_SECRET_KEY"); //apply those variables $config['s3fs.settings']['bucket']=$s3Bucket; $config['s3fs.settings']['root_folder']=$s3RootFolder; $settings['s3fs.access_key']=$s3AccessKey; $settings['s3fs.secret_key']=$s3SecretKey; $settings['s3fs.use_s3_for_public'] = TRUE;

Site Specifics
$config['system.site']['name'] = getenv("SITE_NAME");

Environment Variables Breakdown

The follow table lists and describes each environment variable we use to customize Drupal 8.

Environment Variable Purpose
DB_NAME The database name this site uses
DB_PASS The password for the database
DB_HOST The hostname for the database. If running locally typically this is a linked docker container. If hosting in the cloud (AWS) use the host name
DB_HOST The hostname for the database. If running locally typically this is a linked docker container. If hosting in the cloud (AWS) use the host name
S3_BUCKET The s3 bucket where your images and assets will be stored
S3_ROOT_FOLDER The folder inside S3_BUCKET. This is typically environment specific, DEV/STG/PROD
S3FS_ACCESS_KEY The AWS access key used to upload files to S3_BUCKET. This is required along with the S3FS_SECRET_KEY
S3FS_SECRET_KEY The AWS secret used to upload files to S3_BUCKET. This is required along with the S3FS_ACCESS_KEY
SITE_NAME The name of the site. Visible via the Drupal template
API_KEY Used to connect to AWS API Gateway
comments powered by Disqus