Skip to content

Instantly share code, notes, and snippets.

@hervenicol
Forked from efrecon/run.tpl
Last active May 12, 2023 14:20
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hervenicol/ee5fd66d42d6f301f6ecd5008049df07 to your computer and use it in GitHub Desktop.
Save hervenicol/ee5fd66d42d6f301f6ecd5008049df07 to your computer and use it in GitHub Desktop.
`docker inspect` template to regenerate the `docker run` command that created a container
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}}
@hervenicol
Copy link
Author

Merged work from @jonathanperret found here: https://gist.github.com/865ebce32613418ade54b528ca181b00.git

  • better output (via printf)
  • added autoremove (--rm)

@hervenicol
Copy link
Author

added --network and --network-alias support

@jamesgallagher-ie
Copy link

Thanks for the improvements - this just came in really useful for me :)

@gamesbook
Copy link

I get an error:

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}} \
bash: syntax error near unexpected token `('

@kaikuchn
Copy link

@gamesbook you're not supposed to run the template, but to generate a run command from it. See https://gist.github.com/efrecon/8ce9c75d518b6eb863f667442d7bc679#gistcomment-1812924

@efrecon
Copy link

efrecon commented Feb 22, 2018

Merged back this into the original version, thanks @hervenicol!

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