Add Laravel Sail into your current Laravel project

Yesterday Taylor Otwell released a brand new package called Sail which will be available by default in every new Laravel project. However, if you already have your own Laravel project you might want to include it.


What is Laravel Sail?

Laravel Sail is a light-weight command-line interface for interacting with Laravel’s default Docker development environment. Sail provides a great starting point for building a Laravel application using PHP, MySQL, and Redis without requiring prior Docker experience.

At its heart, Sail is the docker-compose.yml file and the sail script that is stored at the root of your project. The sail script provides a CLI with convenient methods for interacting with the Docker containers defined by the docker-compose.yml file.

Laravel Sail is supported on macOS, Linux, and Windows (via WSL2).

Update on adding Sail

There is an even easier way to add Sail to an existing Laravel project. Thanks to Paul Redmond on Twitter for replying to me.

Require the Sail package with composer require laravel/sail --dev then run the install command php artisan sail:install and finally publish the env config php artisan sail:publish. And now you have Sail set up with your current project.


How to add Laravel Sail into your current project

Firstly, in the root of your project you will want to add a docker-compose.yml file.

# For more information: '3'services:    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            # - selenium    # selenium:    #     image: 'selenium/standalone-chrome'    #     volumes:    #         - '/dev/shm:/dev/shm'    #     networks:    #         - sail    #     depends_on:    #         - laravel.test    mysql:        image: 'mysql:8.0'        ports:            - '${DB_PORT}: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    redis:        image: 'redis:alpine'        ports:            - '${REDIS_PORT}:6379'        volumes:            - 'sailredis:/data'        networks:            - sail    # memcached:    #     image: 'memcached:alpine'    #     ports:    #         - '11211:11211'    #     networks:    #         - sail    mailhog:        image: 'mailhog/mailhog:latest'        ports:            - 1025:1025            - 8025:8025        networks:            - sailnetworks:    sail:        driver: bridgevolumes:    sailmysql:        driver: local    sailredis:        driver: local

Update your .env file

There’s a couple of changes you need to make to your .env file which can be done through a find and replace.


Run Composer

Next, run composer require laravel/sail --dev to install the Sail package. Once installed you will be able to run vendor/bin/sail up to build and run the Sail containers. If you would like to learn more about Sail, then you can read the Laravel documentation here.

Leave a Reply