Kickstart Drupal 8 with Relocated Root and Docker
Bonus: adding the Bulma CSS Framework based theme
“A computer will do what you tell it to do, but that may be much different from what you had in mind.” Joseph Weizenbaum
This method simplified my way of working with Drupal.
Prerequisites
You need composer, docker and docker-compose installed on your system.
The Drupal Community recommend installing Drupal using Drupal Recommended Project
composer create-project drupal/recommended-project:8.9.2 mywebroot
This will install drupal 8.9.2 into your mywebroot
folder.
In the drupal installation dir, there is a sub directory named web
. I
Inside this dir will contain your full drupal package. On the other side vendor
folder contains various utilities.
tree -L 2
├── composer.json
├── composer.lock
├── vendor
│ ├── asm89
│ ├── autoload.php
│ ├── composer
│ ├── doctrine
│ ├── drupal
│ │ ├── core-composer-scaffold
│ │ └── core-project-message
│ │
│ ├── easyrdf
│ ├── egulias
│ ├── guzzlehttp
│ ├── laminas
│ ├── masterminds
│ ├── paragonie
│ ├── pear
│ ├── psr
│ ├── ralouphie
│ ├── stack
│ ├── symfony
│ ├── symfony-cmf
│ ├── twig
│ └── typo3
└── web
├── autoload.php
├── core
├── example.gitignore
├── index.php
├── INSTALL.txt
├── modules
├── profiles
├── README.txt
├── robots.txt
├── sites
├── themes
├── update.php
└── web.config
As shown in the command line descriptive
Next steps:
- Install the site: https://www.drupal.org/docs/8/install
- Read the user guide: https://www.drupal.org/docs/user_guide/en/index.html
- Get support: https://www.drupal.org/support
- Get involved with the Drupal community: https://www.drupal.org/getting-involved
- Remove the plugin that prints this message: composer remove drupal/core-project-message
- Homepage: https://www.drupal.org/project/drupal
- Support:
- docs: https://www.drupal.org/docs/user_guide/en/index.html
- chat: https://www.drupal.org/node/314178
Prepare the Docker containers
I create the file docker-compose.yml
in my project root.
version: '3'
services:
web:
image: mitlabs/apache-php7.4
ports:
- 8080:80
volumes:
- .:/var/www/html
links:
- 'mysql'
mysql:
image: mariadb:latest
restart: always
volumes:
- ./data/db:/var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: mysecretpassword
restart: always
Manage the containers
After everything is ready, I can turn on the two containers on: docker-compose up -d
or off: docker-compose down
.
I can get inside the web containers and enter commands as root at the prompter docker-compose exec web bash
Whenever I want to shutdown connection I type docker-compose down
I visit http://localhost:8080/web
and start to install a new Drupal 8 project.
First time will run the installer.
You solve the permission this way:
chmod a+w sites/default
cp default.settings.php settings.php
chmod a+w settings.php
All necessary changes to sites/default and sites/default/settings.php have been made, so you should remove write permissions to them now in order to avoid security risks. If you are unsure how to do so, consult the online handbook. Server Permissions
When needs to configure database:
DRUPAL_DATABASE_NAME: "drupal"
DRUPAL_DATABASE_USERNAME: "root"
DRUPAL_DATABASE_PASSWORD: ""
DRUPAL_DATABASE_PREFIX: ""
DRUPAL_DATABASE_HOST: "mysql"
The last step is very important you need to explicitly edit the host as default is localhost
.
Change it from localhost
to mysql
.
Install additional modules
Search for the desired Drupal project:
composer browse drupal/coffee
It will open up the page: https://git.drupalcode.org/project/coffee
Run the desired module from the root folder (not from web).
composer require drupal/coffee
Literally this adds the next line "drupal/coffee": "^1.0"
into the composer.org
"require": {
"composer/installers": "^1.2",
"drupal/coffee": "^1.0",
"drupal/core-composer-scaffold": "^8.8",
"drupal/core-project-message": "^8.8",
"drupal/core-recommended": "^8.8"
},
You refresh the Extended page and you’ll see the module in the list:
Note you can remove drupal/core-project-message
as suggested above.
composer remove drupal/core-project-message
Optionally, the same way you can install Drupal commerce or any other module
composer require "drupalcommerce/commerce_base dev-8.x-1.x"
When you finish your day, you can turn off the containers anytime with docker-compose down
or docker-compose stop
and ressurect them back with docker-compose up -d
(-d stands for dettach mode)
Now install the bulma theme:
composer require drupal/bulma
Enable the theme and set it default and tada:
Handling database import and export
If you already have an existing project you can put the project under drupal/web folder without running the installation script. After that, you can export and import database by using these commands
For export database
docker exec drupal8_mysql /usr/bin/mysqldump -u root --password=password drupal > drupal/backup.sql
For import database
cat drupal/backup.sql | docker exec -i drupal8_mysql /usr/bin/mysql -u root --password=password drupal
Note: you can display images with docker images ls
and remove unwanted images from the host node: docker rmi image-name