การจัดการคอนเทนเนอร์ด้วย Docker Compose
จัดการคอนเทนเนอร์ Docker หลายรายการสำหรับไมโครเซอร์วิสด้วย Docker Compose
การจัดการคอนเทนเนอร์ด้วย Docker Compose เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 3 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Multi-Container Apps?
Microservices often rely on several components. Think of a Spring Boot application needing a database, a message queue, or another API service.
Each component typically runs in its own Docker container. Managing these individually can become complex, especially during development.
How do we start them all together, ensure they can communicate, and manage their lifecycle efficiently? This is where Docker Compose steps in!
Introducing Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. Instead of managing each container separately, you define all services in a single, easy-to-read file.
This definition file is typically named docker-compose.yml. It uses YAML syntax, which is both human-readable and machine-parseable.
Compose simplifies your development workflow by letting you spin up your entire application stack with a single command.
Basic docker-compose.yml Structure
The docker-compose.yml file starts with a version, followed by the services section where you define each container as a distinct service.
Each service represents a container, and you can specify its image, exposed ports, environment variables, and more.
Here's a minimal structure for a simple web app:
version: '3.8'
services:
webapp:
image: 'nginx:latest'
ports:
- '80:80'Defining a Web Service
Let's define a simple web service. We'll use the official Nginx image, map a port, and give it an optional container_name.
The image key specifies the Docker image to use (e.g., nginx:latest). The ports key maps host ports to container ports (e.g., host's port 80 to container's port 80).
version: '3.8'
services:
webapp:
image: 'nginx:latest'
ports:
- '80:80'
container_name: my_nginx_appAdding a Database Service
Most applications need a database. Let's add a PostgreSQL database service to our docker-compose.yml file.
We'll specify the postgres:13 image and set crucial environment variables for credentials (POSTGRES_DB, POSTGRES_USER, POSTGRES_PASSWORD).
version: '3.8'
services:
webapp:
image: 'nginx:latest'
ports:
- '80:80'
database:
image: 'postgres:13'
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: user
POSTGRES_PASSWORD: passwordConnecting Services (Networking)
Docker Compose automatically creates a default network for all services defined in your docker-compose.yml file.
Services can communicate with each other using their service names as hostnames. For example, your web app can connect to the database using database as the hostname and the specified port (e.g., 5432 for PostgreSQL).
This automatic networking simplifies inter-service communication setup significantly.
Data Persistence with Volumes
Containers are ephemeral; data inside them is lost if the container is removed. For databases, this is a critical problem!
Docker volumes provide a way to persist data generated by Docker containers. We can use a named volume to store our database's data outside the container, ensuring it survives container restarts or removals.
version: '3.8'
services:
database:
image: 'postgres:13'
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:Building Custom Images
Often, you'll have your own application code (like a Spring Boot JAR) that needs to be built into a Docker image. Docker Compose can handle this too.
Instead of specifying an image, you can use the build key and point to the directory containing your Dockerfile.
Compose will then build your custom image before starting the service. Here's how it looks:
# docker-compose.yml
version: '3.8'
services:
my-app:
build: ./my-app-dir
ports:
- '8080:8080'
# my-app-dir/Dockerfile
FROM openjdk:17-jdk-slim
COPY target/my-app.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]Key Docker Compose Commands
Once your docker-compose.yml is ready, a few commands will be essential for managing your application stack:
docker compose up: Builds (if needed) and starts all services. Use-dfor detached mode.docker compose down: Stops and removes containers, networks, and optionally volumes.docker compose ps: Lists all running services defined in the Compose file.docker compose logs: Shows log output from services.
Remember to run these commands in the directory where your docker-compose.yml file is located.
Quick Check: Compose Essentials
Consider a docker-compose.yml file that defines two services: web and db.
Which of the following statements about how these services communicate and store data are generally TRUE?
Recap: Your Multi-Container Powerhouse
Congratulations! You've learned how Docker Compose simplifies the management of multi-container applications.
You can now define your entire application stack in a single docker-compose.yml file, including web services, databases, and custom-built images.
This tool is incredibly useful for local development and testing of microservices, allowing you to spin up your whole environment with just one command. Keep practicing!
เรียนรู้ Java ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 24
- บทเรียน
- 93
คำถามที่พบบ่อย
บทเรียน “การจัดการคอนเทนเนอร์ด้วย Docker Compose” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการคอนเทนเนอร์ด้วย Docker Compose” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 3 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการคอนเทนเนอร์ด้วย Docker Compose”
จัดการคอนเทนเนอร์ Docker หลายรายการสำหรับไมโครเซอร์วิสด้วย Docker Compose คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 3 บทเรียน
บทเรียน “การจัดการคอนเทนเนอร์ด้วย Docker Compose” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างอิมเมจ Docker สำหรับแอปพลิเคชัน Spring Boot
- การจัดการคอนเทนเนอร์ด้วย Docker Compose
- แนวคิดการจัดการคอนเทนเนอร์