Laravel is a popular PHP web application framework that is widely used by developers to build high-quality web applications. Docker is a powerful containerization platform that simplifies the process of building, deploying, and managing containers. Docker Compose is a tool that simplifies the process of defining and running multi-container Docker applications. In this blog post, we will show you how to use Docker and Docker Compose to set up a Laravel application.
Before we get started, make sure you have the following prerequisites installed on your system:
- Docker (version 17.12.0 or higher)
- Docker Compose (version 1.21.0 or higher)
Step 1: Create a Laravel Project
The first step is to create a new Laravel project. Open your terminal and run the following command:
composer create-project --prefer-dist laravel/laravel my-app
This will create a new Laravel project named
my-app in the current directory.
Step 2: Create a Dockerfile
The next step is to create a
Dockerfile in the root directory of your Laravel project. The
Dockerfile is used to build a Docker image that contains all the dependencies required to run your Laravel application. Here is an example
Dockerfile that you can use:
# Use an official PHP runtime as a parent image FROM php:7.4-apache # Set the working directory to /var/www/html WORKDIR /var/www/html # Copy the current directory contents into the container at /var/www/html COPY . /var/www/html # Install any needed packages RUN apt-get update && \ apt-get install -y git zip unzip && \ docker-php-ext-install pdo_mysql && \ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # Install composer dependencies RUN composer install --no-dev --no-interaction --no-progress --optimize-autoloader # Set the correct permissions for Laravel RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache # Expose port 80 EXPOSE 80 # Start Apache CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Dockerfile starts with a base image of PHP 7.4 with Apache installed. It then sets the working directory to
/var/www/html and copies the contents of the current directory into the container. Next, it installs any necessary packages, such as Git, Zip, and Unzip, as well as the PDO MySQL extension for PHP.
After that, it installs Composer and runs
composer install to install all the dependencies required by your Laravel application. Finally, it sets the correct permissions for Laravel and exposes port 80 before starting Apache.
Step 3: Create a Docker Compose file
The next step is to create a
docker-compose.yml file in the root directory of your Laravel project. This file is used to define the services that make up your application. Here is an example
docker-compose.yml file that you can use:
version: "3" services: app: build: . ports: - "8000:80" depends_on: - db volumes: - .:/var/www/html db: image: mysql:5.7 environment: MYSQL_DATABASE: laravel MYSQL_ROOT_PASSWORD: secret
docker-compose.yml file defines two services:
app service is built using the
Dockerfile in the current directory (
.) and exposes port
80. It depends on the
db service and mounts the current directory (
.) as a volume at
/var/www/html in the container. This allows changes made on the host machine to be reflected in the container.
db service uses the official MySQL 5.7 image and sets the necessary environment variables for the Laravel application.
Step 4: Start the Application
The final step is to start the application using Docker Compose. Open your terminal and navigate to the root directory of your Laravel project. Run the following command to start the application:
docker-compose up -d
This command will start the application in detached mode, which means that the containers will run in the background. You can access your Laravel application by opening a web browser and navigating to
In this blog post, we showed you how to use Docker and Docker Compose to set up a Laravel application. We created a
Dockerfile to build a Docker image containing all the dependencies required to run Laravel and a
docker-compose.yml file to define the services that make up the application. Finally, we started the application using Docker Compose. With this setup, you can easily develop, test, and deploy your Laravel application in a containerized environment.
- Laravel Best Practices: Limiting env Calls to Config Files for Better Performance and Stability
- php artisan make: model, add additional flags to include extra features
- Laravel Request Lifecycle
Follow Laravel.Tips on Instagram
If you found this post informative, we encourage you to share it with your colleagues. We value your feedback and would love to hear your thoughts on our blog and social media posts across platforms such as Instagram, Facebook, LinkedIn, and Twitter.