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.
Successfully complete this lab by achieving the following learning objectives:
- Discover Anonymous Docker Volumes
- View the currently available Docker images.
- List any existing Docker volumes.
- Run two
postgres:12.1containers in detached mode.
- Inspect the postgres containers to learn about the attached volumes.
- Run another detached postgres container using the
- List the Docker volumes, then shut down the postgres containers to see what effect that has on the volumes.
- 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.
- Use the Website Volume with Containers
- Run an
httpdcontainer and mount the data volume. Expose container port
80to view the website.
- View the webpage in a browser. Use the public IP address of your instance.
- Run another
httpdcontainer using the
--rmflag. What do you expect will happen with the data volume when this container is stopped?
- List the current Docker volumes.
- Stop the temporary
- List the Docker volumes again. Did the volume behave like you expected?
- Run an
- Clean up Unused Volumes
- Remove any volumes not currently in use.
- View the new list of volumes.
- Back up and Restore the Docker Volume
- Inspect the
websiteDocker 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