Skip to content

Instantly share code, notes, and snippets.

@tvon tvon/add-label
Last active Feb 2, 2020

What would you like to do?
Add a label to a Docker image tarball.
# Add a label to an image tarball (manifest v1).
# $ docker save alpine -o alpine.tar
# $ add-label alpine.tar org.mine.key foobar
# $ docker load -i alpine.tar
# $ docker inspect alpine | jq '.[].Config.Labels'
# {
# "org.mine.key": "foobar"
# }
set -euf -o pipefail
if [[ -z "${tarball}" || -z "${key}" || -z "${val}" ]]; then
echo "Usage: add-label image.tar key value" && exit 1
workdir=$(mktemp -d)
mkdir "${imagedir}"
cleanup() {
rm -rf "${workdir}"
exit "${rc}"
trap cleanup EXIT
tar -C "${imagedir}" -xf "${tarball}"
# Figure out which file is the config json.
config=$(jq -r '.[].Config' "${imagedir}/manifest.json")
# modify config.Labels to a tempfile
jq -c \
--arg key "${key}" \
--arg val "${val}" \
'. * {config: { Labels: { ($key): $val }}}' "${imagedir}/${config}" \
# modify container_config.Labels and overwrite original file.
jq -c \
--arg key "${key}" \
--arg val "${val}" \
'. * {container_config: { Labels: { ($key): $val }}}' "${workdir}/config.json" \
mv -f "${tarball}" "${tarball}.orig"
pushd "${imagedir}" >/dev/null
# shellcheck disable=SC2046
tar -cf "${workdir}/out.tar" $(ls)
popd >/dev/null
mv "${workdir}/out.tar" "${tarball}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.