Build it your way with Lando, Docker, and Drupal

Isovera Staff
Isovera
Published in
6 min readSep 27, 2018

My first post in this series, Getting started with Lando, Docker, and Drupal, I explained my favorite method to download and install Drupal locally. I will not repeat the system requirements and how to install Docker and Lando, so check that post if needed.

Same disclaimer as last time:

It is not strictly a requirement, but if you are not comfortable using the command line, then this tutorial is probably not for you.

Now I want to explain what changes depending on how you install Drupal. The earlier post downloads a tarball, and it is not much different if you use a zip file instead. Read on for details on the following methods:

  • Use the official Drupal git repository.
  • Install Drupal with composer.
  • Run a local copy of a site hosted on Acquia.
  • Run a local copy of a site hosted on Pantheon.

Use the official Drupal git repository

Get the code

Download the git repository for Drupal core. If you visit the project page for Drupal core, then you can find the git URL on the “Version control” tab, same as for any other project (module, theme, other) hosted on drupal.org:

git clone --branch 8.6.x https://git.drupal.org/project/drupal.git drupal-lando-git
cd drupal-lando-git

Optionally, check out a specific version. As of today, the latest release is 8.6.1:

git checkout 8.6.1

Install dependencies

Since version 8.3, Drupal’s git repository does not commit Symfony and other packages on which it depends. You have to install them using composer: see Install dependencies with composer in the “Installing Drupal 8” guide.

composer install -o --no-dev

The -o option tells composer to generate optimized autoload files.

The --no-dev option means that composer should not install development packages, such as PHPUnit. I would not actually use that option for a local installation, but I am afraid that someone will copy and paste my command when installing on a production site, where some of the development packages create a security hole.

Initialize Lando

Normally, I would run lando init interactively, but for the sake of copy-and-paste, you can use

lando init --recipe drupal8 --webroot=. --name="drupal-lando-git"

Alternatively, just create .lando.yml with these lines:

name: drupal-lando-git
recipe: drupal8
config:
webroot: .

Install Drupal

Start Lando with

lando start

If all goes well, you should get a message like this:

BOOMSHAKALAKA!!!Your app has started up correctly.
Here are some vitals:
NAME drupallandogit
LOCATION /Users/bfisher/Sites/drupal-lando-git
SERVICES appserver, database
APPSERVER URLS https://localhost:32772
http://localhost:32773
http://drupallandogit.lndo.site
https://drupallandogit.lndo.site

Then you can visit http://drupallandogit.lndo.site and install Drupal as usual.

Now that Drupal 8.6 has been released, we can choose the Umami installation profile:

As explained in the previous post, lando info will tell you what to fill in for the database credentials:

  • Database name: drupal8
  • Database username: drupal8
  • Database password: drupal8
  • Host: database (under “Advanced options”)

Since I chose the Umami installation profile, it takes a little longer than usual, but in a few minutes we get our reward:

Install Drupal with composer

There are a few ways to manage a Drupal site using composer described on Using Composer to manage Drupal site dependencies. Although not officially supported by the Drupal project, the standard method is to use the Composer template for Drupal projects, also known as drupal-composer/drupal-project.

Assuming that you already have PHP and composer installed, you can install Drupal in the directory lando-drupal-composer following the instructions there:

composer create-project drupal-composer/drupal-project:8.x-dev lando-drupal-composer --stability dev --no-interaction

This command creates your project in the directory lando-drupal-composer/. It creates the document root in the web/ subdirectory: that is where to look for index.php, the core/ subdirectory, and so on. It also installs Drush, Drupal Console, and a few other projects for you, and it takes care of running composer install.

After that, change to the new directory and initialize Lando:

cd lando-drupal-composer
lando init --recipe drupal8 --webroot=web --name="drupal-lando-composer"
lando start

The only differences here are the --name option and --webroot=web. Here is the generated .lando.yml:

