Setup Laravel Project with Docker Nginx & phpMyadmin
Docker provides a tool called Docker Compose for defining the setup process of a docker container. Docker Compose allows developers to define the infrastructure of their application, services, volumes, networks, and any dependencies in one file called the docker-compose file. It can manage multiple Docker containers through its commands such as docker container create, docker container run, etc.
composer create-project laravel/laravel example-app
cd example-app
Creating the Dockerfile & other setup
Next create a .docker folder in root directory
Next create app folder in .docker directory & create new two file Dockerfile , php-fpm.ini
in example-app/.docker/app/Dockerfile
This dockerfile will set the base image and specify the necessary commands and instructions to build the laravel application image. Add the following code to the file:
FROM php:8.0.2-fpm
ARG user
ARG uid
# Install Dependencies
RUN apt-get update && apt-get install -y git curl libpng-dev libonig-dev libxml2-dev zip unzip
#install some base extensions
RUN apt-get install -y \
libzip-dev \
zip \
zlib1g-dev\
&& docker-php-ext-install zip \
&& docker-php-ext-install gd
# Clear Cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install sockets pdo_mysql intl mbstring exif pcntl bcmath gd
#Install Node Js and Npm
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get install -y nodejs
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Set working directory
WORKDIR /var/www
RUN chown -R www-data:www-data /var/www
USER $user
Config PHP
in example-app/.docker/app/php-fpm.ini
date.timezone = UTC
display_errors = Off
log_errors = On
memory_limit = -1
max_execution_time = 3000
max_input_time = 600
memory_limit = 512M
post_max_size = 256M
upload_max_filesize = 256M
[opcache]
opcache.enable = 1
opcache.enable_cli = 1
opcache.revalidate_freq = 0
opcache.validate_timestamps = 1
opcache.max_accelerated_files = 25000
opcache.memory_consumption = 192
opcache.max_wasted_percentage = 10
opcache.interned_strings_buffer = 16
opcache.fast_shutdown = 1
realpath_cache_size = 4096K
realpath_cache_ttl = 7200
[xdebug]
xdebug.remote_enable = 1
xdebug.remote_port = 9000
xdebug.remote_autostart = 0
xdebug.remote_connect_back = 0
xdebug.idekey = PHPSTORM
Setup nginx config
create nginx folder in .docker folder & create default.conf file
example-app/.docker/nginx/default.conf
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
docker-compose Laravel
Creating Docker Compose File
~/example-app/docker-compose.yml
docker-compose file will config app , nginx, phpmyadmin
version: "3.7"
services:
nginx:
image: nginx:alpine
container_name: example-app-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www/html:cached
- ./.docker/nginx:/etc/nginx/conf.d
networks:
- example-app-network
depends_on:
- app
app:
build:
args:
user: Ridoy
uid: 1000
context: ./.docker/app
dockerfile: Dockerfile
image: example-app
container_name: example-app
restart: unless-stopped
ports:
- 6001:6001
working_dir: /var/www/html
volumes:
- ./:/var/www/html:cached
- ./.docker/app/php-fpm.ini:/usr/local/etc/php/conf.d/99-app.ini
networks:
- example-app-network
depends_on:
- database
database:
image: mariadb:10.5.8
container_name: example-app-mariadb
restart: unless-stopped
ports:
- 3304:3304
volumes:
- example-app-volume:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
- MYSQL_USER=${DB_USERNAME}
- MYSQL_PASSWORD=${DB_PASSWORD}
- MYSQL_DATABASE=${DB_DATABASE}
networks:
- example-app-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: example-app-phpmyadmin
restart: unless-stopped
tty: true
depends_on:
- database
ports:
- 8088:80
environment:
PMA_HOST: database
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
UPLOAD_LIMIT: 300M
networks:
- example-app-network
networks:
example-app-network:
driver: bridge
volumes:
example-app-volume:
driver: local
Setup Environment ENV
DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=example_app
DB_USERNAME=root
DB_PASSWORD=123456
You just need to a single command to start all of the containers, create the volumes, and set up nginx,phpmyadmin and connect the networks
sudo docker-compose up --build
or
sudo docker-compose up -d --build
when docker build complete visit
visit http://127.0.0.1:8000
docker compose create a database for access phpmyadmin visit
visit http://127.0.0.1:8088
login with your DB_USERNAME & DB_PASSWORD which you set in env
composer install & npm install
for access bash use container name following command:
sudo docker exec -it example-app bash
then run
composer install
npm install
Conclusion
I hope this article help you to setup laravel with docker & nginx , phpmyadmin