How to Use Takeout to Add New Services to Laravel Sail and Save RAM
Posted on December 8, 2020
!Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.
Today Taylor Otwell, the creator of Laravel, released Laravel Sail, a tool that makes it incredibly easy to spin up development environments with Docker for your Laravel applications.
Out of the box, Sail comes with MySQL, Redis, and MailHog. But what if you want to add PostgreSQL? ElasticSearch? Memcached? MsSQL? Or what if you have four Sail environments running, each using 300-400MB of RAM for their MySQL instances?
Never fear: Sail works great with Tighten's tool Takeout, a simple CLI tool for managing one-off Docker services.
How to use Sail
As always, the Sail docs are your best option. But here's the simple rundown:
- Make sure you don't have any other servers like Valet running
- Create a new Laravel application
sail up -d
- Interact with your site at http://localhost/
- Shut Sail down, if you'd like, with
That's about it! You'll also want to pass any commands (for example,
php artisan migrate) through Sail:
sail artisan migrate, or
sail composer require tightenco/tlint. Or, you can run
sail shell to run Bash on the container,
sail test to run your tests, or
sail tinker to run Tinker.
The docs also show you how to connect to your MySQL container using your favorite SQL GUI.
Note: If you're a Docker pro, but still want to use Sail, you can publish your Sail config files by running
sail artisan sail:publishand editing the files in the
/dockerdirectory of your project. Then run
sail build --no-cacheto update your Sail containers.
How to use Takeout
Takeout is also a tool for managing Docker, but it focuses on creating one-off containers for common services that can be shared by all of your local projects, whether or not those projects are using Docker.
To install Takeout, run
composer global require tightenco/takeout. Don't have Composer installed globally? No worries. Try Liftoff, the quickest way to get Composer and Takeout installed on your machine.
In order to spin up a service—let's say you want to use PostgreSQL—run
takeout enable servicename, or just run
takeout enable to choose from a list. So, for now,
takeout enable postgresql, follow the prompts, and you'll be up and running.
Note: If you're actually following along, it's much easier to work with PostgreSQL if you set a password, so for now I just set mine to
How to pair Sail and Takeout
Now that you have both working, let's get them connected!
docker-compose.yml to connect to the
In order to connect Sail and Takeout, we'll just add a "network" to your
docker-compose.yml file. Open that file in your favorite editor, scroll all the way down to the bottom, and under the
networks setting, add this entry:
takeout: external: name: takeout
networks key should now look like this:
networks: sail: driver: bridge takeout external: name: takeout
Then find the
networks keyed object under
services:laravel.test:networks (it's line 18 at the time of this writing), and add a new line for
- takeout, making it look like this:
services: laravel.test: [...] networks: - sail - takeout
Save that file, and now run
sail build --no-cache && sail up. Your app should now have access to your Takeout-managed instance of PostgreSQL.
Referencing Takeout services in your configuration
Every Takeout services gets an alias that you can use to refer to it in your Laravel configuration (
.env or elsewhere). You can find those aliases by running
takeout list, which should give you output like this (I removed some irrelevant columns to save space on the blog):
+---------------------------------+-------------+-------------------+ | Names | Base Alias | Full Alias | +---------------------------------+-------------+-------------------+ | TO--postgresql--9.6.20--5432 | postgres | postgresql9.6 | +---------------------------------+-------------+-------------------+
For any row that has both a
Base alias and a
Full Alias, I can use either to refer to this service in my
However, if I have more than one PostgreSQL instance running through Takeout, only one will get the Base Alias of
postgres, which is why there's the Full Alias if I need it.
So now, I can update my
.env and set
postgres, and my password to the password I set when I spun it up with Takeout (
password) and that's it!
You can run
sail artisan migrate and see that it's now connecting to your Postgres database. If you've followed along, you'll actually see an error:
SQLSTATE  FATAL: database "my_app" does not exist, but that just a sign you need to create the database, which is out of the scope of this post. (TLDR: easiest option is to connect your SQL GUI to your new database and create it there).
That's it! Enjoy!
Comments? I'm @stauffermatt on Twitter
Tags: laravel • sail • docker • takeout