Skip to content

Instantly share code, notes, and snippets.

@nickboldt
Last active November 10, 2022 01:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickboldt/b9a9bb4cf12ec1841e69f335b7eddf1a to your computer and use it in GitHub Desktop.
Save nickboldt/b9a9bb4cf12ec1841e69f335b7eddf1a to your computer and use it in GitHub Desktop.
extract files from docker image to local dir
#!/bin/bash -e
usage ()
{
echo "Usage: $0 CONTAINER [--override-arch s390x] [--tar-flags tar-extraction-flags]"
echo "Usage: $0 quay.io/crw/operator-metadata:latest"
echo "Usage: $0 quay.io/crw/plugin-java8-openj9-rhel8:2.4 --override-arch s390x"
echo "Usage: $0 quay.io/crw/pluginregistry-rhel8:latest --tar-flags var/www/html/*/external_images.txt"
echo "Usage: $0 quay.io/crw/devfileregistry-rhel8:latest --tar-flags var/www/html/*/external_images.txt --override-arch ppc64le"
exit
}
if [[ $# -lt 1 ]]; then usage; fi
while [[ "$#" -gt 0 ]]; do
case $1 in
'--override-arch') ARCH_OVERRIDE="--override-arch $2"; shift 1;;
'--tar-flags' ) TAR_FLAGS="$2"; shift 1;;
'-h') usage; shift 0;;
*) container="$1"; shift 0;;
esac
shift 1
done
# echo "ARCH_OVERRIDE = $ARCH_OVERRIDE"
# echo "TAR_FLAGS = $TAR_FLAGS"
# echo "container = $container"
PODMAN=$(command -v podman)
if [[ ! -x $PODMAN ]]; then
echo "[WARNING] podman is not installed."
PODMAN=$(command -v docker)
if [[ ! -x $PODMAN ]]; then
echo "[ERROR] docker is not installed. Aborting."; exit 1
fi
fi
if [[ ${ARCH_OVERRIDE} == "" ]] && [[ ${container} == *"-openj9"* ]]; then
ARCH_OVERRIDE="--override-arch s390x"
fi
tmpcontainer="$(echo $container | tr "/:" "--")-$(date +%s)"
unpackdir="/tmp/${tmpcontainer}"
if [[ $(${PODMAN} images | grep $container) ]] || [[ $(${PODMAN} images localhost/$container:latest -q) ]] || [[ $(${PODMAN} images localhost/$container -q) ]]; then
echo "[INFO] Using local $container ..."
else
# get remote image
echo "[INFO] Pulling $container ..."
${PODMAN} pull ${ARCH_OVERRIDE} $container 2>&1
fi
# create local container
${PODMAN} rm -f "${tmpcontainer}" 2>&1 >/dev/null || true
# use sh for regular containers or ls for scratch containers
${PODMAN} create --name="${tmpcontainer}" $container sh 2>&1 >/dev/null || ${PODMAN} create --name="${tmpcontainer}" $container ls 2>&1 >/dev/null
# export and unpack
${PODMAN} export "${tmpcontainer}" > /tmp/${tmpcontainer}.tar
rm -fr "$unpackdir" || true
mkdir -p "$unpackdir"
echo "[INFO] Extract from container ..."
tar xf /tmp/${tmpcontainer}.tar --wildcards -C "$unpackdir" ${TAR_FLAGS} || true
# cleanup
${PODMAN} rm -f "${tmpcontainer}" 2>&1 >/dev/null || true
rm -fr /tmp/${tmpcontainer}.tar || true
echo "[INFO] Container $container unpacked to $unpackdir"
@themr0c
Copy link

themr0c commented Jun 17, 2021

  • bash /home/remote/ffloreth/src/gl-cee/ffloreth/red-hat-devtools/tools/downstreaming-crw/dockerContainerExtract.sh quay.io/crw/crw-2-rhel8-operator-metadata:2.9
    [INFO] Pulling quay.io/crw/crw-2-rhel8-operator-metadata:2.9 ...
    Trying to pull quay.io/crw/crw-2-rhel8-operator-metadata:2.9...
    Getting image source signatures
    Copying blob 151be4f174b0 skipped: already exists
    Copying blob 4f4fb700ef54 [--------------------------------------] 0.0b / 0.0b
    Copying config 9ab1bad8a2 done
    Writing manifest to image destination
    Storing signatures
    9ab1bad8a24f6c814c53bed159ee1a3d0f8d1d8d0f4bc9c258353d2c291e6405
    Error: no container with name or ID "quay.io-crw-crw-2-rhel8-operator-metadata-2.9-1623921854" found: no such container
    [INFO] Extract from container ...
    [INFO] Container quay.io/crw/crw-2-rhel8-operator-metadata:2.9 unpacked to /home/remote/ffloreth/src/gl-cee/ffloreth/red-hat-devtools/tools/downstreaming-crw/crw-operator-metadata

@nickboldt
Copy link
Author

this is the line that throws a benign error message:

+ /usr/bin/podman rm -f ubi8-minimal-8.4-1623932135
Error: Failed to evict container: "": Failed to find container "ubi8-minimal-8.4-1623932135" in state: no container with name or ID ubi8-minimal-8.4-1623932135 found: no such container

@nickboldt
Copy link
Author

Official version of this gist is now in https://github.com/redhat-developer/codeready-workspaces/blob/crw-2-rhel-8/product/containerExtract.sh so feel free to throw me a PR there.

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