Skip to content

Instantly share code, notes, and snippets.

@djmaze
Last active November 12, 2021 17:05
Show Gist options
  • Save djmaze/e2b46d357d0590c432e0 to your computer and use it in GitHub Desktop.
Save djmaze/e2b46d357d0590c432e0 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
@okezieokpara
Copy link

This was really useful. Thanks

@thierryherrmann
Copy link

Indeed! Thanks

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