How to Install and Set Up Laravel with Docker on macOS: A Step-by-Step Guide

How to Install and Set Up Laravel with Docker on macOS: A Step-by-Step Guide

Introduction

Laravel and Docker are two technologies that every modern web developer needs to be familiar with. Laravel is a PHP framework renowned for its elegant syntax and robust features, making it easier to build web applications. Docker is a tool that enables developers to run and manage applications in isolated containers, simplifying dependency and environment management.

In this tutorial, we'll cover how to install and set up Laravel using Docker on a macOS machine. By the end, you'll have a Laravel application running smoothly in a Docker container, giving you the perfect environment for building amazing web apps.

Pre-requisites

  • A macOS machine
  • Basic familiarity with terminal commands
  • Docker Desktop installed on your macOS

Installing Docker Desktop

Before diving into the Laravel installation, you need Docker Desktop on your macOS. Docker Desktop provides an intuitive UI for managing containers, which simplifies Docker operations. You can download Docker Desktop from its official website.

Once installed, you should see the Docker icon on your macOS menu bar, confirming that Docker is running successfully.

Create a New Laravel Project using Docker

Laravel offers a built-in solution called Laravel Sail for running your Laravel applications in Docker containers. With Sail, you can easily manage Docker straight from your terminal.

To set up a new Laravel project using Docker, open your terminal and execute the following command:

curl -s "https://laravel.build/YourProjectName" | bash
1curl -s "https://laravel.build/YourProjectName" | bash

Replace YourProjectName with your desired project name. This command will install a new Laravel project complete with Docker support, thanks to Laravel Sail.

Once the project is created, navigate to your application directory:

cd YourProjectName
1cd YourProjectName

Running Laravel Sail

To start Laravel Sail (and thus your Docker containers), execute the following command within your project directory:

./vendor/bin/sail up
1./vendor/bin/sail up

If you encounter port conflicts (e.g., the port is already in use), you'll need to modify the docker-compose.yml file in your project directory. Locate the ports section and change them to resolve conflicts. Here's an example change:

# Change the port from 8025 to 8026
ports:
    - '8026:8025'
1# Change the port from 8025 to 8026 2ports: 3 - '8026:8025'

After resolving port conflicts, re-run the ./vendor/bin/sail up command.

Verifying the Installation

Once Laravel Sail is running, open your web browser and go to http://localhost or the specified IP address. You should see your Laravel application up and running.

Making Changes and Observing Updates

Your Laravel application is now ready for development. To verify that your changes reflect in real-time, edit your code files and simply refresh your browser. For instance, you can edit the welcome view in Laravel and immediately see your changes by reloading your web browser.

Conclusion

Congratulations, you have successfully installed and set up Laravel with Docker on your macOS machine. This setup provides an excellent foundation for developing sophisticated web applications using the Laravel framework. With Docker handling the environment, you can focus more on coding and less on system configurations.

If you have any questions or run into issues, feel free to leave a comment below. Happy coding, and don't forget to like and subscribe for more tutorials on web development, programming, and technology!