Skip to content

Instantly share code, notes, and snippets.

@JoshuaGross
Last active April 9, 2022 04:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshuaGross/762f13ae4ac1c2c09594808c5bea20d1 to your computer and use it in GitHub Desktop.
Save JoshuaGross/762f13ae4ac1c2c09594808c5bea20d1 to your computer and use it in GitHub Desktop.
Freeing up extra disk space inside a Docker container

Freeing up extra disk space inside a Docker container

Sometimes while running a command inside of a docker container, the container itself will run out of disk space and things can fail (sometimes in strange ways). This is not a guide on how to allocate resources, but a quick cookbook on how to free up disk space inside a container. See below for links to more resources and reading.

If at all possible, just run Docker with increased disk space

See Limit disk size and bandwidth of a Docker container.

Resize an aufs volume

If the storage driver is aufs (the default storage driver, except for on RedHat) you can resize the VM disk image used for containers. On Docker for Mac this is located at ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2. Learn how to increase the size of a qcow2 volume.

You can validate your storage container method by running docker info.

Make sure you have qemu-img installed (brew install qemu). Then resize the volume:

qemu-img resize ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2 +1GB

Note that sometimes this forces me into a loop of having to restart Docker, and sometimes reset settings to default; rinse and repeat. It's not ideal and can cause the size of the Docker.qcow2 image to balloon to unfortunate sizes, but the alternative for me was that I couldn't use docker containers at all. ¯_(ツ)_/¯

See how much space is available inside a vanilla docker container:

  $ docker run --rm --privileged debian:jessie df -h
  Filesystem      Size  Used Avail Use% Mounted on
  none             19G   18G     0 100% /
  tmpfs           999M     0  999M   0% /dev
  tmpfs           999M     0  999M   0% /sys/fs/cgroup
  /dev/vda1        19G   18G     0 100% /etc/hosts
  shm              64M     0   64M   0% /dev/shm

It's the first filesystem listed that you want to optimize, sometimes called none, sometimes overlay.

You can alias this command for ease of use:

alias docker_free_space="docker run --rm --privileged debian:jessie df -h"

Remove exited container images

  $ docker ps -q -a -f status=exited | xargs -n 100 docker rm -v

Remove dangling, unused Docker images

Warning: this can remove cached images that may take a long time to rebuild or download.

  $ docker images -q --filter "dangling=true" | xargs -n 100 docker rmi

Check free space again...

  $ docker run --rm --privileged debian:jessie df -h
  Filesystem      Size  Used Avail Use% Mounted on
  none             19G  5.2G   13G  31% /
  tmpfs           999M     0  999M   0% /dev
  tmpfs           999M     0  999M   0% /sys/fs/cgroup
  /dev/vda1        19G  5.2G   13G  31% /etc/hosts
  shm              64M     0   64M   0% /dev/shm

This usually frees up more than enough space for me.

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment