Creating command aliases for Lando tooling

I’m a big fan of Lando. Since first learning about it, I’ve switched from using a virtual machine with Vagrant (i.e. VVV) to using Docker containers for my day-to-day local development environment. Docker on its own is not the most ergonomic (at least, I’m not an expert), so wrappers like Local by Flywheel make it easy to use. Lando makes Docker easy to use while also having a similar developer experience as Vagrant. My colleague Felix Arntz and I made a Lando-based wordpressdev environment which use now instead of VVV for development of WordPress core, themes, and plugins. The AMP project also now has a Lando configuration for contributing.

Lando has a tooling facility which makes it easy to execute commands inside a container from the host machine. For example, to invoke npm in a container from the host, you can do lando npm. Our wordpressdev environment has about a dozen such tooling commands. In order to invoke these commands as if they were not in a container, I’ve added a landoify function to my .bashrc which adds aliases so that what I run on my host command line actually gets run in a container. When I start working on a given project, I can open a terminal tab, go to the project directory, and run:

lando start; landoify

Then I can run commands as normal, like npm run build or wp plugin activate amp, but they run in a Docker container. This prevents you from having to prefix commands with lando and it prevents you from accidentally running a command outside a container.

In order to keep track of the fact that I’ve currently entered into this Lando virtual environment, the landoify command also prefixes the command prompt with [Lando]. Here’s the Bash function I’ve added to my .bashrc (without some of my modifications):

function landoify {
	alias composer="lando composer"
	alias grunt="lando grunt"
	alias gulp="lando gulp"
	alias npx="lando npx"
	alias phpunit="lando phpunit"
	alias wp="lando wp"
	alias yarn="lando yarn"
	# Add any other aliases you want based on your environment...

	# Modify this as required for your prompt.
	if ! grep -qi lando <<< $PS1; then
		PS1="[Lando] $PS1"
	fi
}

I hope it’s useful to you as well.

Aside: It would be cool if running lando start (or another subcommand) could automatically set such aliases for the tooling in a given project.