Lab 4_ Managing Persistent Data with Docker Volumes
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:
Pre-requisites
Step-by-Step Instructions
mkdir docker-volumes-lab
cd docker-volumes-lab
Explanation: This folder will store files and directories used in this lab.
mkdir data
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.
Run the following command to start an Nginx container and bind the data folder to a directory
inside the container:
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.
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.
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.
http://localhost:8080
Explanation: Changes made to the file on the host are immediately reflected in the container
because of the bind mount.
Explanation: Stopping and removing the container does not delete the files in the data folder
on your host.
type data\index.html
Expected Output:
Explanation: The bind mount ensures that data stored on the host is not lost when the
container is stopped or removed.
Explanation:
Conclusion
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.