How to Dockerize Laravel and MySQL
Febaruary 23, 2025
Hi, maybe you're here wondering what Docker is or how to dockerize a Laravel application. Before we start, let's briefly go over why containerizing your application is a game-changer! ๐
Why Use Docker for Laravel?
Containerizing your Laravel app ensures that your application runs in a consistent environment, regardless of where it's deployed. This eliminates the โit works on my machineโ problem. With Docker, you can package Laravel, MySQL, and other dependencies into isolated containers, making local development and deployment smoother.
Step 1: Install Docker
First, ensure that Docker is installed on your machine. You can download it from Docker's official website
Once you installed, open your terminal and type the command "docker -v". you should see something like:
docker -v
Docker version 27.4.0, build bde2b89
Step 2: Create a Dockerfile for Laravel
In the root of your Laravel project, create a Dockerfile:
# Use the official PHP image with required extensions
FROM php:8.1-fpm
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
libpng-dev \
libjpeg-dev \
zip \
unzip \
git \
curl \
&& docker-php-ext-install pdo pdo_mysql gd
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Copy application files
COPY . .
# Set correct permissions
RUN chown -R www-data:www-data /var/www
# Expose port
EXPOSE 9000
CMD ["php-fpm"]
Step 3: Create a docker-compose.yml
Next, create a docker-compose.yml file in your project root to define your Laravel and MySQL services:
version: '3.8'
services:
app:
build: .
container_name: laravel_app
restart: unless-stopped
working_dir: /var/www
volumes:
- .:/var/www
depends_on:
- db
networks:
- laravel
db:
image: mysql:8
container_name: laravel_db
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
ports:
- "3306:3306"
networks:
- laravel
networks:
laravel:
driver: bridge
This sets up two services: app, which runs the Laravel PHP container, and db, which runs MySQL with preconfigured credentials.
Step 4: Create a .env Configuration
Update your Laravel .env file to use the database container:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret
Step 5: Start the Containers
Run the following command to start your Laravel and MySQL containers:
docker-compose up -d
To check running containers:
docker ps
If everything is set up correctly, your Laravel app should now be running inside Docker! ๐
Step 6: Running Migrations and Serving Laravel
Once your containers are running, enter the Laravel container to run migrations:
docker exec -it laravel_app php artisan migrate
To serve Laravel, you can use:
docker exec -it laravel_app php artisan serve --host=0.0.0.0 --port=8000
Now, your Laravel app should be accessible at http://localhost:8000.
Conclusion
You've successfully dockerized your Laravel application with MySQL! This setup helps keep your development environment consistent and makes it easy to deploy your application anywhere Docker is supported.
If you need to stop the containers, run:
docker-compose down
That's it! ๐ Let me know if you have any questions in the comments below. ๐