Laravel, a popular PHP framework, provides a great ecosystem for web application development. Docker, on the other hand, is a tool designed to make it easier to create, deploy, and run applications using containers. By combining these technologies, developers can enjoy the benefits of a consistent development environment and simplified deployment process.
In this article, we will walk you through setting up a Laravel application with Docker using Docker-compose.
Prerequisites
- Docker installed on your system.
- Docker-compose installed on your system.
- Basic understanding of Docker concepts such as images, containers, and Dockerfile.
Step 1: Setting up Laravel
Start by creating a new Laravel project or navigating to your existing project.
composer create-project --prefer-dist laravel/laravel my-laravel-app
cd my-laravel-app
Step 2: Create a Dockerfile
The Dockerfile contains instructions to Docker for building your app’s environment.
Let’s create a file named Dockerfile in your project root directory and add the following:
FROM php:8.1-fpm
# Install system dependencies
RUN apt-get update && apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev zip git curl unzip
# Install PHP extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd pdo pdo_mysql
# Get Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www
# Copy existing application directory
COPY . .
# Install Laravel dependencies
RUN composer install
CMD ["php-fpm"]
EXPOSE 9000
Step 3: Docker-compose Configuration
Docker-compose helps in defining and running multi-container Docker applications. Create a docker-compose.yml file in the project root:
version: '3'
services:
# PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: my-laravel-app
container_name: app
restart: unless-stopped
tty: true
working_dir: /var/www
volumes:
- ./:/var/www
- ./storage:/var/www/storage
networks:
- app-network
# MySQL Service
database:
image: mysql:5.7.22
container_name: database
restart: unless-stopped
tty: true
ports:
- "4306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: root_password
MYSQL_PASSWORD: password
MYSQL_USER: user
networks:
- app-network
# Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "8000:80"
volumes:
- ./:/var/www
- ./nginx:/etc/nginx/conf.d
networks:
- app-network
# Networks Configuration
networks:
app-network:
driver: bridge
Step 4: Nginx Configuration
The Docker-compose uses Nginx web server for running the application.
Create a new folder nginx in your project root. Inside, create a file named default.conf with the following content:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Step 5: Start the Docker Containers
With everything set up, start your containers:
docker-compose up -d
Once the containers are up, you can visit http://localhost:8000 in your browser to view your Laravel application.
Step 6: Cleanup
To stop the services, you can use:
docker-compose down
Conclusion
By integrating Laravel with Docker and Docker-compose, developers can ensure a consistent environment throughout development, testing, and production stages. This not only eases the deployment process but also eradicates the common “it works on my machine” problem. As you become more accustomed to Docker, you can further enhance and customize this setup to suit your specific needs.