Storing Container Data In Docker Volumes

45 minutes
  • 5 Learning Objectives

About this Hands-on Lab

Storing data within a container image is one option for automating a container with data, but it requires a copy of the data to be in each container you run. For static files, this can be a waste of resources. Each file might not amount to more than a few megabytes, but once the containers are scaled up to handle a production load, that few megabytes can turn into gigabytes of waste. Instead, you can store one copy of the static files in a Docker volume for easy sharing between containers. In this lab, you will learn how Docker volumes interact with containers. You will do this by creating new volumes and attaching them to containers. You’ll then clean up space left by anonymous volumes created automatically by the containers. Finally, you’ll learn about backup strategies for your volumes.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Discover Anonymous Docker Volumes
  1. View the currently available Docker images.
  2. List any existing Docker volumes.
  3. Run two postgres:12.1 containers in detached mode.
  4. Inspect the postgres containers to learn about the attached volumes.
  5. Run another detached postgres container using the --rm flag.
  6. List the Docker volumes, then shut down the postgres containers to see what effect that has on the volumes.
Create a Docker Volume
  1. Create a Docker volume for the website code.
  2. Copy the website code into the volume from the host. You will need to use root permissions.
Use the Website Volume with Containers
  1. Run an httpd container and mount the data volume. Expose container port 80 to view the website.
  2. View the webpage in a browser. Use the public IP address of your instance.
  3. Run another httpd container using the --rm flag. What do you expect will happen with the data volume when this container is stopped?
  4. List the current Docker volumes.
  5. Stop the temporary httpd container.
  6. List the Docker volumes again. Did the volume behave like you expected?
Clean up Unused Volumes
  1. Remove any volumes not currently in use.
  2. View the new list of volumes.
Back up and Restore the Docker Volume
  1. Inspect the website Docker volume to find its location on disk.
  2. Back up the Docker volume from the host by using tar to create a compressed archive.
  3. Back up the Docker volume from a container by mounting the volume and a backup data location to another container.
  4. Restore a backup.

    Note: Because the Docker data directories are protected, change to the root user for this task using sudo su - and the provided password.

Additional Resources

You want to reduce the amount of static and infrequently changing content being built into your images. Some of the content does change occasionally, so it would be nice if the container images did not have to be rebuilt to handle these minor changes.

You'll also need to know how to back up the data being stored in this new way. Backups won't do any good if you can't restore them, so you'll need to know how to restore the volume data from a backup.

Since the website code is static, it makes a good test case to learn how to work with content in Docker volumes. The website code is available on GitHub. It has been cloned to the server, in the cloud_user's home directory, as widget-factory-inc.

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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

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