Compare commits

..

2 Commits

Author SHA1 Message Date
4c420c3cf4 Add initial desired README.md 2021-04-26 16:14:28 -05:00
929ef0e127 Add initial search prototype 2021-04-26 11:50:36 -05:00
4 changed files with 94 additions and 0 deletions

4
Dockerfile Normal file
View File

@ -0,0 +1,4 @@
FROM busybox
ADD testfile.txt .
RUN rm testfile.txt && mkdir app && echo "hello" > /app/othertestfile.txt

41
README.md Normal file
View File

@ -0,0 +1,41 @@
# Docker Artifact
> Use docker image labels to identify which image layer contains a particular file, enabling retrieving just that file (well, the layer the file is in) from the image without needing to download the whole image.
## Setup
### Prerequisites
Requires the `jq` and `curl` programs to be installed on your PATH.
### Installation
Download `docker-artifact.sh` file from this repository and install it at `~/.docker/cli-plugins/docker-artifact` (note the lack of `.sh` suffix) with execute permissions. Validate correct installation by observing the `artifact` command listed in `docker help`.
-OR-
Run the following command in your shell:
```bash
mkdir -p ~/.docker/cli-plugins && \
curl https://raw.githubusercontent.com/infogulch/docker-artifact/master/docker-artifact.sh > ~/.docker/cli-plugins/docker-artifact && \
chmod +x ~/.docker/cli-plugins/docker-artifact && \
docker help | grep artifact > /dev/null && echo "Docker artifact install succeeded!" || echo "Docker artifact install failed :("
```
## Usage
`docker artifact label [image] [file-path-1] [file-path-2] ...`
Adds labels to an existing image enabling the `download` command below to pull just the layers that contain the file paths specified above.
Note: `download` must download the whole layer; to optimize for artifact download size, add the target files to the image in a separate layer.
`docker artifact download [image] [file-path-1] [file-path-2]`
Downloads the image layers associated with the file paths specified and extracts them into the current directory.
## See also
Related to [timwillfixit's original `docker-artifact`](https://github.com/tomwillfixit/docker-artifact) in spirit, though not in history. The primary difference is that this uses a more precice strategy to search for files.

48
docker-artifact.sh Executable file
View File

@ -0,0 +1,48 @@
set -e
verbose=1
image="$1"
export searchpath="$2"
export tarfile="$(mktemp)-$image.tar"
# Save image tar to temp file
[ $verbose ] && >&2 echo "Exporting image '$image' to temp file '$tarfile'..."
docker image save "$image" -o "$tarfile"
# Set up cleanup to not leave image behind
function onexit() {
[ $verbose ] && >&2 echo "Cleaning up exported image '$tarfile'"
rm "$tarfile"
}
trap onexit EXIT
# Extract manifest and config file contents
[ $verbose ] && >&2 echo "Collecting metadata from image..."
manifest=$(tar -xf "$tarfile" -x manifest.json -O | jq)
config_file=$(echo "$manifest" | jq -r '.[0].Config')
config=$(tar -f "$tarfile" -x "$config_file" -O | jq)
# Combine manifest.json and config json to build a map from tar layer directory to layer id sha
export idmap=$(echo "$manifest" "$config" | jq -sr '[ [ .[0][0].Layers, .[1].rootfs.diff_ids ] | transpose[] | { (.[0]): .[1] } ] | reduce .[] as $x ({}; . * $x)')
# Search each layer for a file matching $searchpath
# TODO also search for related whiteout files that start with `.wh.`
[ $verbose ] && >&2 echo "Searching layers for '$searchpath'..."
found=$(echo "$manifest" | jq '.[0].Layers[]' | xargs -I {} sh -c 'digest=$(echo "$idmap" | jq -r ".[\"{}\"]"); tar -f "$tarfile" -x {} -O | tar -t | sed s_^_/_ | grep -wx "$searchpath" | xargs -I [] echo "[]=$digest"')
# If more than one is found, then bail
if [ $(echo "$found" | wc -l) -gt 1 ]; then
>&2 echo "Multiple matches found, aborting:"
>&2 echo "$found"
exit 2
fi
# print out labels to add during image rebuild
labels=$(echo "$found" | sed 's_^.*$_--label "\0"_' | paste -d' ')
echo "$labels"
[ $verbose ] && >&2 echo "Done!"
exit

1
testfile.txt Normal file
View File

@ -0,0 +1 @@
hi