Blue container

Earlier this year I wrote a guide on how to use Docker to develop in WordPress. I really enjoyed working on that post, and it looks like you liked it too (thanks for all your comments and emails). If you didn’t read the post, here’s a quick reminder:

Docker is a program that allows you to package software in containers and run them on top of virtual machines. The main advantage of Docker is that its containers run using the virtualization environment provided by the host operating system, resulting in fast and light containers.

The main problem Docker has is that its configuration is cumbersome. For example, to create a WordPress development environment, we need to start two (at least) services: a database service and a webapp server.

In the post that commented at the beginning we saw a tool, Docker Compose, that allows to define all the services we have to spin up for a given project. With Docker Compose, we managed to define in a single file docker-compose.yml all the dependencies of our project and kick it off with a single command.

Or that is what I initially promised, because I closed the post by explaining how to configure yet another Docker service that would allow you to use beautiful domain names in your WordPress installations. If you remember, with the original docker-compose.yml file, our WordPress dev env would be accessible via http://localhost:8080. But if we wanted to use something like http://myplugin.local we had to set up a proxy that could resolve domain names and add more settings to our docker-compose.yml

I know, I know—it was quite complicated to get things up and running! But once you had everything properly set up, it was quite easy, right? Anyway, I’ve decided to introduce you my new friend: Lando. He’ll help you set up new development environments in seconds.

Enter Lando

I think the best way to introduce yourself to Lando is by using a sentence from his documentation:

Lando is for developers who want to avoid the buildt-in-masochism of directly using docker or docker-compose.

Lando’s docs

Yup, apparently using docker and docker-compose is for masochists… So let’s see how we can specify quickly and without complications all the services we need to work on a new WordPress project.

Installing Lando

The first thing you should do to be able to use Lando is (surprise, surprise!) install it on your computer. The installation process is extremely simple and is very well documented, but we can summarize it in the following steps:

  1. Lando requires Docker. If you are on Linux, install Docker Community Edition first. If you are on Mac or Windows, Lando’s installer itself will install Docker if it is not already available.
  2. Download the appropriate Lando installer for your operating system. In my case, for example, I downloaded the file lando-v3.0.0-rc.22.deb (I’m on Linux) and installed it with dpkg. For Mac, use the .dmg file and, for Windows, the .exe.
  3. Run the installer and follow the steps.

And that’s it!

How to Create a new WordPress Project in Lando

The first thing we have to do is go to the project folder we want to work with (or create a new one for it). In my case, I will create a new WordPress dev env to develop our Nelio A/B Testing plugin, so I’ll cd to the folder where I downloaded the project: ~/dev/plugins/nelio-ab-testing.

Once there, we have to create Lando’s configuration file. This file is similar to docker-compose.yml, but much simpler. For starters, you don’t even need to write it—Lando can do it for you! Simply use lando init to create a new development environment:

lando init \
  --recipe wordpress
  --source remote
  --remote-url https://wordpress.org/latest.tar.gz
  --webroot wordpress
  --name "Nelio AB Testing" # name the project as you want

Let’s see what the previous instruction does:

  • First, we tell Lando we want to create a new configuration file in the current directory using lando init.
  • The first parameter (--recipe wordpress) tells Lando that we want to create a WordPress project. This will help Lando know the services it has to set up (a database, a web server, etc).
  • Then we indicate we want Lando to download WordPress from WordPress.org.
  • With --webroot wordpress we tell Lando where WordPress is going to be installed. Since we just specified that WordPress has to be downloaded from WordPress.org and the zip Lando will download will create a wordpress folder after unzipping, that’s the location we use for Lando.
  • Finally, we specify the name of our project (Lando will use it to generate friendly URL domains).

After running the previous command, you will see how Lando installs everything and welcomes you with the following info:

NOW WE'RE COOKING WITH FIRE!!!
Your app has been initialized!
Go to the directory where your app was initialized and run `lando start` to get rolling.
Check the LOCATION printed below if you are unsure where to go.
Oh... and here are some vitals:
 NAME      my-site                                               
 LOCATION  /tmp/plugin                                           
 RECIPE    wordpress                                             
 DOCS      https://docs.devwithlando.io/tutorials/wordpress.html

List all files in the current folder and you’ll see Lando created a hidden file named .lando.yml with the following content:

name: my-site
recipe: wordpress
config:
  webroot: wordpress

Extremely simple, right? Now start your service using lando start and you’re good to go:

BOOMSHAKALAKA!!!
Your app has started up correctly.
Here are some vitals:
 NAME            my-site                   
 LOCATION        /tmp/plugin               
 SERVICES        appserver, database       
 APPSERVER URLS  https://localhost:32781   
                 http://localhost:32782    
                 http://nelio-ab-testing.lndo.site  
                 https://nelio-ab-testing.lndo.site

Congrats! You now have a new WordPress installation up and running. If we now go to one of the URLs Lando listed on start, you’ll see you simply need to follow the steps to complete the installation of WordPress:

Install WordPress in Country
Install WordPress in Lando.

Completing WordPress Installation Quickly

Obviously you can complete the installation of WordPress using the installer of the previous screen. But there is an even better solution: use WP-CLI, which we also have available in Lando.

To install WordPress with WP-CLI we need to first create a wp-config.php in the wordpress directory. To create that file, we need some information about our database: where it is, its username and password, etc. All this information can be found via lando info, which returns the following JSON:

