4

What's the best way to reconstruct docker run command parameters from existing docker container? I could use docker inspect and use the info found there. Is there any better way?

  • It does seem like `docker inspect` is the proper tool to inspect running containers. What's wrong with using that? Do you want to talk to the Docker API directly? – Thilo Oct 27 '16 at 06:35

2 Answers2

5

Not super easy, but you can do it by formatting the output from docker inspect. For a container started with this command:

> docker run -d -v ~:/home -p 8080:80 -e NEW_VAR=x --name web3 nginx:alpine sleep 10m

You can pull out the volumes, port mapping, environment variables, container name, image name and command with:

> docker inspect -f "V: {{.Mounts}} P: {{.HostConfig.PortBindings}} E:{{.Config.Env}} NAME: {{.Name }} IMAGE: {{.Config.Image}} COMMAND: {{.Path}} {{.Args}}" web3

That gives you the output:

V: [{ /home/scrapbook /home   true rprivate}] P: map[80/tcp:[{ 8080}]] E:[NEW_VAR=x PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin NGINX_VERSION=1.11.5] NAME: /web3 IMAGE: nginx:alpine COMMAND: sleep [10m]

Which is a start.

Docker Captain Adrian Mouat has an excellent blog post on formatting the output: Docker Inspect Template Magic.

Elton Stoneman
  • 17,906
  • 5
  • 47
  • 44
0

See also this answer which links to a tool which programmatically derives the docker run command from a container.

Stefan Winkler
  • 3,871
  • 1
  • 18
  • 35