-
Notifications
You must be signed in to change notification settings - Fork 0
Docker: Volumes
Docker volumes are a mechanism for persisting and sharing data between Docker containers and the host machine, providing a flexible and scalable solution for managing data in containerized environments.
Docker volumes are a mechanism for persisting and sharing data between Docker containers and the host machine, providing a flexible and scalable solution for managing data in containerized environments.
This sentence accurately describes docker volumes, however there is a lot more to docker volumes. I will go into more detail down below.
Docker volumes are vital components of a docker environment architecture, serving as a kind of a bridge between containers and the underlying host system's storage. This bridge allows the docker environment resp. docker containers to have persistent data. They also enable containers to access and manipulate data stored outside their isolated environments, thus creating data persistence, sharing and synchronization across container instances. This makes horizontal scaling possible, since the instances/nodes that are automatically created still use the same data. This is a .yaml file comes into play. See following documentation to see more.
📄Docker Documentation: Persist Container Data
As you can interpret from the text above, persistent data is made possible through docker volumes but why do this? These are some different use cases for using docker volumes:
As you can interpret from the text above, persistent data is made possible through docker volumes, but why do this? Docker volumes serve several critical purposes in containerized environments, enabling a wide range of use cases:
-
Database Management: Docker volumes are commonly used to persist database data across container restarts and updates, ensuring data integrity and consistency in database-driven applications. This allows for efficient database management without the risk of losing critical data.
-
Configuration Sharing: Docker volumes facilitate the sharing of configuration files and other resources among containers within a distributed application. This ensures consistent configurations across multiple instances, simplifying management and deployment processes.
-
Logging and Monitoring: Docker volumes enable containers to write log files and store monitoring data externally, making it easier to analyze and manage application performance. Centralized logging and monitoring solutions can efficiently collect and process data from multiple containers using shared volumes.
-
Content Management: Docker volumes are ideal for managing content in content management systems (CMS) and web applications. By storing media files, user uploads, and other content externally, containers remain lightweight and scalable while ensuring data persistence and availability.
-
Horizontal Scaling: As mentioned before Docker volumes support horizontal scaling by allowing multiple container instances to access the same data simultaneously. This enables seamless load balancing and failover scenarios, where containers can be added or removed dynamically without disrupting data access or integrity.
-
Development and Testing Environments: Docker volumes facilitate the creation of consistent development and testing environments by sharing code repositories, dependencies, and other resources across containers. Developers can work in isolated environments while maintaining access to the same data and configurations.
-
Backup and Disaster Recovery: Docker volumes provide a reliable mechanism for backing up data and implementing disaster recovery strategies. By storing critical data externally, organizations can create regular backups and replicate data across multiple locations for redundancy and fault tolerance.
-
Cross-Platform Compatibility: Docker volumes offer cross-platform compatibility, allowing containers to access data stored on different operating systems and storage solutions. This flexibility enables seamless migration of applications between environments and ensures interoperability across diverse infrastructure setups.
Setting up a Docker volume is a straightforward process. Docker volumes are used to persist data generated by and used by Docker containers. Here's a basic guide on how to set up a Docker volume:
-
Create a Volume: You can create a Docker volume using the
docker volume create
command. For example:docker volume create docker-volume-setup-example
This will create a volume named
docker-volume-setup-example
. -
Mount the Volume to a Container: You can mount a volume to a container when you run it using the
-v
or--volume
flag followed by the volume name and the mount point within the container. For example:docker run -d --name my_container -v docker-volume-setup-example:/data portainer
In this example, the volume
docker-volume-setup-example
is mounted to the/data
directory within the container. -
Verify the Volume: You can list all volumes using the
docker volume ls
command:docker volume ls
This will show you a list of all Docker volumes, including the one you just created.
-
Inspect Volume Information: You can inspect the details of a specific volume using the
docker volume inspect
command. For example:docker volume inspect docker-volume-setup-example
This will give you detailed information about the volume, including its mount point on the host system.
-
Use the Volume: Once the volume is mounted to a container, any data written to the specified mount point within the container will be stored in the volume. This data will persist even after the container is stopped or removed.
-
Remove the Volume: If you no longer need the volume, you can remove it using the
docker volume rm
command. For example:docker volume rm docker-volume-setup-example
This will delete the volume from your system.
Giving a docker container persistent data with docker volumes is best done over another docker related file. Here we will use docker-compose.yaml You can read more to docker-compose here in my Wiki 📄Docker: Docker-Compose
Create a Dockerfile
with the following content in your project directory:
FROM httpd:latest
This will use the latest version of Apache HTTP Server available on Docker Hub.
Create a docker-compose.yml
file in the same directory with the following content:
version: '3'
services:
apache:
build:
context: .
dockerfile: Dockerfile
volumes:
- docker-volume-setup-example:/usr/local/apache2/htdocs
ports:
- "80:80"
volumes:
docker-volume-setup-example:
driver: local
In this Compose file:
- We define a service named
apache
using the image built from the Dockerfile we created. - We mount the volume
docker-volume-setup-example
to the Apache container at/usr/local/apache2/htdocs
. This is where Apache typically serves files from. - Port 80 of the container is exposed to port 80 on the host.
- We define a named volume
docker-volume-setup-example
using thelocal
driver.
Open your terminal and navigate to the directory where your docker-compose.yml
and Dockerfile
are located.
Run the following command to build and run the Docker environment:
docker-compose up -d
This command will build the Apache image using the Dockerfile and start the container based on the configuration in the docker-compose.yml
file. The -d
flag runs the containers in detached mode, meaning they will run in the background.
You can now access your Apache server in your browser by navigating to http://localhost
. Any files you place in the docker-volume-setup-example
volume will be served by Apache.
When you're done testing, you can stop and remove the Docker environment using the following command:
docker-compose down
This will stop and remove the containers defined in the docker-compose.yml
file, but it will preserve the named volume docker-volume-setup-example
, keeping your data intact for future use.