How to Use Takeout to Add New Services to Laravel Sail and Save RAM

Posted on December 8, 2020

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:

  1. Make sure you don't have any other servers like Valet running
  2. Create a new Laravel application
  3. Run sail up -d
  4. Interact with your site at http://localhost/
  5. Shut Sail down, if you'd like, with sail down

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:publish and editing the files in the /docker directory of your project. Then run sail build --no-cache to 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 password.

How to pair Sail and Takeout

Now that you have both working, let's get them connected!

Edit docker-compose.yml to connect to the takeout network

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:

            name: takeout

Your networks key should now look like this:

        driver: bridge
            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:

            - 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 .env.

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 DB_CONNECTION to pgsql, DB_HOST to postgres, DB_PORT to 5432, DB_USERNAME to 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[08006] [7] 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


Quick links to fresh content, and more thoughts that don't make it to the blog