oliverdavies.uk/source/_posts/using-environment-variables-settings-docksal.md

93 lines
2.9 KiB
Markdown
Raw Normal View History

2018-06-04 20:25:13 +00:00
---
title: How to Use Environment Variables for your Drupal Settings with Docksal
2020-03-08 14:32:13 +00:00
date: 2018-06-04
2018-12-31 12:13:05 +00:00
excerpt: How to leverage environment variables with Drupal and Docksal.
2018-06-04 20:25:13 +00:00
tags:
- drupal
- drupal-planet
- docksal
2018-06-04 20:25:13 +00:00
---
Within the [Docksal documentation for Drupal settings][0], the example database
settings include hard-coded credentials to connect to the Drupal database. For
example, within a `settings.php` file, you could add this:
2018-06-04 20:25:13 +00:00
```language-php
$databases['default']['default'] = [
'driver' => 'mysql',
'host' => 'db',
'database' => 'myproject_db',
'username' => 'myproject_user',
'password' => 'myproject_pass',
];
```
Whilst this is fine, it does mean that there is duplication in the codebase as
the database credentials can also be added as environment variations within
`.docksal/docksal.env` - this is definitely the case if you want to use a custom
database name, for example.
2018-06-04 20:25:13 +00:00
Also if one of these values were to change, then Drupal wouldn't be aware of
that and would no longer be able to connect to the database.
2018-06-04 20:25:13 +00:00
It also means that the file cant simply be re-used on another project as it
contains project-specific credentials.
2018-06-04 20:25:13 +00:00
We can improve this by using the environment variables within the settings file.
The relevant environment variables are `MYSQL_DATABASE` for the database name,
and `MYSQL_USER` and `MYSQL_PASSWORD` for the MySQL username and password. These
can be set in `.docksal/docksal.env`, and will need to be present for this to
work.
2018-06-04 20:25:13 +00:00
For example:
```
DOCKSAL_STACK=default
2018-08-31 08:05:12 +00:00
MYSQL_DATABASE=myproject_db
2018-06-04 20:25:13 +00:00
MYSQL_USER=myproject_user
MYSQL_PASSWORD=myproject_pass
```
With these in place, they can be referenced within the settings file using the
`getenv()` function.
2018-06-04 20:25:13 +00:00
```
$databases['default']['default'] = [
'driver' => 'mysql',
'host' => 'db',
2018-08-31 08:05:12 +00:00
'database' => getenv('MYSQL_DATABASE'),
2018-06-04 20:25:13 +00:00
'username' => getenv('MYSQL_USER'),
'password' => getenv('MYSQL_PASSWORD'),
];
```
Now the credentials are no longer duplicated, and the latest values from the
environment variables will always be used.
2018-06-04 20:25:13 +00:00
However, you may see a message like this when you try and load the site:
> Drupal\Core\Database\DatabaseAccessDeniedException: SQLSTATE[HY000][1045]
> Access denied for user ''@'172.19.0.4' (using password: NO) in
> /var/www/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php on line 156
2018-06-04 20:25:13 +00:00
If you see this, the environment variables arent being passed into Docksals
`cli` container, so the values are not being populated. To enable them, edit
`.docksal/docksal.yml` and add `MYSQL_DATABASE`, `MYSQL_PASSWORD` and
`MYSQL_USER` to the `environment` section of the `cli` service.
2018-06-04 20:25:13 +00:00
```language-yml
version: '2.1'
services:
cli:
environment:
2018-08-31 08:05:12 +00:00
- MYSQL_DATABASE
2018-06-04 20:25:13 +00:00
- MYSQL_PASSWORD
- MYSQL_USER
```
After changing this file, run `fin start` to rebuild the project containers and
try to load the site again.
2018-06-04 20:25:13 +00:00
[0]: https://docksal.readthedocs.io/en/master/advanced/drupal-settings