Surya.dev
Published on

#3-Storing Container Data In Docker Volumes

Authors

Storing data in container images is one option for automating containers with data, but requires a copy of the data to reside in each container you run.

For static files, this can be a waste of resources. Each file may be no more than a few megabytes, but once the container scales up to handle production loads, those few megabytes can turn into gigabytes of waste.

Instead, I can store a single copy of a static file in a Docker volume for easy sharing between containers.

In this lab, I will learn how Docker volumes interact with containers. I would create a new volume and attach it to the container. I will then clean up the space left by the anonymous volumes automatically created by the container. Lastly, I'll explore backup strategies for volumes.

Discover Anonymous Docker Volumes

  1. Check the docker images:
    docker images
    
  2. Run a container using the postgres:12.1 repository:
    docker run -d --name db1 postgres:12.1
    
  3. Run a second container using the same image:
    docker run -d --name db2 postgres:12.1
    
  4. Check the status of the containers:
    docker ps
    
  5. List the anonymous volumes:
    docker volume ls
    
  6. Inspect the db1 container:
    docker inspect db1 -f '{{ json .Mounts }}' | python -m json.tool
    
  7. Inspect the db2 container:
    docker inspect db2 -f '{{ json .Mounts}}' | python -m json.tool
    
  8. Create a third container using the --rm flag:
    docker run -d --rm --name dbTmp postgres:12.1
    
  9. Check the status of the container:
    docker ps -a
    
  10. List the anonymous volumes:
    docker volume ls
    
  11. Stop the db2 and dbTmp containers:
    docker stop db2 dbTmp
    
  12. List the anonymous volumes:
    docker volume ls
    
  13. Check the status of the containers:
    docker ps -a
    

Create a Docker Volume

  1. Create a Docler volume:
    docker volume create website
    
  2. Verify that the volume was created successfully:
    docker volume ls
    
  3. Copy the widget-factory-inc data to the website container:
    sudo cp -r /home/cloud_user/widget-factory-inc/web/* /var/lib/docker/volumes/website/_data/
    
  4. List the copied data:
    sudo ls -l /var/lib/docker/volumes/website/_data/
    

Use the Website Volume with Containers

  1. Run a docker container with the website volume:
    docker run -d --name web1 -p 80:80 -v website:/usr/local/apache2/htdocs:ro httpd:2.4
    
  2. Check the status of the container:
    docker ps
    
  3. In a web browser, verify connectivity to the container:
    <PUBLIC_IP_ADDRESS>
    
  4. Run a second container with the --rm flag:
    docker run -d --name webTmp --rm -v website:/usr/local/apache2/htdocs:ro httpd:2.4
    
  5. Check the status of the containers:
    docker ps
    
  6. Stop the webTmp container:
    docker stop webTmp
    
  7. Check the status of the containers:
    docker ps -a
    
  8. Verify that the website can still be accessed through a web browser:
    <PUBLIC_IP_ADDRESS>
    

Clean Up Unused Volumes

  1. Clean up the unused volumes:
    docker volume prune
    
  2. Check the currently running containers:
    docker ps -a
    
  3. Remove the db2 container:
    docker rm db2
    
  4. Clean up the unused volumes again:
    docker volume prune
    
  5. List the current volumes:
    docker volume ls
    

Back Up and Restore the Docker Volume

  1. Switch to the root user:

    sudo -i
    
  2. Find where the website volume data is stored:

    docker volume inspect website
    
  3. Copy the Mountpoint.

  4. Back up the volume:

    tar czf /tmp/website_$(date +%Y-%m-%d-%H%M).tgz -C /var/lib/docker/volumes/website/_data .
    
  5. Verify that the data was backed up properly:

    ls -l /tmp/website_*.tgz
    
  6. List the contents of the tgz file:

    tar tf <BACKUP_FILE_NAME>.tgz
    
  7. Exit out of root:

    exit
    
  8. Run a new container using the website volume, and create a backup:

    docker run -it --rm -v website:/website -v /tmp:/backup bash tar czf /backup/website_$(date +%Y-%m-%d-%H-%M).tgz -C /website .
    
  9. Verify that the data was backed up properly:

    ls -l /tmp/website_*.tgz
    
  10. Switch to the root user:

    sudo -i
    
  11. Change to the /docker/volumes/ directory:

    cd /var/lib/docker/volumes/
    
  12. List the volumes:

    ls -l
    
  13. Change to the website/_data directory:

    cd website/_data/
    
  14. Remove the contents of the directory:

    rm * -rf
    
  15. Verify that the backups are still available:

    ls -l /tmp/website_*.tgz
    
  16. Restore the data to the current directory:

    tar xf /tmp/<BACKUP_FILE_NAME>.tgz .
    
  17. Verify that the data was restored successfully:

    ls -l
    

END