- Lab
- A Cloud Guru
Storing Container Data In Docker Volumes
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.
Path Info
Table of Contents
-
Challenge
Discover Anonymous Docker Volumes
- View the currently available Docker images.
- List any existing Docker volumes.
- Run two
postgres:12.1
containers in detached mode. - Inspect the postgres containers to learn about the attached volumes.
- Run another detached postgres container using the
--rm
flag. - List the Docker volumes, then shut down the postgres containers to see what effect that has on the volumes.
-
Challenge
Create a Docker Volume
- Create a Docker volume for the website code.
- Copy the website code into the volume from the host. You will need to use root permissions.
-
Challenge
Use the Website Volume with Containers
- Run an
httpd
container and mount the data volume. Expose container port80
to view the website. - View the webpage in a browser. Use the public IP address of your instance.
- Run another
httpd
container using the--rm
flag. What do you expect will happen with the data volume when this container is stopped? - List the current Docker volumes.
- Stop the temporary
httpd
container. - List the Docker volumes again. Did the volume behave like you expected?
- Run an
-
Challenge
Clean Up Unused Volumes
- Remove any volumes not currently in use.
- View the new list of volumes.
-
Challenge
Back Up and Restore the Docker Volume
- Inspect the
website
Docker volume to find its location on disk. - Back up the Docker volume from the host by using tar to create a compressed archive.
- Back up the Docker volume from a container by mounting the volume and a backup data location to another container.
- 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. - Inspect 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.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.