Skip to content

Instantly share code, notes, and snippets.

@exarkun
Forked from miracle2k/.bashrc
Last active November 20, 2016 15:47
Show Gist options
  • Save exarkun/efce92d9bf48be754a7131d9d2ace401 to your computer and use it in GitHub Desktop.
Save exarkun/efce92d9bf48be754a7131d9d2ace401 to your computer and use it in GitHub Desktop.
Convert an existing docker container into a "docker run" command line
# Convert an existing docker container into a "docker run" command line.
#
# This is useful when trying to debug containers that have been created
# by orchestration tools.
#
# Install jq: stedolan.github.io/jq/
jq="docker run --rm -i imega/jq:1.0.0"
function format_run() {
cid=$1
name=$2
json=$(docker inspect $cid 2>&1)
# parse container info
entrypoint=$( echo $json | $jq -r '.[0].Config.Entrypoint | join(" ")' )
envvars=$( echo $json | $jq -r '(.[0].Config.Env | [" -e " + .[]] | join(""))' )
image=$( echo $json | $jq -r .[0].Image )
volume=$( echo $json | $jq -r '.[0].HostConfig.Binds | "-v " + .[0]' )
cmd=$( echo $json | $jq -r '.[0].Config.Cmd | join(" ")' )
echo "docker run --name $name --entrypoint $entrypoint $volume $envvars $image $cmd"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment