Using Volumes in Docker Containers

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create the shared volume and counter container to generate some sample data.
  1. Create a shared volume.

    docker volume create test-data
  2. Create the counter container 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 
    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 counter container 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.
  1. Create the fluentd container 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"
  2. Verify that the fluentd container is generating output on the Docker host.

    ls /etc/fluentd/output

    You should see some files containing the transformed log data.

Additional Resources

Your team wants to use an application called fluentd to transform log output from containers into a standard format. In order to make use of this technology, they have asked you to design a proof-of-concept to demonstrate how this can be done. You will need to run a container that generates some log output in a file, then run a second container with fluentd that is able to read the log output from the first container, transform it, and output the transformed data to another file on the host machine. You do not need to have knowledge of fluentd to complete this task. A fluentd configuration file has already been provided for you.

The proof-of-concept should meet the following specifications:

  • The first container should be called counter. It will generate some log data for testing by counting numbers.
  • For the counter container, use the busybox image with the following command: sh -c 'i=0; while true; do echo "$i: $(date)" >> /var/log/test/1.log; i=$((i+1)); sleep 1; done'. This will write some test data to /var/log/test/1.log every second.
  • Create a volume called test-data, and mount it to the counter container at /var/log/test. This volume will be shared with the fluentd container so that it can read the test data.
  • Create a second container called fluentd with the image.
  • Provide an environment variable to the fluentd container called FLUENTD_ARGS with the value -c /fluentd/etc/fluent.conf
  • Mount the test-data volume to the fluentd container at /var/log/input.
  • There is a fluentd configuration file located on the server at /etc/fluentd/fluent.conf. Use a bind mount to mount this file to the fluentd container at /fluentd/etc/fluent.conf.
  • Create an additional bind mount so that the fluentd container can output the transformed log data to the host's file system. Mount the directory /etc/fluentd/output on the host to /var/log/output on the fluentd container.

If you get stuck, feel free to check out the solution video, or the detailed instructions under each objective. Good luck!

What are Hands-on Labs

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.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?