Coding

Setting Up a Laravel 11 App in a Docker Container

Setting Up a Laravel 11 App in a Docker Container

Posted on Fri, Dec 27, 2024

In this blog post, I want to share how I set up a Laravel project using Docker. While I often use Herd because it's easy to set up and get an app running quickly, there are times when I need more customization or a specific configuration for a client to ensure the app runs seamlessly on their systems.

Prerequisites

Before starting, make sure Docker is installed on your system.

Step 1: Clone the Repository

To get started, clone the following repository:

$ git clone https://github.com/cloudwales/docker-laravel.git

This is a fork of vshloda/docker-laravel, which is a great starting point.

Once the repository has been cloned, navigate into the directory:

$ cd docker-laravel

Step 2: Setting Up the Laravel Project

If you prefer, you can run Docker commands directly from the root directory of the project. However, I personally cd into the src directory and run commands from there. While this approach might not be standard practice in the Docker community, I find it simplifies my workflow.

Remove Placeholder Files

First, we need to remove the placeholder empty files from the src and mysql directories:

$ rm -rf src/empty  
$ rm -rf mysql/empty  

Create a New Laravel App

Now, pull in Laravel using Composer:

$ cd src  
$ composer create-project laravel/laravel:^11.0 .

The . at the end ensures that the Laravel files are placed directly into the current directory.

Alternatively, you can use Docker to create the project:

$ docker compose run --rm composer create-project laravel/laravel .

This command accomplishes the same task but runs Composer inside the Docker container. It's already configured to use the src directory.

Step 3: Running the Application

Start the Docker containers with the following command:

$ docker compose up -d  

Your application should now be hosted at http://localhost:80.

Troubleshooting Common Errors

If you encounter an error such as:

Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:443 -> 0.0.0.0:0 listen tcp 0.0.0.0:443: bind: address already in use

This means the system port (e.g., port 80 or 443) is already in use. To resolve this, update the docker-compose.yml file:

ports:  
  - 8080:80 # Change this to an unused port, e.g., 8080:80  
  - 444:443 # Change this to another unused port, e.g., 444:443  

In this configuration:

  • The first number (e.g., 8080) is the port on your system.
  • The second number (e.g., 80) is the port inside the Docker container.

If you're running multiple Docker containers, ensure each container uses a unique system port. Make a note of these changes in your app documentation so future users know which port to access.

After updating the ports, rebuild and start the containers:

$ docker compose up -d  

You can then access your application in the browser at http://localhost:8080 (or whichever port you configured).

Wrapping Up

I hope this guide helps you set up your next Laravel project using Docker! If you run into any issues or need assistance with this setup, feel free to reach out.