Skip to content

Instantly share code, notes, and snippets.

@djismgaming
Forked from djmaze/get-run-cmd
Created June 5, 2019 14:54
Show Gist options
  • Select an option

  • Save djismgaming/ffa62df22d3dd20a4d02be2fcf68a896 to your computer and use it in GitHub Desktop.

Select an option

Save djismgaming/ffa62df22d3dd20a4d02be2fcf68a896 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/\]//")
# TODO ports
#ports=$(docker inspect --format '{{.Config.ExposedPorts}}' $container | sed "s/\[//; s/\]//")
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