In this lab, students will:
- Understand Docker storage drivers and their role in container storage.
- Experiment with Docker's writable layer and observe data persistence behavior.
- Use Docker volumes and bind mounts for persistent storage.
- Docker installed on your Linux system.
- Basic understanding of Docker concepts.
Start a container using the alpine
image:
docker run -dit --name temp-container alpine sh
Access the container shell and create a file:
docker exec -it temp-container sh
touch /test-file
echo "Hello, Docker!" > /test-file
cat /test-file
Stop and remove the container:
docker stop temp-container
docker rm temp-container
Run a new container with the same image and check if the file exists:
docker run -it alpine sh
ls /test-file
Observation:
The file is no longer available, demonstrating that data in the writable layer is not persistent.
Find the storage driver Docker is using:
docker info | grep "Storage Driver"
Inspect the layers of an image:
docker image inspect alpine
Run a container, make changes, and inspect its layers:
docker run -dit --name layer-test alpine sh
docker exec -it layer-test sh -c "echo 'Layer Test' > /layer-file"
docker commit layer-test layer-image
docker image inspect layer-image
Create a named volume:
docker volume create my-volume
Run a container with the volume attached:
docker run -dit --name volume-container -v my-volume:/data alpine sh
Access the container shell and create a file in the volume:
docker exec -it volume-container sh
echo "Persistent data" > /data/persistent-file
Stop and remove the container, then attach the volume to a new container:
docker stop volume-container
docker rm volume-container
docker run -it --name new-container -v my-volume:/data alpine sh
cat /data/persistent-file
Observation:
Data in the volume persists across container lifecycles.
Create a directory on your host system:
mkdir ~/docker-bind-mount
echo "Hello from Host!" > ~/docker-bind-mount/host-file
Run a container with the directory mounted:
docker run -dit --name bind-container -v ~/docker-bind-mount:/app alpine sh
Access the container shell and verify the contents:
docker exec -it bind-container sh
ls /app
cat /app/host-file
echo "Modified by Container" >> /app/host-file
Verify changes from the host:
cat ~/docker-bind-mount/host-file
Remove all containers, volumes, and temporary files created during the lab:
docker rm -f $(docker ps -aq)
docker volume rm my-volume
rm -r ~/docker-bind-mount
- The writable layer in Docker is temporary and tied to the container's lifecycle.
- Docker volumes provide persistent storage that is independent of container lifecycles.
- Bind mounts enable direct interaction between host filesystems and containers.
- Storage drivers play a critical role in managing container storage layers.