wagoodman / dive

A tool for exploring each layer in a docker image
MIT License
46.69k stars 1.78k forks source link

Helper script to run via docker #338

Open nemchik opened 3 years ago

nemchik commented 3 years ago

https://github.com/docker/compose/blob/master/script/run/run.sh is a script that you can save using

sudo curl -L --fail https://raw.githubusercontent.com/docker/compose/master/script/run/run.sh -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

and then run docker-compose as if it were installed locally.

Here is a modified version of that script that can be used for dive:

#!/bin/sh
#
# Run dive in a container
#
# This script will attempt to mirror the host paths by using volumes for the
# following paths:
#   * ${PWD}
#
# You can add additional volumes (or any docker run options) using
# the ${DIVE_OPTIONS} environment variable.
#
# You can set a specific image and tag, such as "wagoodman/dive:v0.9.2"
# using the $DIVE_IMAGE_TAG environment variable (defaults to "wagoodman/dive:latest")
#

set -e

# Setup options for connecting to docker host
if [ -z "${DOCKER_HOST}" ]; then
    DOCKER_HOST='unix:///var/run/docker.sock'
fi
if [ -S "${DOCKER_HOST#unix://}" ]; then
    DOCKER_ADDR="-v ${DOCKER_HOST#unix://}:${DOCKER_HOST#unix://} -e DOCKER_HOST"
else
    DOCKER_ADDR="-e DOCKER_HOST -e DOCKER_TLS_VERIFY -e DOCKER_CERT_PATH"
fi

# Setup volume mounts
if [ "${PWD}" != '/' ]; then
    VOLUMES="-v ${PWD}:${PWD}"
fi
if [ -n "${HOME}" ]; then
    VOLUMES="${VOLUMES} -v ${HOME}:${HOME} -e HOME" # Pass in HOME to share docker.config and allow ~/-relative paths to work.
fi

# Only allocate tty if we detect one
if [ -t 0 ] && [ -t 1 ]; then
    DOCKER_RUN_OPTIONS="${DOCKER_RUN_OPTIONS} -t"
fi

# Always set -i to support piped and terminal input in run/exec
DOCKER_RUN_OPTIONS="${DOCKER_RUN_OPTIONS} -i"

# Handle userns security
if docker info --format '{{json .SecurityOptions}}' 2> /dev/null | grep -q 'name=userns'; then
    DOCKER_RUN_OPTIONS="${DOCKER_RUN_OPTIONS} --userns=host"
fi

# Detect SELinux and add --privileged if necessary
if docker info --format '{{json .SecurityOptions}}' 2> /dev/null | grep -q 'name=selinux'; then
    DOCKER_RUN_OPTIONS="${DOCKER_RUN_OPTIONS} --privileged"
fi

# shellcheck disable=SC2086
exec docker run --rm ${DOCKER_RUN_OPTIONS} ${DOCKER_ADDR} ${DIVE_OPTIONS} ${VOLUMES} -w "${PWD}" "${DIVE_IMAGE_TAG:-wagoodman/dive:latest}" "$@"

Just save as /usr/local/bin/dive and then run sudo chmod +x /usr/local/bin/dive and then you can run dive <dive arguments...> as if it were installed natively, but it'll actually just run the docker container.

It would be great if you could include this script in your repo and provide instructions to install it similar to https://github.com/linuxserver/docker-docker-compose#recommended-method (which is based on the official docker compose script but releases with multi-arch manifest support for all CPU types).

P.S. I have tested and run this myself.

esabol commented 3 years ago

@nemchik : This is exactly the sort of thing I was looking for. Thanks!