Skip to main content

Docker Volumes

Docker Volumes are a key feature that allow you to persist data generated by and used by Docker containers. Volumes are stored on the host filesystem outside the container's filesystem, so they are not deleted when the container is removed, making them ideal for persisting data like database files, logs, and configuration files.

Example: Using Docker Volumes

1. Creating a Simple Container with a Volume

Let's create a simple Docker container using the official nginx image and attach a volume to persist data.


docker run -d --name my-nginx -v /mydata:/usr/share/nginx/html:ro -p 8080:80 nginx

Explanation:

  • -d: Runs the container in detached mode (in the background).
  • --name my-nginx: Names the container my-nginx.
  • -v /mydata:/usr/share/nginx/html:ro: Creates a volume that maps the host directory /mydata to the container directory /usr/share/nginx/html. The :ro option makes this volume read-only inside the container.
  • -p 8080:80: Maps port 8080 on the host to port 80 in the container, allowing you to access the Nginx server via http://localhost:8080.
  • nginx: The official Nginx image from Docker Hub.

2. Accessing the Volume

  • On the host machine, navigate to /mydata and create a simple HTML file:

echo "<h1>Hello from Docker Volume!</h1>" > /mydata/index.html
  • Now, if you visit http://localhost:8080 in your browser, you should see the message "Hello from Docker Volume!" displayed, which is served from the file you created on the host.

3. Modifying Data in the Volume

  • Since the volume is mounted with read-only access (:ro), if you try to modify the data from inside the container, it will not work. For example:

docker exec -it my-nginx /bin/bash
echo "<h1>Trying to modify from inside the container</h1>" > /usr/share/nginx/html/index.html
  • This command will fail because the volume is read-only inside the container.

  • If you need to modify the data from within the container, you can mount the volume with read-write access by omitting the :ro flag:


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

4. Listing Volumes

You can list all the volumes created on your Docker host by running:


docker volume ls

This command will show all the volumes, including those automatically created by Docker.

5. Removing a Volume

If you want to remove a volume, first stop and remove the container using it, and then remove the volume:


docker stop my-nginx
docker rm my-nginx
docker volume rm <volume_name>

Replace <volume_name> with the name of the volume you want to remove. If the volume was created automatically, you might need to list the volumes first to find its name.

Example: Persistent Data in a MySQL Container

For a more practical example, let's create a MySQL container with a Docker volume to persist the database data.


docker run -d \
--name my-mysql \
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
-v mysql-data:/var/lib/mysql \
mysql:latest

Explanation:

  • -e MYSQL_ROOT_PASSWORD=my-secret-pw: Sets the root password for the MySQL instance.
  • -v mysql-data:/var/lib/mysql: Mounts a volume named mysql-data to the MySQL data directory inside the container (/var/lib/mysql). This ensures that even if the container is removed, the database data is persisted in the volume.

1. Inspecting the Volume

You can inspect the volume to see its details:


docker volume inspect mysql-data

This command provides information about where the volume is stored on the host and other metadata.

2. Stopping and Removing the Container

Even if you stop and remove the MySQL container, the data will still persist:


docker stop my-mysql
docker rm my-mysql

You can start a new MySQL container with the same volume, and your data will still be intact.


docker run -d \
--name my-new-mysql \
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
-v mysql-data:/var/lib/mysql \
mysql:latest

This new container will have access to the data stored in the mysql-data volume.

Conclusion

Docker Volumes are essential for managing persistent data in containers. They are versatile, easy to use, and provide a reliable way to separate application data from container lifecycles. Whether you're running simple web servers or complex databases, Docker volumes allow you to ensure data persistence across container restarts, removals, and updates.

Comments

Popular posts from this blog

Maven Create and Build Artifacts

In Maven, you can create and build artifacts using the package phase of the build lifecycle. The package phase is responsible for taking the compiled code and other project resources and packaging them into a distributable format, such as a JAR (Java Archive), WAR (Web Application Archive), or other custom formats. Here are the steps to create and build artifacts using Maven: Configure the Build Output: In your project's pom.xml file, you need to configure the output of the build. This includes specifying the type of artifact you want to create (e.g., JAR, WAR) and any additional resources to include. You do this in the <build> section of your pom.xml: <build>     <finalName>my-artifact</finalName> <!-- Name of the artifact without the extension -->     <plugins>         <!-- Plugin configurations for creating the artifact -->         <!-- For example, maven-jar-plugin or maven-war-plugin -->     </plugins> </build> Depend

Experiment No. 5 Title: Applying CI/CD Principles to Web Development Using Jenkins, Git, and Local HTTP Server

  Experiment No. 5 Title: Applying CI/CD Principles to Web Development Using Jenkins, Git, and Local HTTP Server  Objective: The objective of this experiment is to set up a CI/CD pipeline for a web development project using Jenkins, Git, and webhooks, without the need for a Jenkinsfile. You will learn how to automatically build and deploy a web application to a local HTTP server whenever changes are pushed to the Git repository, using Jenkins' "Execute Shell" build step. Introduction: Continuous Integration and Continuous Deployment (CI/CD) is a critical practice in modern software development, allowing teams to automate the building, testing, and deployment of applications. This process ensures that software updates are consistently and reliably delivered to end-users, leading to improved development efficiency and product quality. In this context, this introduction sets the stage for an exploration of how to apply CI/CD principles specifically to web development using J

Maven Repositories (local, central, global)

Maven relies on repositories to manage dependencies, plugins, and other artifacts required for a project. There are typically three types of repositories in Maven: local, central, and remote/global repositories. Local Repository: Location: The local repository is located on your local development machine. By default, it's in the .m2 directory within your user home directory (e.g., C:\Users\<username>\.m2\repository on Windows or /Users/<username>/.m2/repository on macOS and Linux). Purpose: The local repository is used to store artifacts (JARs, POMs, and other files) that your machine has downloaded or built during previous Maven builds. These artifacts are specific to your local development environment. Benefits: Using a local repository improves build performance since it caches dependencies locally, reducing the need to download them repeatedly. It also ensures reproducibility by maintaining a local copy of dependencies. Central Repository: Location: The central repo