Introduction to the

Docker is a tool for creating application containers. These containers are used to wrap applications, so they can run smoothly in any environment (or operating system) without requiring any special setup or configuration.

Sail provides a Docker development environment for your Laravel applications. With it, you don’t need to manually create a Docker container for your Laravel app. It will provide you with a native development environment, including PHP, MySQL, and Redis by default, although you can choose the specific services you want. This means that Laravel developers no longer need to do any special configuration (such as installing PHP, MySQL, local servers, etc.) on their operating systems to start building Laravel applications. With Sail, they can start running.

Another advantage of Sail is that developers don’t have to worry about installing the correct version of configuration software on their systems. With Sail containers, if there is an outage due to compatibility issues, you can simply delete the container and create a new one, all out of your local environment, leaving no footprints.

If you’re working with other developers on a project, Sail makes sharing simple and straightforward because there’s no need to think about the operating system the other developers are using.

Sail is the favored, Valet, Laragon, Takeout, Laradock and Vessel after a long list of Laravel development environment to try.

The entire package consists of two files: Yml, which contains your application’s Docker containers, and the Sail script, which provides you with cli.docker-compose. Yml is located in the root directory of your project, The Sail script is in your vendor/bin directory.

In this article, we will learn how to install and utilize a Laravel Sail. We’ll create a new project and take a closer look at some basic Sail commands.

A prerequisite for

Sail’s only requirement is to install Docker on the operating system it runs on.

If you use Windows you will need to install and enable Windows Subsystem for Linux 2 (WSL2). It will allow you to run Linux binary executables natively on your Windows operating system. Also, be sure to configure Docker Desktop to use the WSL2 backend.

Otherwise, no Docker experience is required.

Step 1: Install and configure the Sail

Every new Laravel application comes with a Sail out of the box. It only needs a simple command to start it.

Run the following command to create a new Laravel application.

curl -s https://laravel.build/new-sail-application | bash

Copy the code

The command above creates the project in a new directory called new-sail-Application. Feel free to rename the name of any project you want.



Next, navigate to the project directory:

cd new-sail-application

Copy the code

To start Sail, run the following command.

./vendor/bin/sail up

Copy the code

Add Sail to an existing application

You can also use Composer to install Sail into an existing application if your local development environment is set up to use it.

composer require laravel/sail --dev

Copy the code

Once installed, you can publish Sail’s docker-comemage. yml file in your project directory with the following command.

php artisan sail:install

Copy the code

Finally, to start Sail, run the following command.

./vendor/bin/sail up

Copy the code

Step 2: Take a close look at your application

Your development environment is now up and running. In Docker, each container can hold only one service. This means that our Sail application has three services: one for PHP, one for MySQL, and one for Redis.

These three containers are hosted and managed by Docker Compose, using the docker-comemage. yml file at the root of your project. If you open this file, you will see the Services section, which has three components: laravel.test, mysql, and redis.

Laravel.test deals with PHP components, and the other two deal with what their names imply.

Under the mySQL service (which manages your database), you’ll find the image parameter, which represents the image being instantiated by the container. Basically, the image is like a blueprint for the house, and the container is the house itself that was built. The MySQL version (8.0) to install is also specified.

Mysql: image: 'mysql:8.0' ports: - '${FORWARD_DB_PORT:-3306}:3306' environment: MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}' MYSQL_DATABASE: '${DB_DATABASE}' MYSQL_USER: '${DB_USERNAME}' MYSQL_PASSWORD: '${DB_PASSWORD}' MYSQL_ALLOW_EMPTY_PASSWORD: 'yes' volumes: - 'sailmysql:/var/lib/mysql' networks: - sail healthcheck: test: ["CMD", "mysqladmin", "ping"]Copy the code

All the images we need come from Docker Hub, the largest image library on the web. They come with documentation explaining how to use them. Check out the MySQL documentation here.

The ports parameter is used to synchronize your container port with your local port. DB_PORT:-3306 is your local port, and ${FORWARD_DB_PORT:-3306}:3306 will synchronize it with your container’s port.

In environment, your database credentials are defined. They currently point to values in the. Env file at the root of the project.

Docker uses volumes to hold (or save) data used by the container (even after the container has been destroyed). Externalizing or keeping data out of containers is important for a number of reasons.

Containers, by design, are created to be temporary. So when they crash or stop, all the data they hold is lost. When data is written to a volume and the container to which it belongs is terminated, the data still exists and can therefore be used by a new container. Persistent data can also be shared by multiple containers on different machines.

Run volumes: – ‘sailmysql:/var/lib/mysql’ to store all the data in the var/lib/mysql directory (this is where our mysql data was stored).

All the services on our container must be connected to the same network to work together. The Networks parameter specifies which internal network your container should use, in this case, sail.

Finally, the Healthcare parameter specifies the commands Docker will run to check the health of your container and make sure it works as expected.

If you take a closer look at the Laravel. test service, the first argument that comes up is build. It points to a in the. / vendor/laravel Dockerfile/sail/runtimes / 8.0. This Dockerfile was created by the Laravel team and contains the build instructions for the images we need here, so we won’t instantiate any images from the Docker Hub:

Laravel. Test: build: context:. / vendor/laravel/sail/runtimes / 8.0 dockerfile: dockerfile args: WWWGROUP: '${WWWGROUP}' image: sail-8.0/app ports: - '${APP_PORT:-80}:80' environment: WWWUSER: '${WWWUSER}' LARAVEL_SAIL: 1 volumes: - '.:/var/www/html' networks: - sail depends_on: - mysql - redis - seleniumCopy the code

The image parameter points to the image to be instantiated. In this case, the image created by Build. Ports maps our container to local ports. Environments points to the certificates used in our PHP code. Volumes were used to store PHP data we didn’t want to lose, and networks specified the internal network the service should connect to.

The Depends_on parameter specifies which services should be up and running before our application is started. Our application relies on mysql, Redis, and Selenium.

The last service, Redis, is similar to the mysql service.

redis:
    image: 'redis:alpine'
    ports:
        - '${FORWARD_REDIS_PORT:-6379}:6379'
    volumes:
        - 'sailredis:/data'
    networks:
        - sail
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]

Copy the code

First, a Redis image is instantiated. Our container is then synchronized with the local port. We use a volume to hold our Redis data, define the internal network that the service should connect to, and perform health checks to ensure that our container is running properly.

Step 3 – Try out some Sail commands

By default, to run the Sail command, the line vendor/bin/ Sail should precede the command. However, we can configure a bash alias with only one word to make our commands shorter.

Basically, we’ll replace the line vendor/bin/sail with a single word.

alias sail='bash vendor/bin/sail'

Copy the code

To run all the containers in our docker-comemage.yml file and start our application, we use the following command.

sail up

Copy the code

To start these containers in the background, we use.

sail up -d

Copy the code

Once your app is up and running, you can visit http://localhost to view it.

To stop these containers, press Control + Con your keyboard. If they’re running in the background, run.

sail down

Copy the code

When running artisan, Composer, and NPM commands, the sail alias must precede the command.

For example, don’t run

php artisan migrate

Copy the code

Do you run

sail artisan migrate

Copy the code

Instead of running

composer require laravel/sanctum

Copy the code

You have to run.

sail composer require laravel/sanctum

Copy the code

conclusion

You have reached the end of this tutorial! We have been able to create and set up a new Sail application. We went through Sail’s docker-comemage. yml file to see how the container was set up. We also learned some basic commands for running the Sail application. But there’s more to know! To learn more about Sail, check out the official documentation here.

The postLaravel and Docker: A guide to using Laravel sails first appeared on The LogRocket blog.