Ethernet and Coffee

How to regenerate an original docker run command

From this gist

Create a file called run.tpl and put this text in it:

docker run \
 --name={{.Name}} \
 {{range $e := .Config.Env}}--env={{printf "%q" $e}} \
 {{end}}{{range $p, $conf := .NetworkSettings.Ports}}{{with $conf}}-p {{(index $conf 0).HostIp}}:{{(index $conf 0).HostPort}}:{{$p}} \
 {{end}}{{end}}{{range $n, $conf := .NetworkSettings.Networks}}{{with $conf}}--network {{printf "%q" $n}} \
 {{range $conf.Aliases}}--network-alias {{printf "%q" .}} {{end}} \
 {{end}}{{end}}{{range $v := .HostConfig.VolumesFrom}}--volumes-from={{printf "%q" .}} \
 {{end}}{{range $v := .HostConfig.Binds}}--volume={{printf "%q" .}} \
 {{end}}{{range $l, $v := .Config.Labels}}--label {{printf "%q" $l}}={{printf "%q" $v}} \
 {{end}}{{range $v := .HostConfig.CapAdd}}--cap-add {{printf "%q" .}} \
 {{end}}{{range $v := .HostConfig.CapDrop}}--cap-drop {{printf "%q" .}} \
 {{end}}{{range $d := .HostConfig.Devices}}--device={{printf "%q" (index $d).PathOnHost}}:{{printf "%q" (index $d).PathInContainer}}:{{(index $d).CgroupPermissions}} \
 {{end}}{{range $v := .Config.Entrypoint}}--entrypoint={{printf "%q" .}} \
 {{end}}{{with .HostConfig.LogConfig}}--log-driver={{printf "%q" .Type}} \
 {{range $o, $v := .Config}}--log-opt {{$o}}={{printf "%q" $v}} \
 {{end}}{{end}}{{with .HostConfig.RestartPolicy}}--restart="{{.Name}}{{if eq .Name "on-failure"}}{{.MaximumRetryCount}}{{end}}" \
 {{end}}{{if .Config.Tty}}-t \
 {{end}}{{if .Config.OpenStdin}}-i \
 {{end}}{{if not (.Config.AttachStdout)}}--detach=true \
 {{end}}{{if .HostConfig.Privileged}}--privileged \
 {{end}}{{printf "%q" .Config.Image}} \
 {{range .Config.Cmd}}{{printf "%q" .}} {{end}}

Then execute it like this…

root@wumpus:/opt/docker# docker inspect --format "$(<run.tpl)" ethernetandcoffeedocker 
 run \
 --name=/ethernetandcoffee \
 --env="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
 --env="NPM_CONFIG_LOGLEVEL=info" \
 --env="NODE_VERSION=4.6.0" \
 --env="GOSU_VERSION=1.7" \
 --env="GHOST_SOURCE=/usr/src/ghost" \
 --env="GHOST_VERSION=0.11.0" \
 --env="GHOST_CONTENT=/var/lib/ghost" \
 -p 0.0.0.0:2368:2368/tcp \
 --network "bridge" \
 --volume="/opt/ethernetandcoffee/:/var/lib/ghost" \
 --entrypoint="/entrypoint.sh" \
 --log-driver="json-file" \
 --restart="always" \
 "ghost" \
 "npm" "start"
root@wumpus:/opt/docker#