แอปหลายคอนเทนเนอร์ด้วย Docker Compose
ใช้ Docker Compose เพื่อกำหนดและเรียกใช้แอปพลิเคชันหลายบริการ (แอป ฐานข้อมูล แคช) ร่วมกันด้วยไฟล์เชิงประกาศเพียงไฟล์เดียว
แอปหลายคอนเทนเนอร์ด้วย Docker Compose เป็นบทเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Powered SaaS: Stripe + Auth + Billing + Deploy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Docker Compose?
Real apps rarely run in one container. You often need a web app, a database, and a cache. Docker Compose lets you define all of them in one YAML file and start them with a single command.
The compose.yaml File
Compose reads a file named compose.yaml (or docker-compose.yml). Each entry under services describes one container.
services:
web:
build: .
ports:
- "3000:3000"
db:
image: postgres:16Defining a Service
A service can be built from a local Dockerfile or pulled as an existing image. You also set ports, environment variables, and dependencies here.
Environment Variables
Pass configuration with environment or load from an .env file. Never hardcode secrets directly in the YAML committed to git.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}Service Networking
Compose puts all services on a shared network. A service reaches another by its service name as the hostname. The web app connects to the database at db:5432.
Persisting Data with Volumes
Containers are ephemeral. Use a named volume so database data survives restarts and rebuilds.
services:
db:
image: postgres:16
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:Startup Order with depends_on
depends_on controls start order, but it does not wait for a service to be ready. Combine it with a healthcheck so the web app waits until the database accepts connections.
Bringing It Up and Down
Start everything in the background with up -d and stop with down. Add --build to rebuild images after code changes.
docker compose up -d --build
docker compose downViewing Logs
Aggregate logs from all services or follow one. This is essential for debugging how containers interact.
docker compose logs -f webScaling a Service
Run multiple copies of a stateless service to handle more load. Put a load balancer or reverse proxy in front to distribute traffic.
docker compose up -d --scale web=3Compose for Dev vs Production
Use a base compose.yaml plus an override file for each environment. Development might mount source code for hot reload; production uses prebuilt images and stricter restart policies.
Quick Check
Check your Docker Compose knowledge.
Recap
You learned to orchestrate multi-container apps with Docker Compose:
- Define services in
compose.yaml - Connect them via service-name networking
- Persist data with volumes and control order with depends_on/healthcheck
- Use
up,down, logs, and scaling
Compose makes local and production multi-service setups reproducible.
เรียนรู้ AI Powered SaaS: Stripe + Auth + Billing + Deploy ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “แอปหลายคอนเทนเนอร์ด้วย Docker Compose” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “แอปหลายคอนเทนเนอร์ด้วย Docker Compose” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Powered SaaS: Stripe + Auth + Billing + Deploy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Powered SaaS: Stripe + Auth + Billing + Deploy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “แอปหลายคอนเทนเนอร์ด้วย Docker Compose”
ใช้ Docker Compose เพื่อกำหนดและเรียกใช้แอปพลิเคชันหลายบริการ (แอป ฐานข้อมูล แคช) ร่วมกันด้วยไฟล์เชิงประกาศเพียงไฟล์เดียว คุณปฏิบัติ AI Powered SaaS: Stripe + Auth + Billing + Deploy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Powered SaaS: Stripe + Auth + Billing + Deploy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “แอปหลายคอนเทนเนอร์ด้วย Docker Compose” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy นี้ได้ไหม
ได้ บทเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทำแอปพลิเคชันให้เป็นคอนเทนเนอร์ด้วย Docker
- บทนำสู่ผู้ให้บริการคลาวด์
- การนำไปใช้งานบน VM คลาวด์
- แอปหลายคอนเทนเนอร์ด้วย Docker Compose