Dockerizing a Laravel project involves creating Docker containers for the Laravel application, Apache web server, MySQL database, MailHog for email testing, and Minio for object storage. Below is a step-by-step guide to dockerize a Laravel project using these services.
- Laravel Project Setup:
Assuming you already have a Laravel project, make sure it's configured correctly.
- Create Dockerfile for Laravel :
Create a file named Dockerfile in the root of your Laravel project:
# Use the official PHP image as a base image
FROM php:7.4-apache
# Set working directory
WORKDIR /var/www/html
# Install dependencies
RUN apt-get update && \
apt-get install -y \
libzip-dev \
unzip \
libonig-dev \
libxml2-dev && \
docker-php-ext-install pdo_mysql zip mbstring exif pcntl bcmath && \
a2enmod rewrite && \
service apache2 restart
# Copy the Laravel application files
COPY . /var/www/html/
# Set permissions
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
# Expose port 80
EXPOSE 80
# Start Apache
CMD ["apache2-foreground"]
- Create Docker Compose File:
Create a file named docker-compose.yml in the root of your project:
version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile
image: laravel-app
ports:
- "8080:80"
volumes:
- .:/var/www/html
depends_on:
- db
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: laravel
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
ports:
- "3306:3306"
volumes:
- db-data:/var/lib/mysql
mailhog:
image: mailhog/mailhog
ports:
- "8025:8025"
minio:
image: minio/minio
command: server /data
ports:
- "9000:9000"
environment:
MINIO_ACCESS_KEY: minioadmin
MINIO_SECRET_KEY: minioadmin
volumes:
- minio-data:/data
volumes:
db-data:
minio-data:
- Configure Laravel .env File:
Update your Laravel .env file with the database, mail, and storage configurations:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=secret
MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
FILESYSTEM_DRIVER=minio
MINIO_ENDPOINT=http://minio:9000
MINIO_KEY=minioadmin
MINIO_SECRET=minioadmin
MINIO_BUCKET=your_bucket_name
- Build and Run:
In your terminal, navigate to the project root directory and run the following commands:
docker-compose up -d --build
- Laravel Setup:
Run the following commands to set up Laravel within the Docker container:
docker-compose exec app composer install
docker-compose exec app php artisan key:generate
docker-compose exec app php artisan migrate
docker-compose exec app php artisan storage:link
- Access the Application:
Open your web browser and go to http://localhost:8080 to access your Laravel application.
MailHog can be accessed at http://localhost:8025
Minio at http://localhost:9000
That's it! You now have a Dockerized Laravel project with Apache, MySQL, MailHog, and Minio.
Adjust configurations as needed for your specific project requirements.