How to Customize Laravel Sail with Docker Compose Files?

Aaron Reddix
CodeX
Published in
5 min readJun 18, 2024

Laravel Sail is a game-changer for Laravel development. It provides a pre-configured Docker environment with all the necessary services running out of the box. This eliminates the need for manual setup and ensures consistent development environments across your team. But what if your project has specific requirements? Laravel Sail shines again with its ability to be customized using Docker Compose files.

Docker Compose is the magic behind Sail. It manages the configuration and orchestration of multiple Docker containers. The docker-compose.yml file is the heart of Docker Compose, defining the services (containers) needed for your application. By making informed modifications to this file, you can tailor your Sail environment to perfectly suit your project’s needs.

In this article, we’ll delve into the world of customizing Laravel Sail with Docker Compose. We’ll explore how to adjust services, volumes, ports, and environment variables to create a development experience that’s efficient and perfectly aligned with your project’s specific requirements.

Understanding the docker-compose.yml File

The docker-compose.yml file acts as the blueprint for your Laravel Sail environment. It instructs Docker Compose on how to set up and manage the various services (containers) that make your application run smoothly. Opening this file in a text editor reveals its structured format, typically containing several key sections:

1. Version

This section specifies the Docker Compose file format version. While newer versions offer more features, Sail uses a well-supported and compatible version for broader accessibility.

2. Services

This is the heart of the configuration. It defines each service (container) required for your application. Common Sail services include:

  • laravel.test: The container running your Laravel application.
  • mysql: The database container, usually MySQL.
  • phpmyadmin: A container providing a graphical user interface (GUI) for managing your database.

Each service definition includes details like the base image (e.g., php:8.1), ports to expose (e.g., 80:80 for web traffic), volumes to mount (project code and data), and environment variables for configuration.

Also Read: Testing Laravel Applications with Dusk

3. Volumes

This section defines named volumes that persist data across container restarts. For instance, your application code and database files might be mounted as volumes, ensuring they’re preserved even when containers are stopped and recreated.

4. Networks

This section defines custom networks for container communication. By default, Sail uses a bridge network for communication between services within the environment.

5. Environment

This section allows you to define environment variables that can be accessed within your application containers. These variables can be used to configure various aspects of your application, such as database credentials or API keys.

Understanding the purpose and structure of each section in the docker-compose.yml file is crucial before diving into customization. By familiarizing yourself with these sections, you’ll be well-equipped to make informed changes and tailor your Sail environment to your project’s specific needs

Customizing Services

As we saw, the services section of the docker-compose.yml file defines each container running in your Sail environment. This is where the magic of customization happens! Let’s explore some ways to personalize services for your project:

1. Base Image:

The base image specifies the operating system and software pre-installed within a container. By default, Sail uses a specific PHP version image (e.g., php:8.1) for the laravel.test service. However, if your project requires a different PHP version (e.g., php:8.2 for a newer feature), you can simply modify the base image definition in the docker-compose.yml file. Here’s an example:

2. Ports:

By default, Sail maps specific container ports to your host machine ports. This allows you to access your application through a convenient URL (usually http://localhost:80). But what if you’re working on multiple projects simultaneously? You can customize ports for each project’s laravel.test service to avoid conflicts. Here’s how:

This configuration exposes the container’s port 80 (web traffic) to your host machine’s port 8080. Now, you can access your application at http://localhost:8080.

3. Volumes:

Volumes are directories shared between your host machine and the container. This allows you to persist data like your application code and database files even when containers are recreated. Sail pre-configures some volumes, but you can add additional volumes for specific needs.

For example, imagine you have a custom configuration file outside your project directory. You can mount this file as a volume in the laravel.test service:

This mounts the my-custom-config.php file from your host machine to the /var/www/html directory within the container, making it accessible to your application.

4. Environment Variables:

Environment variables provide a way to configure your application without modifying the code itself. Sail defines some default environment variables, but you can add custom ones specific to your project. Here’s an example:

These environment variables can be accessed within your Laravel application using the env() helper function.

Advanced Sail Customization

While the previous sections focused on core service customizations, Laravel Sail offers even more flexibility for experienced developers. Here’s a glimpse into some advanced options:

1. Networks:

By default, Sail uses a bridge network for communication between services. However, for complex applications requiring more granular control, you can define custom networks in the docker-compose.yml file. These networks allow you to isolate services and control communication patterns.

Important Note: Custom network configuration involves a deeper understanding of Docker networking concepts and is recommended for developers comfortable with advanced Docker Compose techniques.

2. Adding Custom Services:

Sail provides pre-configured services like MySQL, but what if you need additional services like Memcached for caching or Mailhog for testing emails? The beauty of Docker Compose lies in its ability to integrate various services. You can define custom services in the docker-compose.yml file, specifying the desired image and configuration options.

For instance, to add a Memcached service:

Remember: Adding custom services requires understanding their purpose and configuration options. Refer to the official documentation of the chosen service image for specific guidance.

3. Using Docker Secrets:

Security is paramount. Storing sensitive information like database credentials directly in the docker-compose.yml file is not recommended. Docker Compose offers a secure solution called “secrets.” These secrets can be defined outside the docker-compose.yml file and referenced within the services using environment variables.

Important Note: Using Docker secrets requires additional configuration and potentially managing separate files for sensitive information. Make sure you understand Docker security best practices before implementing this option.

These advanced customization options unlock a whole new level of flexibility for your Laravel Sail environment. However, it’s crucial to approach them with caution and a solid understanding of Docker Compose concepts. For beginners, it’s wise to master the core service customizations before venturing into advanced configurations.

Conclusion

Laravel Sail empowers you to streamline your Laravel development workflow with a pre-configured Docker environment. But the true magic lies in its ability to be customized. By leveraging Docker Compose files, you can tailor your Sail setup to perfectly suit your project’s requirements.

This article explored various customization options, from adjusting service base images and ports to mounting volumes and defining custom environment variables. We even ventured into advanced territory with custom networks, adding services, and utilizing Docker secrets for enhanced security.

Remember, customization is a powerful tool, but wielding it effectively requires understanding the docker-compose.yml file and Docker Compose concepts. Start small, experiment with basic customizations, and gradually explore advanced options as your confidence grows.

Ready to unleash the full potential of Laravel Sail? Refer to the official Sail documentation for in-depth details and explore the vast world of Docker Compose resources online. Feel free to share your experiences and ask questions in the comments section below. Happy customizing!

--

--

Aaron Reddix
CodeX
Writer for

Web dev here, with a serious case of Flutter fever! Currently, exploring the exciting world of cross-platform development with Flutter.