Skip to content

Instantly share code, notes, and snippets.

@DALDEI
Forked from miracle2k/.bashrc
Last active February 2, 2018 11:50
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 DALDEI/13f38f1a53443034c3887a23104617d5 to your computer and use it in GitHub Desktop.
Save DALDEI/13f38f1a53443034c3887a23104617d5 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/
# Improved version that handles ports, volumes, binds
function format_run() {
cid=$1
json=$(docker inspect $cid 2>&1)
# parse container info
entrypoint=$( echo $json | jq -j '.[0].Config.Entrypoint | join(" ")' )
envvars=$( echo $json | jq -j '(.[0].Config.Env | [" -e " + .[]] | join(""))' )
image=$( echo $json | jq -j .[0].Image )
cmd=$( echo $json | jq -j '.[0].Config.Cmd? | join(" ")' )
ports=$( echo $json | jq -j '(.[0].HostConfig.PortBindings?|.[][]|["-p " + .HostIp + ":" + .HostPort]|join(""))' )
vols=$( echo $json | jq -j '(.[0].HostConfig.Binds?|["-v " + .[]?]|join(""))' )
pub=$( echo $json | jq -j 'if .[0].HostConfig.PublishAllPorts == false then "-P" else "" end')
echo "docker run --entrypoint $entrypoint $envvars $ports $vols $pub $image $cmd"
}
@DALDEI
Copy link
Author

DALDEI commented Mar 28, 2017

Added support for volumes, ports, published ports

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