မင်္ဂလာပါ။
ကျွန်တော်ကတော့ Spiceworks Myanmar မှာ Backend Developer အနေနဲ့ တာဝန်ယူလုပ်ကိုင်နေတဲ့ သုတယာမိုး ဖြစ်ပါတယ်။ Docker မိတ်ဆက် အကြောင်းကိုတော့ မျှဝေပေးထားပြီးပြီမို့လို့ ဝင်ရောက်ဖတ်ရှုနိုင်ပါတယ်။ အခု ဘလော့ဂ်မှာတော့ Docker ကိုအသုံးပြုပြီး Laravel project အတွက် လိုအပ်တဲ့ Nginx web server, PHP application server နှင့် MySQL database စသော services တွေကိုအသုံးပြုပြီး development environment တည်ဆောက်ပုံကို ဝေမျှပေးသွားချင်ပါတယ်။
Project Structure
Laravel project structure ကတော့ အောက်ပါအတိုင်းဖြစ်ပါတယ်။
my-laravel-project/ ├── docker/ │ ├── nginx/ │ │ └── Dockerfile │ │ └── default.conf │ ├── php/ │ │ └── Dockerfile ├── docker-compose.yml ├── .env └── (other Laravel project files)
Dockerfile for PHP
PHP အတွက် Dockerfile ကို php/nginx/ directory မှာ create လုပ်ပေးပါ။
#choose php:8.3-fpm as the base image
FROM php:8.3-fpm
# Set working directory
WORKDIR /var/www
# Copy existing application directory contents
COPY . /var/www
# Install dependencies and required packages for the PHP application
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
libonig-dev \
libzip-dev \
libgd-dev
# Clear cache and remove unnecessary packages and files to reduce the image size
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions and configure GD library for image processing in PHP applications
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-external-gd
RUN docker-php-ext-install gd
# Install Composer and run the composer install command to install the application dependencies and generate the autoloader file
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install --no-scripts && composer dump-autoload -o
# Expose port 9000 and start php-fpm server to listen on port 9000 when the container starts running
EXPOSE 9000
CMD ["php-fpm"]
Dockerfile for Nginx
Nginx အတွက် Dockerfile ကို docker/nginx/ directory မှာ create လုပ်ပေးပါ။
# choose nginx:stable-perl as the base image FROM nginx:stable-perl # copy the default nginx configuration file to the container COPY ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf # expose port 80 and start the nginx server to listen on port 80 when the container starts running CMD ["nginx", "-g", "daemon off;"]
docker/nginx/ directory မှာ default.conf နာမည်ဖြင့် Nginx Configuration file တစ်ခု create လုပ်ပေးပါ။
# This is the default configuration file for Nginx
server {
listen 80;
listen [::]:80;
server_name localhost;
root /var/www/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Docker Compose Configuration
Project ရဲ့ root directory မှာ docker-compose.yml file create လုပ်ပေးပါ။
services:
web:
container_name: docker-laravel-nginx
build:
context: .
dockerfile: ./docker/nginx/Dockerfile
image: nginx:perl
depends_on:
- app
ports:
- "8080:80"
volumes:
- ./:/var/www
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
networks:
- docker-laravel-network
app:
container_name: docker-laravel-app
image: php:8.3-fpm
build:
context: .
dockerfile: ./docker/php/Dockerfile
ports:
- "9000:9000"
volumes:
- ./:/var/www
networks:
- docker-laravel-network
db:
container_name: docker-laravel-db
image: mysql:oracle
ports:
- "3308:3306"
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_USER: ${DB_USERNAME}
MYSQL_PASSWORD: ${DB_PASSWORD}
volumes:
- dbdata:/var/lib/mysql
networks:
- docker-laravel-network
artisan:
container_name: docker-laravel-artisan
image: php:8.3-fpm
build:
context: .
dockerfile: ./docker/php/Dockerfile
volumes:
- ./:/var/www
entrypoint: ["php", "/var/www/artisan"]
networks:
- docker-laravel-network
volumes:
dbdata:
driver: local
networks:
docker-laravel-network:
driver: bridge
Environment Variables
ထို့နောက် .env file မှာ DB_HOST ဆိုတဲ့ environment variable မှာ database service ရဲ့ container name ကို အစားထိုးပေးပါ။
#docker-compose.yml db: container_name: docker-laravel-db #.env DB_HOST=docker-laravel-db
Build and Run Your Containers
အခုဆိုရင် docker setup လုပ်ပြီးပြီဖြစ်တဲ့အတွက် အောက်ပါ command ကို အသုံးပြုကာ container build လုပ်ပါမယ်။
docker-compose up -d --build
ထို command ကတော့ composer-compose.yml မှာ ကြေညာထားတဲ့ services တွေကို build လုပ်ပြီး အလုပ်လုပ်စေမှာဖြစ်ပါတယ်။ Containers တွေကို အလုပ်လုပ်ပြီဆိုရင်တော့ laravel project ကို http://localhost:8080 မှာ တွေ့မြင်နိုင်မှာဖြစ်ပါတယ်။ အဆုံးထိဖတ်ရှုပေးတဲ့အတွက် ကျေးဇူးတင်ပါတယ် ခင်ဗျ။
