build: Dockerfile + docker-compose with ports driven by .env

- web: nginx:alpine serving the static SPA, listens on WEB_CONTAINER_PORT
  rendered from deploy/nginx.conf.template (envsubst), gzip + cache headers,
  /healthz, proxies /api/ to the api container
- api: julia:1.13-bookworm, libpq5 + curl, Pkg.instantiate from the committed
  Manifest, listens on FARM_API_PORT, healthcheck on /api/v1/health
- db: postgres:17-alpine with 001_init.sql mounted for first-run schema,
  pg_isready healthcheck, named volume
- every port comes from .env (container port + host port separated);
  .env.example committed, .env ignored
This commit is contained in:
Bkk1234 committed 2026-11-23 01:56:39 +07:00
1 parent 05fd682e33
commit af19d04496
8 files changed
+297

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
# .dockerignore — ตัดของที่ไม่อยู่ใน image เว็บ
# คัดลอกเฉพาะ path ที่ Dockerfile ระบุ จึงไม่ควรพึ่งไฟล์นี้ แต่กันไว้ไม่ให้ context ใหญ่
.git
.gitignore
node_modules
farm-blog
intro-irre
archive
coding/scss
coding/js
coding/logic
coding/img
Document
e-book
open data
SQL
PWA-fam-plans
startbootstrap-*
start bootstrap
start Bootstrap
---
_readme.txt
*.tmp
*.docx
*.pdf
*.zip
*.download
service
tests
tools
scripts
*.md
.env
.env.*
+24
View File
@@ -0,0 +1,24 @@
# .env.example — คัดลอกเป็น .env ที่ราก repo แล้วแก้ค่าจริง (ห้าม commit .env)
# พอร์ตทุกตัวแก้ที่นี่ที่เดียว · container port = พอร์ตที่ process ฟังในคอนเทนเนอร์ · host port = พอร์ตที่แมปออกเครื่อง
# ---- เว็บ static (nginx) ----
WEB_CONTAINER_PORT=8080
WEB_HOST_PORT=8095
# ---- API (Julia) ----
API_CONTAINER_PORT=8000
API_HOST_PORT=8000
# ---- PostgreSQL ----
DB_HOST_PORT=5433
DB_NAME=farm_planning
DB_USER=postgres
# ต้องตั้งค่านี้เอง — compose จะไม่ยอม start ถ้าว่าง
DB_PASSWORD=
# ---- ตัวเลือก ----
# true = บันทึกผลคำนวณลง calculation_runs
FARM_PERSIST_RUNS=true
# ตอนพัฒนาให้เปิดกว้างได้ · prod ให้ใส่ origin จริง เช่น https://farm.example.com
FARM_CORS_ORIGIN=*
TZ=Asia/Bangkok
+3
View File
@@ -105,3 +105,6 @@ build/
# Document/ (เอกสารวิธีคำนวณที่เครื่องคำนวณต้องอ้างอิง) · PWA-fam-plans/{dockerfile,nginx}
# AGENTS.md · DESIGN.md · SYSTEM-DESIGN.md · service/README.md
# =====================================================================
# docker compose env (มีรหัส DB)
.env
+30
View File
@@ -0,0 +1,30 @@
# Dockerfile — เว็บ static (SPA + PWA) ของ farm-planning
# พอร์ตที่ nginx ฟังในคอนเทนเนอร์มาจาก env WEB_CONTAINER_PORT (compose ส่งเข้ามา)
# ไฟล์นี้คัดลอกเฉพาะ path ที่หน้าเว็บใช้จริง — ไม่ COPY ทั้ง repo
FROM nginx:1.27-alpine
ENV WEB_CONTAINER_PORT=8080 \
API_CONTAINER_PORT=8000 \
TZ=Asia/Bangkok
# nginx official image แทนค่า ${VAR} ใน /etc/nginx/templates/*.template ให้เองตอน start
COPY deploy/nginx.conf.template /etc/nginx/templates/default.conf.template
# ---- ไฟล์หน้าเว็บ ----
WORKDIR /app
COPY index.html manifest.json sw.js ./
COPY app ./app
COPY assets ./assets
COPY partials ./partials
COPY core ./core
COPY data ./data
COPY css ./css
COPY logo ./logo
COPY ["flat icon", "./flat icon"]
COPY coding/vendor ./coding/vendor
COPY coding/css ./coding/css
# EXPOSE รับ env ไม่ได้ — ใส่ค่าตั้งต้นไว้ให้ตรงกับ WEB_CONTAINER_PORT
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
+73
View File
@@ -0,0 +1,73 @@
# nginx config ของ farm-planning (ถูก envsubst ตอน start)
# ตัวแปรที่ใช้: ${WEB_CONTAINER_PORT} ${API_CONTAINER_PORT}
# หมายเหตุ: official nginx image แทนค่าเฉพาะ env var ที่มีอยู่จริง — $uri/$host ไม่ถูกแตะ
server {
listen ${WEB_CONTAINER_PORT};
listen [::]:${WEB_CONTAINER_PORT};
server_name _;
root /app;
index index.html;
charset utf-8;
gzip on;
gzip_comp_level 6;
gzip_min_length 1024;
gzip_proxied any;
gzip_types text/plain text/css application/javascript application/json
image/svg+xml application/manifest+json;
# ---- health ----
location = /healthz {
access_log off;
add_header Content-Type text/plain always;
return 200 "ok\n";
}
# ---- API (proxy ไปคอนเทนเนอร์ api) ----
location /api/ {
proxy_pass http://api:${API_CONTAINER_PORT};
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 5s;
proxy_read_timeout 30s;
add_header Cache-Control "no-store" always;
}
# ---- ไฟล์ที่ต้องได้ของใหม่ทุกครั้ง (SW, version, shell) ----
location = /sw.js {
add_header Cache-Control "no-cache, must-revalidate" always;
try_files $uri =404;
}
location = /app/version.json {
add_header Cache-Control "no-cache, must-revalidate" always;
try_files $uri =404;
}
location = /index.html {
add_header Cache-Control "no-cache, must-revalidate" always;
}
# ---- JSON ข้อมูล: ให้ cache สั้น ๆ (stale-while-revalidate ที่ฝั่ง SW อยู่แล้ว) ----
location /data/ {
expires 5m;
add_header Cache-Control "public, max-age=300" always;
try_files $uri =404;
}
# ---- asset: cache ยาว ----
location ~* \.(css|js|png|jpe?g|gif|svg|ico|woff2?|webp)$ {
expires 30d;
add_header Cache-Control "public, max-age=2592000, immutable" always;
try_files $uri =404;
}
# ---- SPA (hash router): ทุก path ที่ไม่ใช่ไฟล์ ให้ส่ง index.html ----
location / {
add_header Cache-Control "no-cache, must-revalidate" always;
try_files $uri $uri/ /index.html;
}
}
+97
View File
@@ -0,0 +1,97 @@
name: irrcal
# พอร์ตทั้งหมดมาจาก .env (คัดลอก .env.example → .env แล้วแก้ค่าจริง)
# *_CONTAINER_PORT = พอร์ตที่ process ฟังในคอนเทนเนอร์
# *_HOST_PORT = พอร์ตที่แมปออกเครื่องจริง
# ไฟล์นี้ไม่ถูก commit (.env) — .env.example ขึ้น git
services:
web:
build:
context: .
dockerfile: Dockerfile
image: irrcal-web:latest
environment:
WEB_CONTAINER_PORT: ${WEB_CONTAINER_PORT:-8080}
API_CONTAINER_PORT: ${API_CONTAINER_PORT:-8000}
TZ: ${TZ:-Asia/Bangkok}
ports:
- "${WEB_HOST_PORT:-8095}:${WEB_CONTAINER_PORT:-8080}"
depends_on:
api:
condition: service_started
restart: unless-stopped
healthcheck:
test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:$${WEB_CONTAINER_PORT}/healthz > /dev/null 2>&1 || exit 1"]
interval: 30s
timeout: 5s
retries: 3
start_period: 5s
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
api:
build:
context: ./service
dockerfile: Dockerfile
image: irrcal-api:latest
environment:
FARM_API_HOST: 0.0.0.0
FARM_API_PORT: ${API_CONTAINER_PORT:-8000}
FARM_DB_HOST: db
FARM_DB_PORT: 5432
FARM_DB_NAME: ${DB_NAME:-farm_planning}
FARM_DB_USER: ${DB_USER:-postgres}
FARM_DB_PASSWORD: ${DB_PASSWORD:?ต้องตั้ง DB_PASSWORD ใน .env ก่อนรัน}
FARM_PERSIST_RUNS: ${FARM_PERSIST_RUNS:-true}
FARM_CORS_ORIGIN: ${FARM_CORS_ORIGIN:-*}
TZ: ${TZ:-Asia/Bangkok}
ports:
- "${API_HOST_PORT:-8000}:${API_CONTAINER_PORT:-8000}"
depends_on:
db:
condition: service_healthy
restart: unless-stopped
healthcheck:
test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:$${FARM_API_PORT}/api/v1/health > /dev/null || exit 1"]
interval: 30s
timeout: 5s
retries: 5
start_period: 10s
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
db:
image: postgres:17-alpine
environment:
POSTGRES_DB: ${DB_NAME:-farm_planning}
POSTGRES_USER: ${DB_USER:-postgres}
POSTGRES_PASSWORD: ${DB_PASSWORD:?ต้องตั้ง DB_PASSWORD ใน .env ก่อนรัน}
TZ: ${TZ:-Asia/Bangkok}
volumes:
- pgdata:/var/lib/postgresql/data
# สคีมาถูกสร้างอัตโนมัติเมื่อฐานข้อมูลยังว่าง (ครั้งแรกเท่านั้น)
- ./service/db/001_init.sql:/docker-entrypoint-initdb.d/001_init.sql:ro
ports:
- "${DB_HOST_PORT:-5433}:5432"
restart: unless-stopped
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 10s
timeout: 5s
retries: 10
start_period: 10s
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
volumes:
pgdata:
+7
View File
@@ -0,0 +1,7 @@
# .dockerignore — image ของ API ส่งเฉพาะซอร์สและไฟล์ dependency
# ห้าม exclude โฟลเดอร์ที่ Dockerfile COPY (src, test, Project.toml, Manifest.toml)
.git
.env
.env.*
!*.env.example
*.md
+29
View File
@@ -0,0 +1,29 @@
# Dockerfile — farm-planning API (Julia + HTTP.jl + LibPQ)
# พอร์ตที่ฟังในคอนเทนเนอร์มาจาก env FARM_API_PORT (server.jl อ่านค่านี้เอง)
FROM julia:1.13-bookworm
ENV JULIA_DEPOT_PATH=/opt/julia-depot \
JULIA_NUM_THREADS=1 \
FARM_API_HOST=0.0.0.0 \
FARM_API_PORT=8000 \
TZ=Asia/Bangkok
# libpq = ที่ LibPQ.jl ต้องใช้ · curl = healthcheck
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq5 curl ca-certificates \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /srv
# ---- ชั้น dependency: เปลี่ยนเฉพาะเมื่อ Project/Manifest เปลี่ยน ----
COPY Project.toml Manifest.toml ./
RUN julia --project=/srv -e 'using Pkg; Pkg.instantiate(); Pkg.precompile()'
# ---- ชั้นซอร์ส ----
COPY src ./src
COPY test ./test
EXPOSE 8000
# server.jl ฟังที่ FARM_API_HOST:FARM_API_PORT
CMD ["julia", "--project=/srv", "/srv/src/server.jl"]