การคงข้อมูลด้วยวอลุ่ม Docker
เรียนรู้ว่าวอลุ่ม Docker และการเมานต์แบบ bind ช่วยให้ข้อมูลยังคงอยู่หลังลีฟไทม์ของคอนเทนเนอร์ และช่วยแบ่งปันไฟล์ระหว่างเครื่องโฮสต์กับคอนเทนเนอร์ได้อย่างไร
การคงข้อมูลด้วยวอลุ่ม Docker เป็นบทเรียน Docker & Kubernetes for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Docker & Kubernetes for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Containers Are Ephemeral
When a container is removed, everything written inside its writable layer is gone. For databases, uploads, or logs you need storage that survives the container.
Two Storage Options
Docker offers two main mechanisms:
- Volumes: managed by Docker, stored in a dedicated area
- Bind mounts: map a specific host folder into the container
Volumes are the recommended default.
Creating a Volume
Create a named volume with docker volume create. Docker manages where it physically lives.
docker volume create app_dataMounting a Volume
Attach the volume to a container path with -v. Anything written to that path persists in the volume.
docker run -d -v app_data:/var/lib/data my_appBind Mounts for Development
A bind mount maps a host directory into the container, great for live-editing source code during development.
docker run -v /home/me/project:/app node:20Listing and Inspecting Volumes
See all volumes with docker volume ls, and view details, including the mount point, with docker volume inspect.
docker volume ls
docker volume inspect app_dataVolumes in Compose
In docker-compose.yml you declare volumes once and reference them in services. Use single quotes and indentation, never tabs.
services:
db:
image: postgres:16
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:Sharing a Volume Between Containers
Multiple containers can mount the same volume to share files, for example an app writing files and a backup container reading them.
docker run -v shared:/data writer
docker run -v shared:/data readerRead-Only Mounts
Append :ro to mount a volume read-only when a container should never modify the data, adding a safety guard.
docker run -v config:/etc/app:ro my_appBacking Up a Volume
Back up a volume by running a throwaway container that tars its contents to the host.
docker run --rm -v app_data:/data -v $(pwd):/backup \
alpine tar czf /backup/data.tar.gz /dataCleaning Up Unused Volumes
Orphaned volumes waste disk. Remove a named one with docker volume rm, or clear all unused volumes with docker volume prune, carefully.
docker volume pruneQuick Check
Test your understanding of Docker storage.
Recap
You learned to persist data in Docker:
- Containers are ephemeral; use volumes or bind mounts for durable data
- Mount with
-v name:/path; use bind mounts for live dev - Declare volumes in Compose and share them between containers
- Back up, mount read-only, and prune unused volumes
Volumes let your databases and files outlive any single container.
คำถามที่พบบ่อย
บทเรียน “การคงข้อมูลด้วยวอลุ่ม Docker” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การคงข้อมูลด้วยวอลุ่ม Docker” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Docker & Kubernetes for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การคงข้อมูลด้วยวอลุ่ม Docker”
เรียนรู้ว่าวอลุ่ม Docker และการเมานต์แบบ bind ช่วยให้ข้อมูลยังคงอยู่หลังลีฟไทม์ของคอนเทนเนอร์ และช่วยแบ่งปันไฟล์ระหว่างเครื่องโฮสต์กับคอนเทนเนอร์ได้อย่างไร คุณปฏิบัติ Docker & Kubernetes for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Docker & Kubernetes for Developers หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Docker & Kubernetes for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การคงข้อมูลด้วยวอลุ่ม Docker” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Docker & Kubernetes for Developers นี้ได้ไหม
ได้ บทเรียน Docker & Kubernetes for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเขียน Dockerfile สำหรับอิมเมจ
- การสร้างอิมเมจ Docker แบบกำหนดเอง
- แอปพลิเคชันหลายคอนเทนเนอร์ด้วย Compose
- การคงข้อมูลด้วยวอลุ่ม Docker