0% found this document useful (0 votes)
2 views

Lab 4_ Managing Persistent Data with Docker Volumes

This lab teaches how to use Docker volumes for data persistence outside of containers. It covers creating and mounting Docker volumes, verifying container operation, and ensuring data remains intact after container removal. The lab concludes with the creation of a Docker-managed volume, demonstrating its independent data lifecycle management.

Uploaded by

Ram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 4_ Managing Persistent Data with Docker Volumes

This lab teaches how to use Docker volumes for data persistence outside of containers. It covers creating and mounting Docker volumes, verifying container operation, and ensuring data remains intact after container removal. The lab concludes with the creation of a Docker-managed volume, demonstrating its independent data lifecycle management.

Uploaded by

Ram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 4: Managing Persistent Data with Docker Volumes

Objective

This lab demonstrates how to use Docker volumes to persist data outside of a container. By
completing this lab, you will understand:

1. How to create and mount Docker volumes.


2. How Docker volumes enable data persistence beyond the lifecycle of a container.

Pre-requisites

1. Docker installed on your Windows machine.


2. Basic understanding of terminal commands.
3. A directory to store persistent data.

Step-by-Step Instructions

Step 1: Create a Working Directory

1. Open a terminal or file explorer.


2. Navigate to a folder where you want to work on this lab.
3. Create a folder named docker-volumes-lab:

mkdir docker-volumes-lab
cd docker-volumes-lab

Explanation: This folder will store files and directories used in this lab.

Step 2: Create a Sample File for Testing

Inside the docker-volumes-lab folder, create a subfolder named data:

mkdir data

Add a test HTML file to the data folder:

echo "<h1>Hello from Docker Volume!</h1>" > data/index.html

Explanation: The data/index.html file simulates content that the container will serve.

Ensure the files in the data directory are accessible to Docker. Sometimes, permission issues
can prevent the container from accessing the files.

Using File Explorer:

1. Navigate to the data directory in File Explorer.


2. Right-click on the data folder and select Properties.
3. Go to the Security tab.
4. Verify that the following permissions are set for your user account or Everyone:
○ Read & execute
○ Read
○ Write (if you plan to modify files from the host).

Step 3: Run a Docker Container with a Bind Mount

Run the following command to start an Nginx container and bind the data folder to a directory
inside the container:

docker run -d -p 8080:80 -v %cd%\data:/usr/share/nginx/html nginx

1. Explanation:
○ docker run: Creates and starts a new container.
○ -d: Runs the container in detached mode (in the background).
○ -p 8080:80: Maps port 8080 on your host to port 80 inside the container.
○ -v $(pwd)/data:/usr/share/nginx/html:
■ Mounts the data folder (from the host) to /usr/share/nginx/html
(inside the container).
■ Any changes in the data folder on your host will reflect in the container
and vice versa.
○ nginx: Specifies the image to use.

Step 4: Verify the Container is Running and the Volume is Mounted

Check the running containers:

docker ps

Explanation: docker ps: Lists all running containers, including their port mappings.

Expected Output:
CONTAINER ID IMAGE COMMAND PORTS NAMES
abc123xyz456 nginx "/docker-entrypoint...." 0.0.0.0:8080->80/tcp
dreamy_turing

Key Details:

● The PORTS column confirms that port 8080 is mapped to the container's port 80.

Verify that the index.html file is accessible to the container:

List the contents of the directory inside the container. If the directory is empty or doesn't have
index.html, the volume mapping (-v) is not working correctly.

docker exec -it <container-id> ls /usr/share/nginx/html

Step 5: Access the Containerized Application

Open a web browser and go to:

http://localhost:8080

Verify that the page displays: Hello from Docker Volume!


Explanation: The Nginx server inside the container serves the index.html file from the
/usr/share/nginx/html directory, which is mounted to your host’s data folder.

Step 6: Modify the Host File and Verify Changes

Edit the data/index.html file on the host:

echo "<h1>Updated Content from Host!</h1>" > data/index.html

Refresh the browser page at http://localhost:8080.

Expected Output: Updated Content from Host!

Explanation: Changes made to the file on the host are immediately reflected in the container
because of the bind mount.

Step 7: Stop and Remove the Container

Stop the container:

docker stop <CONTAINER ID>

Remove the container:

docker rm <CONTAINER ID>

Explanation: Stopping and removing the container does not delete the files in the data folder
on your host.

Step 8: Confirm Data Persistence

Verify that the data/index.html file still exists:

type data\index.html
Expected Output:

<h1>Updated Content from Host!</h1>

Explanation: The bind mount ensures that data stored on the host is not lost when the
container is stopped or removed.

Step 9: Use a Docker Volume Instead of a Bind Mount

Create a named Docker volume:

docker volume create my-volume

Run the Nginx container with the volume:

docker run -d -p 8080:80 -v my-volume:/usr/share/nginx/html nginx

Explanation:

○ docker volume create: Creates a Docker-managed volume named


my-volume.
○ Mounting the volume allows Docker to manage the data's lifecycle independently
of the host filesystem.

Copy the host data into the volume:

docker cp data/index.html <CONTAINER ID>:/usr/share/nginx/html/index.html

2. Refresh http://localhost:8080 to verify the content is served from the


Docker-managed volume.

Conclusion

In this lab, you:

1. Used a bind mount to persist data between the host and container.
2. Verified that changes made to host data reflect in the container in real time.
3. Created and used a Docker-managed volume for data persistence.

You might also like