How To Use Docker In Laravel For Local Development
Docker has become an invaluable tool for developers looking to streamline their local development environments, and when combined with Laravel, a popular PHP framework, it can significantly enhance the ease and consistency of development workflows. Docker allows you to create isolated containers that encapsulate your application’s dependencies and environment, making it easier to set up, share, and replicate development environments across different machines. In this guide, we will walk you through the essential steps to use Docker in Laravel for local development.
In this steps we will be using :
- MySQL
- Nginx
- NPM
- Composer
Step 1 : Create ‘src’ Folder Structure
< YourAppFolder >
< src >
In this src folder your Laravel project will stored.
Step 2 : Create docker-compose.yaml file.
< YourAppFolder >
< src >
docker-compose.yaml
Step 3 : Configure NGINX image in docker-compose.yaml
version: '3.8'
networks:
laravel:
name: laravel
services:
nginx:
build:
context: .
dockerfile: nginx.dockerfile
container_name: nginx
depends_on:
- mysql
- php
ports:
- "3000:80"
- "8443:443"
volumes:
- ./src:/var/www/html
networks:
- laravel
In this section we will set
- Network name as “laravel”
- NGINX container as “nginx”
- Expose port 3000 and 8443
We set our docker network as “Laravel” and we set depends_on for mysql and php which is will we set in the next step. The reason we set this depends_on mysql and php . we want our nginx compose after mysql and php container.
Create our NGINX dockerfile
Create docker file for nginx
FROM nginx:stable-alpine
ADD ./nginx/default.conf /etc/nginx/conf.d/default.conf
RUN mkdir -p /var/www/html
RUN mkdir -p /etc/nginx/certs/mkcert
ADD ./nginx/certs/ /etc/nginx/certs/mkcert
Step 4 : Configure your MySQL container
mysql:
image: mysql:8.1.0
container_name: mysql
ports:
- "4306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE : laraveldb
MYSQL_USER : laravel
MYSQL_PASSWORD : password
MYSQL_ROOT_PASSWORD: password
networks:
- laravel
in this section will be using port 4306 for your mysql client connection
Step 5: Configure composer container
composer:
image: composer:latest
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
networks:
- laravel
in this section will configure composer image which is will much easier for us to use composer for our Laravel application.
Step 6: Configure artisan image
artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: artisan
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint:
- php
- artisan
networks:
- laravel
in this section we will set image for our artisan container
Step 7: Configure our NPM container
npm:
image: node:latest
container_name: npm
volumes:
- ./src:/var/www/html
- ./nginx/certs:/etc/vite/certs
working_dir: /var/www/html
ports:
- "5173:5173"
- "3001:3001"
entrypoint:
- npm
networks:
- laravel
in this section we will set image for our NPM container will be use for our npm command and expose port for 5173 for our Vite.
Step 8 : Configure our PHP container
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ./src:/var/www/html
networks:
- laravel
Create our PHP dockerfile
FROM php:8.1-fpm-alpine
RUN mkdir -p /var/www/html
RUN apk --no-cache add shadow && usermod -u 1000 www-data
RUN docker-php-ext-install pdo pdo_mysql
Step 8 : Set NGINX conf file
Create a folder name NGINX at your root document as sub folder certs to easy manage your SSL certs.
< YourAppFolder >
< src >
< nginx >
< certs >
Create a default.conf file in the nginx folder
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
index index.php index.html;
server_name localhost;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
ssl_certificate /etc/nginx/certs/mkcert/laraveldocker.test.pem;
ssl_certificate_key /etc/nginx/certs/mkcert/laraveldocker.test-key.pem;
}
server {
listen 80;
index index.php index.html;
server_name _;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Step 9 : Set MySQL data folder
Create a folder name mysql to hold all your database data.
< YourAppFolder >
< src >
< nginx >
< certs >
< mysql >
Step 10 : Run your docker environment
docker compose up -d
Thank you!~