- A Cloud Guru
Using Volumes in Docker Containers
Containers are designed to be ephemeral, so when you need persistent data, it is usually not a good idea to store it directly in the container's file system. This is where Docker volumes come into play. Docker volumes allow you to store persistent data outside the container itself, providing greater flexibility in what you can do with your data. In this lab, you will have the opportunity to solve a complex problem using Docker volumes. You will have a chance to work with both shared volumes and bind mounts in order to implement two containers which work together, one container transforming data created by the other. This will give you some practice in working with volumes, as well as some insight into the many ways in which Docker volumes can be used.
Table of Contents
Create the shared volume and counter container to generate some sample data.
- Create a shared volume.
docker volume create test-data
- Create the
countercontainer with the provided command, and mount the shared volume to the container.
docker run --name counter -d --mount type=volume,source=test-data,destination=/var/log/test busybox sh -c 'i=0; while true; do echo "$i: $(date)" >> /var/log/test/1.log; i=$((i+1)); sleep 1; done'
You can confirm that the
countercontainer is generating data by examining the file inside the container:
docker exec counter cat /var/log/test/1.log
Create the fluentd container to transform and output the data from the counter container.
- Create the
fluentdcontainer and mount the shared volume, the config file, and the output directory to it.
docker run --name fluentd -d --mount type=volume,source=test-data,destination=/var/log/input --mount type=bind,source=/etc/fluentd/fluent.conf,destination=/fluentd/etc/fluent.conf --mount type=bind,source=/etc/fluentd/output,destination=/var/log/output --env FLUENTD_ARGS="-c /fluentd/etc/fluent.conf" k8s.gcr.io/fluentd-gcp:1.30
- Verify that the
fluentdcontainer is generating output on the Docker host.
You should see some files containing the transformed log data.
- Create the
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.