Laravel Docker #2 – Apache, PHP & MySQL Setup in Minutes
When you are working on Laravel projects, sometimes you may want to use Docker for local development. Docker makes it easy to run PHP, Apache, and MySQL without installing them directly on your system. This setup will help you to get started quickly with Laravel and Docker.
In this tutorial, I will show you step by step how you can set up Laravel with Apache, PHP, and MySQL inside Docker. We will use Dockerfile and docker-compose to configure everything. You just need to follow each step carefully.
Step 1: Create Laravel Project
To start with, we need a fresh Laravel project. Run the below command to create it:
laravel new my-app
Step 2: Create Dockerfile
Now create a Dockerfile in the root directory. This file will define how PHP and Composer will be installed.
Dockerfile
FROM php:8.3-fpm
WORKDIR /var/www/html
RUN apt-get update & apt-get install -y \
libzip-dev \
unzip \
& docker-php-ext-install zip pdo_mysql
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY . /var/www/html
# Install PHP dependencies
RUN composer install
RUN chown -R www-data:www-data /var/www/html
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache /var/www/html/vendor
RUN chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache /var/www/html/vendor
EXPOSE 80
Step 3: Create Apache Config File
Next, we will add an Apache config file. This will tell Apache how to serve the Laravel project.
.docker/apache/default.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/public
<Directory /var/www/html/public>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Step 4: Create docker-compose.yml
Finally, we will create a docker-compose.yml file to define our services like web and database.
docker-compose.yml
services:
web:
build:
context: .
dockerfile: Dockerfile
image: my-laravel-app
volumes:
- .:/var/www/html
- /var/www/html/storage
- /var/www/html/bootstrap/cache
- ./.docker/apache/default.conf:/etc/apache2/sites-enabled/000-default.conf
working_dir: /var/www/html
user: "www-data"
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
ports:
- "3307:3306"
volumes:
- dbdata:/var/lib/mysql
volumes:
dbdata:
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8080
Now you can use.