ทดลองใช้งาน Docker พื้นฐาน #3

CrossKnight
2 min readFeb 18, 2020

--

หลังจากทดลองใช้งาน Docker แบบเบื้องต้นมาสองบทความแล้ว ครั้งนี้มาลองใช้งานแบบจริงจังขึ้นในการทำ Web service บน Docker

https://www.digitalocean.com/

โดยในบทความนี้ได้เลือกใช้ LEMP Stack ซึ่งประกอบด้วย
Linux, (E)nginx, MariaDB, PHP

แต่เดี๋ยวก่อน ในที่นี้จะเห็นว่าจะมี Service หลักๆอยู่ 3 ตัว หากท่านกำลังจะทำให้ Container 1 ตัว มีทุก Service อยู่ในนั้นคงไม่ใช่เรื่องดีหรือจุดประสงค์ของ Docker เราควรแยกแต่ละ Service ออกจากกัน

โดยระบบจะมี Folder structure ดังนี

Docker-compose

version: "3"

services:
php:
container_name: lemp_php
build: php/
restart: unless-stopped
volumes:
- ./html/:/var/www/html
expose:
- "9000"
depends_on:
- db

nginx:
container_name: lemp_nginx
image: nginx:stable-alpine
restart: unless-stopped
volumes:
- ./html/:/var/www/html
- ./nginx/conf/nginx.conf:/etc/nginx/conf/nginx.conf:ro
- ./nginx/conf.d:/etc/nginx/conf.d:ro
ports:
- "80:80"

db:
container_name: lemp_mariadb
image: mariadb:latest
restart: unless-stopped
volumes:
- ./mariadb/initdb/:/docker-entrypoint-initdb.d
- ./mariadb/data/:/var/lib/mysql/
environment:
- MYSQL_ROOT_PASSWORD=devops101
- MYSQL_DATABASE=devops_db
- MYSQL_USER=devops
- MYSQL_PASSWORD=devops101

networks:
default:
external:
name: web_network

html/index.php

<?php
$servername = "db";
$username = "devops";
$password = "devops101";

$dbhandle = mysqli_connect($servername, $username, $password);
$selected = mysqli_select_db($dbhandle, "titanic");

echo "Connected database server<br>";
echo "Selected database";
?>

nginx/conf/nginx.conf

worker_processes 1;
daemon off;
events {
worker_connections 1024;
}
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
http {
include /etc/nginx/conf/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# tells the server to use on-the-fly gzip compression.
include /etc/nginx/conf.d/*.conf;
}

nginx/conf.d/default.conf

server {
charset utf-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6

root /var/www/html;
index index.php;

location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}

# uncomment to avoid processing of calls to non-existing static files by Yii

#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $uri =404;
#}

#error_page 404 /404.html;

location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php:9000;
try_files $uri =404;
}

location ~ /\.(ht|svn|git) {
deny all;
}
}

php/Dockerfile

FROM php:7.4-fpm-alpine

RUN docker-php-ext-install mysqli

เนื่องจากมีการใช้งาน Database ด้วย จึงเตรียมฐานข้อมูลไว้ทดลองใช้งานเบื้องต้นว่าสามารถ Access ได้ไหม หลังจาก clone แล้วให้นำไฟล์ไปไว้ใน mariadb/initdb

git clone http://gitlab.cpsudevops.com/nuttachot/titanic.git

ซึ่งถ้าทำมาได้ถึงจุดนี้แล้ว จะเหลืออีกเพียงแค่คำสั่งเดียวก็สามารถใช้งานได้แล้วซึ่งนั้นคือคำสั่ง …

docker-compose up -d

หลังจากนั้นก็รอซักพักเพื่อให้ Docker ทำการสร้าง Image ต่างๆและ Start Container

หลังจากที่ Docker ทำการ Start container ทั้ง 3 ตัวเสร็จสิ้นแล้ว ให้ลองเข้าผ่าน domain/public ip จะได้หน้าตาดังนี้

หลังจากนี้ก็สามารถแก้ไขไฟล์ต่างๆได้ผ่าน /html/ ได้เลย เพราะเนื่องจากไฟล์ docker-compose.yml ได้ config เป็นการ Mount folder เข้าด้วยกัน เพื่อการแก้ไขไฟล์ที่ง่ายขึ้นนั่นเอง

หาก Build ไม่ผ่าน อย่าลืมสร้าง network ให้ docker ซะก่อน ซึ่งในที่นี้ได้ใช้ชื่อว่า “web_network”

name: web_network

--

--

No responses yet