Running Laravel with Docker Compose on Ubuntu 22.04
Prerequisites
To follow this tutorial, you will need:
- Ubuntu 22.04 with Docker and Docker Compose installed
- Experience with Docker
- Basic knowledge of Laravel
- Familiarity with Apache and PHP
What is Docker?
Docker is an open-source platform that enables developers to package their applications into containers, making them easy to ship and deploy. Containers are lightweight and portable, allowing developers to work on their applications without worrying about compatibility issues.
Example Laravel App with Backpack
For this tutorial, we will use a simple Laravel application with the popular Backpack package, which provides a user-friendly interface for building and customizing admin panels.
// example Laravel app with Backpack
// File: composer.json
{
"require": {
"backpack/base": "^1.3.0",
"laravel/framework": "^9.0"
}
}
Laravel Sail
Laravel Sail is a Docker-powered local development environment that comes with Laravel 9.x. While it’s a great tool for development, it’s not suitable for production environments due to its general-purpose nature and lack of optimization.
Setting up Docker and Docker Compose
To get started, clone the repository and navigate to the project directory. You’ll find several files, including the docker-compose.yml file, which defines the services for our application.
// File: docker-compose.yml
version: '3'
services:
app:
build: .
environment:
- COMPOSER_HOME=app/composer
- COMPOSER_CACHE_DIR=app/composer/cache
volumes:
- ./:/app
Dockerfile for Laravel
The Dockerfile is a crucial component of our setup. It defines the build process for our Laravel application, including the installation of dependencies and configuration of the environment.
// File: Dockerfile
FROM php:8.1-fpm
# Set working directory to /app
WORKDIR /app
# Copy composer.lock and composer.json
COPY composer.lock composer.json ./
# Install any needed packages specified in composer.json
RUN apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng-dev
# Install composer dependencies
RUN composer install --no-dev --prefer-dist
# Copy the rest of the application code
COPY . .
# Configure PHP
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
# Expose port 9000 for FPM
EXPOSE 9000
# Start FPM
CMD ["php-fpm"]
Docker Compose for Laravel
The docker-compose.yml file defines the services for our application, including the app service, which runs the Laravel application, and the mysql service, which provides the database.
// File: docker-compose.yml
version: '3'
services:
app:
build: .
environment:
- COMPOSER_HOME=app/composer
- COMPOSER_CACHE_DIR=app/composer/cache
volumes:
- ./:/app
depends_on:
- mysql
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: 'your_root_password'
MYSQL_DATABASE: 'your_database'
MYSQL_USER: 'your_user'
MYSQL_PASSWORD: 'your_password'
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
Don’t Ignore the .dockerignore File
The .dockerignore file is similar to the .gitignore file, but for Docker. It specifies files and directories that should be ignored by Docker when building images or running containers.
// File: .dockerignore
.git/
vendor/
composer.lock
composer.json
Dockerfile
.dockerignore