Skip to content

Instantly share code, notes, and snippets.

@efrecon efrecon/run.tpl
Last active Aug 19, 2019

Embed
What would you like to do?
`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}}
@efrecon

This comment has been minimized.

Copy link
Owner Author

commented Jun 28, 2016

This does not support --links so far, nor a number of less-often used options. Any taker?

@efrecon

This comment has been minimized.

Copy link
Owner Author

commented Jun 28, 2016

To run, save this to a file, e.g. run.tpl and do docker inspect --format "$(<run.tpl)" name_or_id_of_running_container

@efrecon

This comment has been minimized.

Copy link
Owner Author

commented Jul 30, 2016

Now with properly working entrypoint (as long as it's a single command, restriction from the command line) and export of devices.

@qband

This comment has been minimized.

Copy link

commented Aug 15, 2016

wow, this way is so cool

@epishan

This comment has been minimized.

Copy link

commented Nov 1, 2016

simple and works out of the box... great hack, man!

@grownseed

This comment has been minimized.

Copy link

commented Nov 15, 2016

@efrecon Thanks a lot for putting this together!

Using your exact command and template, I'm running into two issues, and unfortunately I don't know Go templates (or Go for that matter), so my attempts at fixing them aren't particularly fruitful.

First issue:

I end up with the output being duplicated within itself, e.g.:

docker run \
    --name=/teamcity-server \
    --env="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
    --env="JRE_HOME=/usr/lib/jvm/oracle-jdk/jre" \
    --env="TEAMCITY_DATA_PATH=/data/teamcity_server/datadir" \
    -p 0.0.0.0:8111:8111/tcp \
    --volume="/teamcity:/data/teamcity_server/datadir" \
    --volume="/teamcity/logs:/opt/teamcity/logs" \
    docker run \
    --name=/teamcity-server \
    --env="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
    --env="JRE_HOME=/usr/lib/jvm/oracle-jdk/jre" \
    --env="TEAMCITY_DATA_PATH=/data/teamcity_server/datadir" \
    -p 0.0.0.0:8111:8111/tcp \
    --volume="/teamcity:/data/teamcity_server/datadir" \
    --volume="/teamcity/logs:/opt/teamcity/logs" \
    --log-driver="json-file" \
    --restart="no" \
    --detach=true \
    jetbrains/teamcity-server \
    /run-services.sh

(I tried other containers just in case, still getting the same issue).

Second issue:

Some of my containers have no exposed port (e.g. "9090/tcp": {}), which throws:

-p reflect: call of reflect.Value.Type on zero Value

Any help would be appreciated, and my apologies in advance if I'm missing something completely obvious. Thank you!

@hervenicol

This comment has been minimized.

Copy link

commented Dec 21, 2016

Hi @efrecon. I have an improved version here: https://gist.github.com/hervenicol/ee5fd66d42d6f301f6ecd5008049df07.
Feel free to merge it.

@gamesbook

This comment has been minimized.

Copy link

commented Mar 6, 2017

I get an error:

docker run \
>     --name={{.Name}} \
>     {{range $e := .Config.Env}}--env="{{$e}}" \
>     {{end}}{{range $p, $conf := .NetworkSettings.Ports}}-p {{(index $conf 0).HostIp}}:{{(index $conf 0).HostPort}}:{{$p}} \
bash: syntax error near unexpected token `('
@efrecon

This comment has been minimized.

Copy link
Owner Author

commented Feb 22, 2018

Merged current version from @hervenicol. Thanks for carrying further the flag!

@awltux

This comment has been minimized.

Copy link

commented Jan 30, 2019

Nice! Appreciate your work.
Small fix (missing a separating colon)
{{if eq .Name "on-failure"}}:{{.MaximumRetryCount}}{{end}}

@yjst2012

This comment has been minimized.

Copy link

commented Jan 30, 2019

Great, man, this is just what I'm looking for!

@efrecon

This comment has been minimized.

Copy link
Owner Author

commented Feb 20, 2019

Thank you @awltux, fixed.

@wal5hy

This comment has been minimized.

Copy link

commented Mar 23, 2019

Really useful script, thanks! What I can't figure out is how to ignore settings which are a part of the image, and only take settings which are applied in addition to the default image settings. Any ideas?

@mcgyver83

This comment has been minimized.

Copy link

commented Mar 24, 2019

Great job man!
Works like a charme!

@ptrouvin

This comment has been minimized.

Copy link

commented Jun 9, 2019

Very elegant and simple solution! Many thanks.

@efrecon

This comment has been minimized.

Copy link
Owner Author

commented Jun 10, 2019

@wal5hy no, unfortunately no ideas. Did you figure it out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.