Lando for Drupal development

3 minutes

From some time I have been using this great tool that facilitates the creation of local environments based on Docker containers, and in this article I want to tell you what it is and some of the features that I think make it great for working with Drupal sites.

What is Lando?

It's a multi-platform and open-source tool that simplifies the management of the containers needed to work in a local environment, to help us work with technologies that resemble our production environment, this configuration is through easy-to-understand files so that all developers in a project can work with the same versions of each service.

Lando uses Docker containers and you may wonder why not do it directly with Docker compose and the answer is because this tool reduces even more complexity for orchestrating multiple services.

Recipes

In our case, Lando has recipes ready to work with Drupal 8 and 9, which means it takes care of configuring containers for PHP, MySQL, served by Nginx or Apache according to our choice.

These recipes serve as a starting point for generating more complex systems, with services such as Solr, Elasticsearch, Redis, Memcached, Nodejs, among others.

Configuration

I'll show you the minimum configurations you need to work a Drupal site with Lando.

.lando.yml

This file must be in the root of your project at the same level as the composer.json file.

name: project-name
recipe: drupal9
config:
  webroot: web
  php: '7.3'
  via: nginx
  drush: false
  xdebug: false
  database: mariadb
  composer_version: 2
services:
  mailhog:
    type: mailhog
    hogfrom:
      - appserver
tooling:
  drush:
    service: appserver
    env:
      DRUSH_OPTIONS_URI: "https://jidrone.lndo.site"
env_file:
  - .lando.env

As you can see at a glance, you can recognize the version of each service, the type of web server and other things without being an expert in Docker.

I'll just clarify some not-so-obvious parts:

Drush

For Drupal 9, it's necessary to set drush: false, this is because from Drupal 9, Drush should not be installed globally, but rather per project via composer.

lando composer require drush/drush

For Drupal 8, you can leave it as is:

drush: 9.6.0-rc3

MailHog

This service allows you to capture the emails sent by your site, which will help you test emails like user registration and others.

Mailhog Lando

Environment Variables

You can create a file with the name you want, I usually call it .env or .lando.env, this file is configured in the .lando.yml file in the following way:

env_file:
  - .lando.env

This file must contain the following data:

MYSQL_USER=drupal9
DRUPAL_MYSQL_HOST=database
MYSQL_PASSWORD=drupal9
MYSQL_DATABASE=drupal9
MYSQL_PORT=3306

Drupal Settings PHP

If you have worked with Drupal, you know that some important configurations, such as the connection to the database, are in a file called settings.php, exactly those data are what we will add with the environment variables we defined earlier, this serves to be able to add this file to the code versioning and define those variables in the production environment.

$databases['default']['default'] = [
  'database' => getenv('MYSQL_DATABASE'),
  'driver' => 'mysql',
  'host' => getenv('MYSQL_HOSTNAME'),
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'password' => getenv('MYSQL_PASSWORD'),
  'port' => getenv('MYSQL_PORT'),
  'prefix' => '',
  'username' => getenv('MYSQL_USER'),
];

Basic Commands

Lando Start

The first time you run this command, it builds the containers and starts them, but once built, the subsequent times only start them.

Lando Restart

As its name suggests, you only use it to restart the containers.

Lando rebuild

This command rebuilds the containers, which is necessary when you need to change the version of some service like PHP, MySQL, etc.

Lando Info

It shows you important information about the services, such as their version, volumes, exposed and internal ports, among other things.

Tooling

This allows you to use command-line tools that you need without having to enter each container, but this is easier to understand with some examples.

Composer

lando composer require drupal/group

Drush

lando drush en group

Drupal Console

lando drupal gpb

Conclusion

In this article, I showed you some very basic things about Lando with which you can start using it, but this tool is very powerful and you can still get a lot more out of it, so I hope I have motivated you to try it.

Jidrone Drupal Developer
J. Ivan Duarte
Drupal Senior Developer

Share