Containers, Docker, script

Clean up qcow2 in Docker on MacBook

I really enjoy figuring out stuff to do with DOCKER, but I recently tweeted the following:

Twitter

Why you ask? Well, there is this file, Docker.qcow2 located right here:
~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2
on my MacBook Pro that consumes all of the remaining space I have on my hard drive, and SSD’s ain’t cheap.  I’m sure I need to clean up some stuff myself, but that’s not the point of this post. I want Docker to manage this file. Deleting images or containers has no impact on this file. It just continues to GROW.

Space consumption
and now

So this is the impetus for the blog post today.

A quick Google search shows that this issue is not new and is obviously still present in the most recent release as of this post.

Version

Here is the issue according to the Docker forums:
“The qcow file is a block disk image that grows when unwritten blocks in the 64GB virtual device are written so the sparsity of writes due to ext4 disk layout causes an initially large increase in image size.”

Since I am working with mainly the Oracle 12.1 image, which is in the 5+GB range, space is at a premium and so here’s to hoping they find a solution sooner than later as it is at a minimum inconvenient to have to do either one of these processes.

I have found two ways to work around this issue of my Mac experiencing disk space issues as a result of testing with Docker.

bombYep! Bomb the crap out of Docker by going into the Docker -> Preferences menu and clicking that Reset icon.

Docker Preference Scree

And this works, but you will loose all your images and containers if you don’t take some steps to preserve them. What can we do?

The docker save command produces a .tar file to STDOUT. The -o option writes to a file instead of STDOUT.

You can then go ahead and reset Docker. Once the reset has completed, the docker load command will load a .tar or the standard input stream. It restores both images and tags. This important since other applications outside of Docker may reference the image IDs.

In this example you would:

  1. docker save your images. Use docker images to identify the images you want to save
  2. Reset Docker from the Preferences menu
  3. docker load your images from the .tar files created in step 1

The other option I had was to script all this. Fortunately, this the internet and this issue is not unique. I found a script by Théo Chamley, that basically did exactly what I was looking for. I took the liberty of making a few modifications to meet my specific needs.

  • Disk space was an issues so I am storing image archives on an NAS instead of locally
  • I may want to save my images archives
  • and since I planned to share this code, I added some instructions and options to exit before the process started.

With this script in hand, all you need to do is identify the docker image IDs (docker images) then enter them as arguments separated by spaces for the scripts.

This script basically performs the following:

  1. Reads Image IDs as script arguments
  2. Assign the temp dir to a local variable
  3. Executes docker save for each image in argument list and saves them
  4. Stops the Docker app
  5. Deletes the Docker.qcow2 file (this is the problem file)
  6. Starts the Docker app
  7. Executes docker load to restore the archived images
  8. Either deletes or keeps archived images (Y/N)

You can visit my Github Repo find the script here or copy it from below

—-Script—-

You can also visit my github repo to download this file.

Enjoy


Also published on Medium.