name: drupal-lando-composer
recipe: drupal8
config:
webroot: web

Now you can visit http://drupallandocomposer.lndo.site and install Drupal.

Run a local copy of a site hosted on Acquia

Acquia does not provide any special support for Lando, but if you are using Acquia’s BLT system, then see Lando’s blog post and BLT docs on Lando for how to set up BLT with Lando as the local dev environment.

For this post, here are some basics:

Acquia uses docroot as the web root

Sites hosted on Acquia use docroot/ as the web root, so you should specify this when creating .lando.yml. You can add a command-line option when running lando init:

lando init --recipe drupal8 --webroot=docroot --name="drupal-lando-acquia"

or you can edit the generated file:

name: drupal-lando-acquia
recipe: drupal8
config:
webroot: docroot

Importing the database

This is not specific to Acquia, but this is the first time I have talked about using Lando to make a local version of an existing site, so what about the database?

Using the Acquia dashboard or some other means, you can download a database backup. Save it in the main directory of your local site (the same directory as .lando.yml) and then you can import it with

lando db-import ENV-SITE-DATESTAMP.sql.gz

This will drop all tables in the existing database before importing from the backup. If, for some reason, you do not want to drop the existing tables, then you can add the --no-wipe option.

Run a local copy of a site hosted on Pantheon

Lando has a recipe customized for Pantheon. See Working with Pantheon in the Lando documentation for details.

Using the pantheon recipe has two advantages:

  1. The local environment matches Pantheon’s: nginx, mariadb, redis, etc.
  2. You get additional Lando sub-commands for working with your Pantheon site.

Initialize your site with “lando init pantheon”

Unlike the other methods, there is magic in the lando init pantheon command that is not captured in .lando.yml, so actually run this command:

lando init pantheon --destination=mysite --pantheon-auth=TOKEN --pantheon-site=mysite --name=mysite
cd mysite

Or at least run lando init pantheon (NOT ... --recipe=pantheon) and supply the rest interactively.

If you do not already have a token, then see Creating and Revoking Machine Tokens in the Pantheon documentation.

This command will create mysite/ as a subdirectory of the current directory, with a .lando.ymlsomething like this:

name: mysite
recipe: pantheon
config:
php: '7.1'
webroot: web
framework: drupal8
site: mysite
id: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

Start the site

Before going any further, start your Docker containers with lando start and visit your site. Note that, after lando init pantheon, I already changed to the new directory.

When testing for this blog post, I got an error because of Trusted Host settings. I added this line to settings.php:

$settings['trusted_host_patterns'][] = '\.lndo\.site$';

If you have configured settings.php to include a local settings file, then that is the right place to add this line.

Get all the things

The pantheon recipe supplies a lando pull sub-command. You can use it to pull the code, database, and files from any of your Pantheon environments (“dev”, “test”, or “live”). You can also specify “none” to skip any of these.

For example, to skip the code (since we just did a git clone) and get the files and database from the dev environment,

lando terminus auth:login --machine-token=t0Rj1g5DHGOBm8an9jWHGKYA8uqSGzGpbXgS49axPyjtI
lando pull --code=none --database=dev --files=dev

(I may have done something wrong, because I had to re-authenticate.)

Alternatively, just use lando pull and supply the values interactively.

Other Lando sub-commands

Besides the pull sub-command, there are several others for working with Pantheon. (I have edited out the generic commands from this output.)

$ lando Usage: lando <command> [args] [options] [-- global options]Commands:
pull Pull code, database and/or files from Pantheon
push Push code, database and/or files to Pantheon
redis-cli Run redis-cli commands
switch <env> Switch to a different multidev environment
terminus Run terminus commands
varnishadm Run varnishadm commands
Global Options:
--help, -h Show help
--verbose, -v, -vv, -vvv, -vvvv Change verbosity of output
You need at least one command before moving on

This article was originally published by Benji Fisher on our Isovera blog.

--

--