Skip to content

Instantly share code, notes, and snippets.

@vincepare
Last active July 3, 2021 00:07
Show Gist options
  • Save vincepare/fed1400108c70cda89eeaaf4f3cdc514 to your computer and use it in GitHub Desktop.
Save vincepare/fed1400108c70cda89eeaaf4f3cdc514 to your computer and use it in GitHub Desktop.
Find the readwrite layer and filesystem root of a docker container - DEPRECATED, use https://github.com/vincepare/docker-backup instead

Finds the readwrite layer (top-most layer) and filesystem root of a docker container.

Accessing the readwrite layer from the docker host can be useful to backup ephemeral container data, while finding the filesystem root allow to browse a container directory structure without having to login into.

Please not that this could only work with union filesystem based storage driver like AUFS and Overlay2. Currently, only AUFS is supported, feel free to contribute to add Overlay2 support.

Install

curl -Lo /usr/local/bin/docker-layer https://gist.github.com/vincepare/fed1400108c70cda89eeaaf4f3cdc514/raw/docker-layer.sh && chmod +x /usr/local/bin/docker-layer

OR :

wget -O /usr/local/bin/docker-layer https://gist.github.com/vincepare/fed1400108c70cda89eeaaf4f3cdc514/raw/docker-layer.sh && chmod +x /usr/local/bin/docker-layer

Example

# docker-layer musing_wiles
rw layer : /var/lib/docker/aufs/diff/c83338693ff190945b2374dea210974b7213bc0916163cc30e16f6ccf1e4b03f
mnt      : /var/lib/docker/aufs/mnt/c83338693ff190945b2374dea210974b7213bc0916163cc30e16f6ccf1e4b03f
# docker-layer -w musing_wiles
/var/lib/docker/aufs/diff/c83338693ff190945b2374dea210974b7213bc0916163cc30e16f6ccf1e4b03f
# docker-layer -m musing_wiles
/var/lib/docker/aufs/mnt/c83338693ff190945b2374dea210974b7213bc0916163cc30e16f6ccf1e4b03f
#!/usr/bin/env bash
#
# Find docker container's rw layer and filesystem root
# Author : Vincent Paré
function usage {
cat <<EOT
Usage: $(basename "$0") [options] <container id or name>
Options :
--rw, -w Container readwrite layer
--mnt, -m Container filesystem root
--help, -h This help message
EOT
exit
}
function check_storage_driver {
local driver=$(docker info 2>/dev/null | grep -i 'storage driver:' | sed -r 's/^.*:\s*//g')
if ! grep -iPq '^aufs$' <<< "$driver"; then
>&2 echo "$(basename "$0") is not compatible with $driver storage driver (please use the AUFS storage driver)"
exit 1
fi
}
function get_docker_root_dir {
local root=$(docker info 2>/dev/null | grep -i 'Docker Root Dir:' | sed -r 's/^.*:\s*//g')
echo "$root"
}
function get_mount_id {
search=$1
if [ -z "$search" ]; then
>&2 echo "Missing container argument"
exit 1
fi
idFromId=$(docker ps -a --format '{{.ID}}' --filter "id=$search")
idFromName=$(docker ps -a --format '{{.ID}}' --filter "name=$search")
if [ -n "$idFromId" ]; then
id=$idFromId
elif [ -n "$idFromName" ]; then
id=$idFromName
else
>&2 echo "No container $1"
exit 2
fi
mountId=$(cat /var/lib/docker/image/aufs/layerdb/mounts/$id*/mount-id)
if [ -z "$mountId" ]; then
>&2 echo "No mount-id for $id"
exit 3
fi
echo "$mountId"
}
if [ -z "$1" ]; then usage; fi
check_storage_driver
case $1 in
-h|--help)
usage
;;
-w|--rw)
dir='diff'
shift
;;
-m|--mnt)
dir='mnt'
shift
;;
esac
root=$(get_docker_root_dir)
mountId=$(get_mount_id "$1")
if [ -z "$mountId" ]; then exit 1; fi
if [ -n "$dir" ]; then
echo "$root/aufs/$dir/$mountId"
else
echo "rw layer : $root/aufs/diff/$mountId"
echo "mnt : $root/aufs/mnt/$mountId"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment