Skip to content

Instantly share code, notes, and snippets.

@miracle2k
Created July 6, 2014 12:19
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save miracle2k/c85b7b077fdb8d54bc89 to your computer and use it in GitHub Desktop.
Save miracle2k/c85b7b077fdb8d54bc89 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/
function format_run() {
cid=$1
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 )
cmd=$( echo $json | jq -r '.[0].Config.Cmd | join(" ")' )
echo "docker run --entrypoint $entrypoint $envvars $image $cmd"
}
@westsouthnight
Copy link

This with ports mapping:


function format_run() {
    cid=$1

    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                                       )
    cmd=$(         echo $json | jq -r '.[0].Config.Cmd | join(" ")'                    )
    mapped_to_container=`(echo $json| jq '.[0].NetworkSettings .Ports' | jq '.[]' | jq '.[]' | jq '.HostPort' | tr -d '"' | cut -f 1- -d\/ --output-delimiter=$'\n' )`
    mapped_to_host=$(echo $json | jq -r '.[0].NetworkSettings.Ports | keys | to_entries |  map(.value) | join(" ")')

    array=(${mapped_to_container// / })
    array2=(${mapped_to_host// / })

    ports_result=()

    for i in "${!array[@]}"
    do
        for b in "${!array2[@]}"
        do
          if [[ "$i" == "$b" ]];
          then
            port_map=`(echo "-p ${array[i]}:${array2[b]}")`
            ports_result+=($port_map)
          fi
         done
    done

    echo "docker run --entrypoint $entrypoint $envvars ${ports_result[@]} $image $cmd"
}

@amine250
Copy link

Note: volumes are missing.

@weshouman
Copy link

Here's with volumes too:

function format_run() {
    cid=$1

    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)
    cmd=$(echo $json | jq -r '.[0].Config.Cmd | join(" ")')
    
    # Parse port mappings
    mapped_to_container=`(echo $json| jq '.[0].NetworkSettings.Ports' | jq 'to_entries[] | .key' | tr -d '"' | cut -f 1- -d\/ --output-delimiter=$'\n' )`
    mapped_to_host=$(echo $json | jq -r '.[0].NetworkSettings.Ports | to_entries | map(.value[].HostPort) | join(" ")')

    array=(${mapped_to_container// / })
    array2=(${mapped_to_host// / })

    ports_result=()

    for i in "${!array[@]}"; do
        for b in "${!array2[@]}"; do
            if [[ "$i" == "$b" ]]; then
                port_map="-p ${array[i]}:${array2[b]}"
                ports_result+=($port_map)
            fi
        done
    done

    # Parse volume information
    volumes=$(echo $json | jq -r '.[0].Mounts | map("-v " + .Source + ":" + .Destination) | join(" ")')

    echo "docker run --entrypoint $entrypoint $envvars ${ports_result[@]} $volumes $image $cmd"
}

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