Last active
November 12, 2021 17:05
-
-
Save djmaze/e2b46d357d0590c432e0 to your computer and use it in GitHub Desktop.
Get run command from running Docker container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
This was really useful. Thanks