35

I have a Docker container running, and I want to create another one similar to it. How can I find out what command was used to start the container? There's docker inspect, but I'd have to go through and look at each of the config options one by one.

Edit: I want to get the full command used to start the container, including environment variables, links, volumes, etc. For example:

docker run -d --name foo -v /bar:/bar --link baz:baz -e DEBUG=True image bash
z0r
  • 8,185
  • 4
  • 64
  • 83

1 Answers1

38

The following will show the environment variables, the ENTRYPOINT of the Dockerfile, the CMDLINE, the volumes from, the volumes, the links.

docker inspect -f '{{ .Config.Env}} {{ .Config.Entrypoint}} {{ .Config.Cmd}} {{ .VolumesFrom}} {{.Volumes}}  {{ .HostConfig.links}}' container_id
BMW
  • 42,880
  • 12
  • 99
  • 116
user2915097
  • 30,758
  • 6
  • 57
  • 59
  • 2
    like this `docker inspect -f '{{json .Config}}' 2fd | jq '.Env'` for Env, but it will also be a long command with all the parameters , not sure if this is more handy – user2915097 Jun 21 '15 at 08:27
  • 2
    This is pretty cool - certainly makes it easier to find the information. But it's not in a format that I could just copy and paste to spawn a new container. I'm considering writing a script to parse the JSON output of `docker inspect` and then format it into a command. – z0r Jun 21 '15 at 12:53
  • 3
    that jq command is missing volumes. The original reply does not seem to work on the latest docker 18.09.0 it seems they changed the json locations of the volumes – Annerajb Feb 02 '19 at 22:06
  • 2
    @ z0r you should use reckod https://github.com/nexdrew/rekcod that reverse engineers a `docker run` – user2915097 Jun 05 '20 at 07:24
  • 27
    `Template parsing error: template: :1:61: executing "" at <.VolumesFrom>: map has no entry for key "VolumesFrom"` – Nakilon Oct 26 '20 at 01:27
  • 1
    @Nakilon, It means that no volumnes were added and you can try removing that. – Naveen Reddy Marthala Nov 01 '20 at 12:13
  • 6
    Thanks! Works great on docker version 19. Also, this command can be customised according to the user's docker configuration. Eg, if you don't have volumes configured, you will get error - `Template parsing error: template: :1:61: executing "" at <.VolumesFrom>: map has no entry for key "VolumesFrom"`. Just remove those missing keys accordingly. In my case, even host links were not configured. So, finally the full fledged working command in my environment was: `docker inspect -f '{{ .Config.Env}} {{ .Config.Entrypoint}} {{ .Config.Cmd}}' 1e8623ed76d4` – Binita Bharati Sep 02 '21 at 11:56