Laravel is one of the most popular PHP frameworks for web development. If you want to make your Laravel project easy to set up and run on any system, Docker is a great solution. In this guide, we will explain how to dockerize a Laravel project in a simple and easy way, step by step, even for beginners.

What is Docker?

Docker is a tool that helps developers package applications along with all necessary dependencies in a container. A container is like a small box that holds everything your app needs to run. With Docker, you don’t have to manually install PHP, MySQL, or any other software – it’s all included inside the container.

Why Use Docker for Laravel?

Docker makes Laravel development easier because:

  • You don’t have to install PHP, MySQL, or Nginx manually.
  • Your project will run the same way on any computer (Windows, macOS, Linux).
  • It helps avoid problems like “it works on my machine but not on yours.”
  • It’s easy to share your project with others.

Let’s see how to run a Laravel app in Docker step by step.

Step 1: Install Docker

Before we begin, make sure Docker is installed on your system. You can download it from Docker’s official website. Follow the installation guide for your operating system.

Step 2: Create a Dockerfile

A Dockerfile is a script that tells Docker how to set up your Laravel app. It contains instructions for installing PHP, Composer, and Laravel dependencies.

Steps:

  1. Inside your Laravel project, create a file named Dockerfile.
  2. Add the following code to the file:
# Use PHP 8.1 with FPM (FastCGI Process Manager)
FROM php:8.1-fpm

# Set the working directory inside the container
WORKDIR /var/www

# Install necessary extensions
RUN apt-get update && apt-get install -y \
    unzip \
    curl \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    && docker-php-ext-install pdo pdo_mysql gd

# Copy Laravel files into the container
COPY . .

# Install Composer (Dependency Manager for PHP)
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install

# Start PHP FPM when the container runs
CMD ["php-fpm"]

# Expose port 9000 for communication
EXPOSE 9000

Step 3: Create a Docker Compose File

Docker Compose allows us to run multiple services together, such as Laravel, MySQL, and Nginx.

Steps:

  1. Create a new file in your Laravel project named docker-compose.yml.
  2. Add the following code:
version: '3.8'
services:
  app:
    build: .
    container_name: laravel_app
    restart: always
    working_dir: /var/www
    volumes:
      - .:/var/www
    networks:
      - laravel
  
  db:
    image: mysql:5.7
    container_name: mysql_db
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: laravel
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    networks:
      - laravel

  nginx:
    image: nginx:latest
    container_name: nginx_server
    restart: always
    ports:
      - "8000:80"
    volumes:
      - .:/var/www
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - laravel

networks:
  laravel:
    driver: bridge

Step 4: Configure Nginx for Laravel

To make Laravel work with Nginx, we need to create an Nginx configuration file.

Steps:

  1. Inside your Laravel project, create a folder named nginx.
  2. Inside the nginx folder, create a file named default.conf.
  3. Add the following content:
server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    root /var/www/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Step 5: Start the Laravel Docker Container

Now, let’s run our Laravel project using Docker.

Steps:

  1. Open a terminal in your Laravel project folder.
  2. Run the command below:
docker-compose up -d

This will start all containers in the background. Your Laravel app will be available at http://localhost:8000.

Step 6: Run Laravel Migrations

Once your container is running, execute the following command to create database tables:

docker exec -it laravel_app php artisan migrate

Step 7: Access Laravel Application

Now, open your browser and visit:

http://localhost:8000

If everything is set up correctly, your Laravel project should be running inside Docker!

Troubleshooting

  • Error: Port already in use → Try stopping other services using ports 80 or 3306 (sudo systemctl stop apache2 for Apache users).
  • File permission issues → Run sudo chmod -R 777 storage/ bootstrap/cache/ inside the Laravel project.
  • Database connection errors → Ensure DB_HOST=db in .env file matches the MySQL container name in docker-compose.yml.

Conclusion

That’s it! You now know how to run a Laravel app in Docker step by step. This method makes it easier to set up and share your Laravel project with a team.

Dockerizing a Laravel project ensures that your application runs the same way on all machines. If you are a junior developer, don’t worry – just follow the steps and you’ll get it working. Keep practicing, and happy coding!

Categorized in: