0
0
Laravelframework~30 mins

Docker deployment in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
Docker Deployment for Laravel Application
📖 Scenario: You have built a Laravel web application and want to deploy it using Docker. Docker helps package your app with all its dependencies so it runs the same everywhere.Imagine you want to share your Laravel app with a friend who doesn't want to install PHP or Composer. Docker makes this easy by creating a container that has everything your app needs.
🎯 Goal: Build a Docker setup for a Laravel app that includes a Dockerfile and a docker-compose.yml file. This setup will run the Laravel app with PHP and a MySQL database inside containers.
📋 What You'll Learn
Create a Dockerfile for the Laravel app with PHP and Composer
Create a docker-compose.yml file to run Laravel and MySQL containers
Configure environment variables for database connection
Set up volume mounting for live code updates
💡 Why This Matters
🌍 Real World
Docker deployment is widely used to package and run web applications consistently across different environments, making it easier to share and deploy apps.
💼 Career
Many companies use Docker to deploy Laravel and other web apps. Knowing how to containerize apps and configure multi-service setups is a valuable skill for developers and DevOps roles.
Progress0 / 4 steps
1
Create Dockerfile for Laravel App
Create a file named Dockerfile in your Laravel project root. Write these exact lines to set up PHP 8.1 with Composer and copy your app code: FROM php:8.1-fpm RUN docker-php-ext-install pdo pdo_mysql COPY . /var/www/html WORKDIR /var/www/html RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer install
Laravel
Need a hint?

Use the official PHP 8.1 FPM image. Install the pdo_mysql extension for database support. Copy your Laravel app code into the container. Set the working directory. Install Composer and run composer install to get dependencies.

2
Create docker-compose.yml with Laravel and MySQL
Create a file named docker-compose.yml in your project root. Add a version key set to '3.8'. Define two services: app and db. For app, use the Dockerfile you created, map port 8000 to container port 9000, and mount the current directory to /var/www/html. For db, use the mysql:8.0 image, set environment variables MYSQL_ROOT_PASSWORD to secret and MYSQL_DATABASE to laravel, and expose port 3306. Your docker-compose.yml should look like this structure:
Laravel
Need a hint?

Use build: . to build the app service from your Dockerfile. Map ports so you can access Laravel on localhost:8000. Mount your code so changes update live. Use the official MySQL 8 image for the database with environment variables for root password and database name.

3
Configure Laravel .env for Docker MySQL
Open your Laravel .env file. Change the database connection settings to match the Docker MySQL service. Set DB_HOST to db, DB_PORT to 3306, DB_DATABASE to laravel, DB_USERNAME to root, and DB_PASSWORD to secret. Your changes should look like this: DB_HOST=db DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=secret
Laravel
Need a hint?

Set DB_HOST to the service name db so Laravel connects to the MySQL container. Use the same database name, username, and password as in docker-compose.yml.

4
Run Laravel with Docker Compose
Add a command to the app service in docker-compose.yml to start the PHP built-in server on port 9000 serving the Laravel public folder. Add this line under app:: command: php artisan serve --host=0.0.0.0 --port=9000 This will let you access your Laravel app at http://localhost:8000.
Laravel
Need a hint?

Use the command key in docker-compose.yml under the app service to start Laravel's built-in server listening on all interfaces at port 9000.