Skip to content

Instantly share code, notes, and snippets.

@manuel-rubio
Forked from djmaze/get-run-cmd
Last active August 2, 2021 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuel-rubio/dc3495157caa68273c6a0957b7669367 to your computer and use it in GitHub Desktop.
Save manuel-rubio/dc3495157caa68273c6a0957b7669367 to your computer and use it in GitHub Desktop.
Get run command from running Docker container
#!/bin/bash
set -eo pipefail
container=$1
image=$(docker inspect --format '{{.Config.Image}}' $container)
cmd=$(docker inspect --format '{{.Config.Cmd}}' $container)
if [[ $cmd == '<nil>' ]]; then cmd=''; fi
binds=$(docker inspect --format '{{.HostConfig.Binds}}' $container | sed "s/\[//; s/\]//")
if [[ $binds == '<nil>' ]]; then binds=''; fi
envs=$(docker inspect --format '{{.Config.Env}}' $container | sed "s/\[//; s/\]//")
ports=$(docker inspect --format '{{range $p, $conf := .NetworkSettings.Ports}}-p {{(index $conf 0).HostIp}}:{{(index $conf 0).HostPort}}:{{$p}} {{end}}' $container)
if [[ $ports == '<nil>' ]]; then ports=''; fi
for b in $binds; do bind_args="$bind_args -v $b"; done
for e in $envs; do
if [[ "$e" != PATH=* ]]; then
env_args="$env_args -e \"$e\""
fi
done
echo docker run $bind_args $env_args $image $cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment