0Pricing
Docker & Kubernetes for Developers · บทเรียน

การคงอยู่ของข้อมูลด้วยวอลุ่ม

นำวอลุ่ม Docker ไปใช้งานเพื่อให้มั่นใจว่าข้อมูลแอปพลิเคชันยังคงอยู่แม้พ้นอายุการใช้งานของคอนเทนเนอร์แต่ละรายการ

การคงอยู่ของข้อมูลด้วยวอลุ่ม เป็นบทเรียน Docker & Kubernetes for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Docker & Kubernetes for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Ephemeral Containers

When you run an application inside a Docker container, any data it creates or modifies is, by default, stored within the container's writable layer. This data is called ephemeral.

This means that if the container is stopped, removed, or crashes, all that valuable data is lost! This is a big problem for databases, user uploads, or configuration files that need to stick around.

We need a robust way to store data outside the container's lifecycle.

What are Docker Volumes?

Docker volumes are the preferred and most efficient way to persist data generated by and used by Docker containers. They are designed to solve the problem of ephemeral data.

  • Volumes are managed by Docker itself, making them more robust and portable.
  • They live on the host filesystem, but Docker handles their creation, management, and mounting to containers.
  • Crucially, volumes are designed to outlive the container that created them, ensuring your data is safe and persistent.

Volumes vs. Bind Mounts

While volumes are the recommended approach, it's good to know the other main method for persistence:

  • Volumes: Docker fully manages the lifecycle and location of the data on the host. This is the primary choice for most containerized applications.
  • Bind Mounts: You control the exact mount point on the host filesystem. Docker doesn't manage the host data, giving you more control but less portability.

For this lesson, we'll focus on the powerful and flexible Docker volumes!

Creating Your First Volume

Before using a volume with a container, you can explicitly create a named volume. This gives you a clear, manageable reference.

Let's create a volume named my-data.

docker volume create my-data

Attaching Volume to Container

Now, let's run a container and attach our my-data volume to it. We'll mount it to /app/data inside the container using the -v flag.

When the container writes data to /app/data, that data will be saved in the my-data volume, ensuring it persists.

docker run -d --name my-app-with-data -v my-data:/app/data alpine sh -c "echo 'Hello from volume!' > /app/data/message.txt && sleep 3600"

Accessing Volume Data

The data written to our volume is now persistent, even if the container stops or is removed. To prove it, let's run a new, temporary container and mount the same volume to it.

Then, we'll read the message.txt file we created earlier. Notice how the data is still there!

docker run --rm -v my-data:/app/data alpine cat /app/data/message.txt

Listing All Volumes

It's good practice to keep track of the volumes on your system. You can easily list all Docker volumes using a simple command. This helps you manage your persistent data storage and identify any unused volumes.

docker volume ls

Removing Unused Volumes

When you no longer need a volume, it's important to remove it to free up disk space. Remember, removing a volume permanently deletes all its data!

First, ensure no containers are actively using the volume. You might need to stop and remove any containers attached to it before removing the volume itself.

docker stop my-app-with-data
docker rm my-app-with-data
docker volume rm my-data

Volume Persistence Check

Containers are designed to be ephemeral. Which of the following commands is the primary way to ensure data created by a Docker container persists even after the container is removed?

Recap: Data Persistence

Great job! In this lesson, you learned about Docker volumes and why they are crucial for data persistence:

  • Container data is ephemeral and typically lost when containers are removed.
  • Docker volumes provide a managed, robust way to store data on the host, independent of the container's lifecycle.
  • You learned how to create, use, inspect, list, and remove named volumes.

Now your Dockerized applications can keep their important data safe and accessible!

คำถามที่พบบ่อย

บทเรียน “การคงอยู่ของข้อมูลด้วยวอลุ่ม” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การคงอยู่ของข้อมูลด้วยวอลุ่ม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Docker & Kubernetes for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การคงอยู่ของข้อมูลด้วยวอลุ่ม”

นำวอลุ่ม Docker ไปใช้งานเพื่อให้มั่นใจว่าข้อมูลแอปพลิเคชันยังคงอยู่แม้พ้นอายุการใช้งานของคอนเทนเนอร์แต่ละรายการ คุณปฏิบัติ Docker & Kubernetes for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Docker & Kubernetes for Developers หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Docker & Kubernetes for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การคงอยู่ของข้อมูลด้วยวอลุ่ม” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Docker & Kubernetes for Developers นี้ได้ไหม

ได้ บทเรียน Docker & Kubernetes for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทำความเข้าใจประเภทเครือข่าย Docker
  2. การกำหนดค่าเครือข่ายคอนเทนเนอร์
  3. การคงอยู่ของข้อมูลด้วยวอลุ่ม
  4. การเชื่อมต่อคอนเทนเนอร์ด้วยเครือข่าย Docker Compose
← กลับไปที่ Docker & Kubernetes for Developers