[
  {
    service: 'appserver',
    urls: [
      'https://localhost:32781',
      'http://localhost:32782',
      'http://nelio-ab-testing.lndo.site',
      'https://nelio-ab-testing.lndo.site'
    ],
    //...
  },
  {
    service: 'database',
    //...
    creds: {
      database: 'wordpress',
      password: 'wordpress',
      user: 'wordpress',
    },
    //...
  }
]

Since the results is a JSON string, we can use Unix’s jq tool to retrieve the specific values we’re interested in. For example, if we want to know the user of our database, just run this:

$ lando info --service database --format json | jq -r ".[0].creds.user"
wordpress

Now, let’s leverage this command and retrieve all the relevant data we need to build the config file using WP-CLI:

# Find WordPress' directory (webroot entry in .lando.yml)
WEBROOT=`grep webroot .lando.yml | cut -d: -f2 | xargs`
# Retrieve database credentials using lando info
DBNAME=`lando info --service database --format json | jq -r ".[0].creds.database"`
DBUSER=`lando info --service database --format json | jq -r ".[0].creds.user"`
DBPASS=`lando info --service database --format json | jq -r ".[0].creds.password"`
DBHOST=`lando info --service database --format json | jq -r ".[0].hostnames[0]"`
# Create WordPress' config file
lando wp config create \
  --dbname=$DBNAME \
  --dbuser=$DBUSER \
  --dbpass=$DBPASS \
  --dbhost=$DBHOST \
  --path=$WEBROOT

Once we have the file created, we can launch the installation command:

# Get WordPress' project name from .lando.yml
PROJECT_NAME=`grep name .lando.yml | cut -d: -f2 | xargs`
SITE_NAME=`echo "$PROJECT_NAME" | sed -e "s/-/ /g" | sed "s/\b[a-z]/\u&/g"`
# Get one of the possible WordPress' URLs ando created (https)
URL=`lando info --service appserver --format json | jq -r ".[0].urls" | grep -vw localhost | grep https: | cut -d'"' -f2`
# Extract the domain from the previous URL (removing https)
DOMAIN=`echo "$URL" | sed -e "s/^https:\/\///"`
# Install WordPress
lando wp core install
 --url=$URL
 --title="$SITE_NAME"
 --admin_user=admin
 --admin_password=password
 --admin_email=admin@$DOMAIN
 --path=$WEBROOT

Done! We already have WordPress installed and working.

How to put our project into WordPress

If we now log in into WordPress and we take a look at the available plugins, we’ll see that our plugin (Nelio A/B Testing, in my example) is not there. That’s because Nelio A/B Testing is not in wordpress/wp-content/plugins/. To fix this, we simply need to create a soft link in this directory that points to the root of our project:

cd wordpress/wp-content/plugins/
ln -s ../../../ nelio-ab-testing
cd -

And that’s it!

It’s now up to you to decide the approach that better suits your needs. If you want to create a WordPress install for every project, just follow the steps I’ve described. If, on the other hand, you want a single WordPress installation with all your themes and plugins in it, then simply create a new folder, init Lando there, and add all your plugins in wordpress/wp-content/plugins/.

I hope you liked this post and tool as much as I did. And I hope it makes your work easier and funnier. Since using Lando, I moved away from Docker completely.

Featured image by Victoire Joncheray on Unsplash.

2 responses to “Local WordPress Development with Lando (or Docker made easy)”

  1. Leonardo Losoviz Avatar
    Leonardo Losoviz

    Hola David,

    great guide on Lando + WordPress, it tackles exactly what I was looking for: how to have Lando already install the WP DB.

    I have a suggestion. Using WP CLI to automatically install WP is such a common use case, that I think it should be part of the Lando WordPress recipe (https://docs.lando.dev/config/wordpress.html) already. Would you consider adding these steps to the recipe in the Lando repo (https://github.com/lando/lando), maybe adding an additional flag `–install-db` to trigger the execution? That would be awesome.

    (I haven’t contributed to Lando or anything… but it just makes so much sense!)

    Concerning the symlink to the plugin, I had done that, but it doesn’t work, because the WordPress installation is actually read from the Docker container, and the target from the symlink in your local drive cannot be accessed from within’s the Docker container.

    So I found a solution to this issue (documented in more detail here: https://leoloso.com/posts/lando-symlinks/):

    1. Have your source code live under your Home directory, which can be read by the Docker container
    2. Create the symlink in Lando when building the service, in .lando.yml:

    yaml
    services:
     appserver:
      run_as_root:
       # Symlink to the source files
       - ln -snf /user/GitRepos/your-plugin-name /app/wordpress/wp-content/plugins/your-plugin-name

    Cheers

    1. David Aguilera Avatar

      Thanks, Leonardo. I’m happy you liked our post. And yes, I agree with everything you said: it’d be great if Lando’s recipe already installed WordPress in its environment… I haven’t contributed to the project either, but it looks like a good idea.

      Regarding symlinks… In my particular case, things work because WordPress is installed inside the plugin itself (my-plugin/.lando.yml and my-plugin/.wordpress), so a relative symlink like my-plugin/.wordpress/wp-content/plugins/../../../ works as expected 🙂

      Anyway, thanks for sharing your solution!

Leave a Reply

Your email address will not be published. Required fields are marked *

I have read and agree to the Nelio Software Privacy Policy

Your personal data will be located on SiteGround and will be treated by Nelio Software with the sole purpose of publishing this comment here. The legitimation is carried out through your express consent. Contact us to access, rectify, limit, or delete your data.