Trwałość danych dzięki wolumenom
Zaimplementuj wolumeny Docker, aby dane aplikacji zachowywały się po zakończeniu cyklu życia poszczególnych kontenerów.
Trwałość danych dzięki wolumenom to bezpłatna lekcja Docker & Kubernetes for Developers na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Docker & Kubernetes for Developers, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Docker & Kubernetes for Developers zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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-dataAttaching 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.txtListing 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 lsRemoving 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-dataVolume 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!
Często zadawane pytania
Czy lekcja „Trwałość danych dzięki wolumenom” jest bezpłatna?
Tak — pełny tekst „Trwałość danych dzięki wolumenom” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Docker & Kubernetes for Developers, przejdź na CoddyKit PRO. Kurs Docker & Kubernetes for Developers zawiera 4 lekcji w sumie.
Co nauczysz się w „Trwałość danych dzięki wolumenom”?
Zaimplementuj wolumeny Docker, aby dane aplikacji zachowywały się po zakończeniu cyklu życia poszczególnych kontenerów. Ćwiczysz Docker & Kubernetes for Developers z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Docker & Kubernetes for Developers?
Nie wymagamy żadnego doświadczenia. Docker & Kubernetes for Developers w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Trwałość danych dzięki wolumenom”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Docker & Kubernetes for Developers?
Tak. Każda lekcja Docker & Kubernetes for Developers zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Poznawanie typów sieci Docker
- Konfigurowanie sieci kontenerów
- Trwałość danych dzięki wolumenom
- Łączenie kontenerów za pomocą sieci Docker Compose