Tutorials > How to run Laravel in a Docker container using Laravel Sail

How to run Laravel in a Docker container using Laravel Sail

Published on: 06 July 2021

Development Docker Laravel


Docker is a tool chosen by many programmers because of the support it offers in application development. The tool allows, in fact, to create containers that make applications independent from the development environment or from the operating system that they are run with.

Docker works in combination with the Laravel framework. In this guide you will discover Sail, a package for using Laravel in a docked version, which creates a development environment with Docker, with no manual execution the containers.

The environment created will automatically provide an installation of PHP, MySQL and Redis. This is a great advantage for a Laravel developer, as you don't have to worry about installing these packages on your OS or checking that they are properly updated. In fact, containers compatible with different versions can be created and deleted in order to adapt them to the development you want for your application.

Another advantage is the ability to share the project development environment with your collaborators, without requiring the installation of other software on their operating system.

With the Sail package, only two files will be provided:

  • docker-compose.yml, which will manage the containers for your application;
  • A file for interactions with the containers.


The only requirement to use Sail is to have Docker installed on your operating system. In case Docker isn’t installed and you are using Ubuntu, follow our guide on  How to Install Docker on Ubuntu 18.04.

Installing and configuring Sail

Start by creating a new Laravel application via the command:

$ curl -s https://laravel.build/my_sailapplication | bash

With this code, a new Laravel project will have been created in a directory called, as in the example, "my_sailapplication".

Now, move to the project directory:

$ cd new-sail-application

Now,launch Sail:

$./vendor/bin/sail up

Adding Sail to an existing application

Using the composer command, add Sail to an existing application. To do so, use the command:

$ composer require laravel/sail --dev

Once the installation is complete, insert Sail's docker-compose.yml file in your project directory, using the command:

$ php artisan sail:install

Finally, as beforehand, launch Sail:

$./vendor/bin/sail up

Analysing the development environment

The three main containers in your development environment will cover the three services installed by default with Sail: PHP, MySQL and Redis.

Inside the project directory is the docker-compose.yml file mentioned above. This file will contain a section dedicated to the 3 services mentioned above: laravel.test (for PHP) , mysql and redis.

Let's start seeing, step by step, the configurations of the various services.


In the database configuration, the first entry is image, which indicates the image and version of MySQL to be instantiated in your container.

The images are provided by Docker Hub, a huge library of images on the web. Visiting this website is recommended to explore its possibilities and details when being unfamiliar with it.

Another important entry is ports, which is used to synchronize the container port with the local one. In fact, the $ {FORWARD_DB_PORT: -3306}: 3306 will have precisely this function.

The database selected and its credentials will be found under environment,.

But will the destruction of a container and its environment lead to the destruction of persistent data such as the database? The answer, fortunately, is no. Docker saves the persistent data in an external path that the containers can then refer to. Another advantage lies in the possibility to refer more containers to the same "database".

The data are usually saved in the volumes. In the statement volumes: - 'sailmysql: / var / lib / mysql', the use of a volume named sailmysql will be specified to save data in the var / lib / mysql directory.

The services of your container will, of course, have to cooperate on the same network (in this case, that of Sail.). the network parameter with the sail entry indicates just that.

Finally, the healthcare item specifies the commands to be launched to allow Docker to perform a check on the correct functioning of your containers.


The laravel.test section is dedicated to services related to the PHP language.

The first parameter you encounter is build. The parameter contains the path to a Docker file with instructions for creating the image that will be instantiated.

The image parameter points to the image that will be instantiated. Created by the parameter build. ports, its function is to connect the container and the local port.

In environments the credentials to be used in the PHP code are described; volumes is used to save permanent information in volumes, as explained above. While networks specifies the network the service should be connected to.

The depends_on parameter finally specifies which services should be running before your application starts.


Redis has parameters similar to those of MySQL.

Basically, an image is instantiated, after which the ports between the localhost and the container are synchronized. Through the volume, however, persistent data are saved.

Finally, the networks and healthcheck parameter serve to establish the network to operate on, as well as, to check the correct functioning of Redis.

Sail's first commands

As you often have to use vendor / bin / sail, assigning this string to a shorter alias is preferable.

$ alias sail='bash vendor/bin/sail'

To run containers from docker-compose.yml :

$ sail up

To run a container in the background, use the command:

$ sail up -d

To stop the execution, instead, press CTRL + C and, to end the background processes, run the command:

$ sail down

For other commands such as those of artisan, composer or npm, it is advisable to add the alias sail as a prefix to these commands.

For example:

$ sail composer require laravel/sanctum


At this point, you will have created and configured your first application with Sail, and understood how it works and the meaning of the entries in the docker-compose.yml file and the services it offers.