Run prettier on all *.md files
```
prettier '{app,source}/**/**.md' --write
```
			
			
This commit is contained in:
		
							parent
							
								
									a3ceeaf0f3
								
							
						
					
					
						commit
						85a10c545b
					
				
					 170 changed files with 5127 additions and 2282 deletions
				
			
		|  | @ -3,11 +3,14 @@ title: How to Use Environment Variables for your Drupal Settings with Docksal | |||
| date: 2018-06-04 | ||||
| excerpt: How to leverage environment variables with Drupal and Docksal. | ||||
| tags: | ||||
|     - drupal | ||||
|     - drupal-planet | ||||
|     - docksal | ||||
|   - drupal | ||||
|   - drupal-planet | ||||
|   - docksal | ||||
| --- | ||||
| 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: | ||||
| 
 | ||||
| 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: | ||||
| 
 | ||||
| ```language-php | ||||
| $databases['default']['default'] = [ | ||||
|  | @ -19,15 +22,23 @@ $databases['default']['default'] = [ | |||
| ]; | ||||
| ``` | ||||
| 
 | ||||
| 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. | ||||
| 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. | ||||
| 
 | ||||
| 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. | ||||
| 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. | ||||
| 
 | ||||
| It also means that the file can’t simply be re-used on another project as it contains project-specific credentials. | ||||
| It also means that the file can’t simply be re-used on another project as it | ||||
| contains project-specific credentials. | ||||
| 
 | ||||
| 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. | ||||
| 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. | ||||
| 
 | ||||
| For example: | ||||
| 
 | ||||
|  | @ -38,7 +49,8 @@ MYSQL_USER=myproject_user | |||
| MYSQL_PASSWORD=myproject_pass | ||||
| ``` | ||||
| 
 | ||||
| With these in place, they can be referenced within the settings file using the `getenv()` function. | ||||
| With these in place, they can be referenced within the settings file using the | ||||
| `getenv()` function. | ||||
| 
 | ||||
| ``` | ||||
| $databases['default']['default'] = [ | ||||
|  | @ -50,13 +62,19 @@ $databases['default']['default'] = [ | |||
| ]; | ||||
| ``` | ||||
| 
 | ||||
| Now the credentials are no longer duplicated, and the latest values from the environment variables will always be used. | ||||
| Now the credentials are no longer duplicated, and the latest values from the | ||||
| environment variables will always be used. | ||||
| 
 | ||||
| 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 | ||||
| > 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 | ||||
| 
 | ||||
| If you see this, the environment variables aren’t being passed into Docksal’s `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. | ||||
| If you see this, the environment variables aren’t being passed into Docksal’s | ||||
| `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. | ||||
| 
 | ||||
| ```language-yml | ||||
| version: '2.1' | ||||
|  | @ -68,6 +86,7 @@ services: | |||
|       - MYSQL_USER | ||||
| ``` | ||||
| 
 | ||||
| After changing this file, run `fin start` to rebuild the project containers and try to load the site again. | ||||
| After changing this file, run `fin start` to rebuild the project containers and | ||||
| try to load the site again. | ||||
| 
 | ||||
| [0]: https://docksal.readthedocs.io/en/master/advanced/drupal-settings | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue