Eric B.
Eric B.

Reputation: 24441

Any way to retrieve the command originally used to create a Docker container?

This question seems to have been often asked, but I cannot find any answer that correctly and clearly specifies how to accomplish this.

I often create test docker containers that I run for a while. Eventually I stop the container and restart it simply using docker start <name>. However, sometimes I am looking to upgrade to a newer image, which means deleting the existing container and creating a new one from the updated image.

I've been looking for a reliable way to retrieve the original 'docker run' command that was used to create the container in the first place. Most responses indicate to simply use docker inspect and look at the Config.Cmd element, but that is not correct.

For instance, creating a container as:

docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Qwerty123<(*' -e TZ=America/Toronto -p 1433:1433 -v c:/dev/docker/mssql:/var/opt/mssql --name mssql -d microsoft/mssql-server-linux

using docker inspect will show:

$ docker inspect mssql | jq -r '.[0]["Config"]["Cmd"]'
[
  "/bin/sh",
  "-c",
  "/opt/mssql/bin/sqlservr"
]

There are many issues created on github for this same request, but all have been closed since the info is already in the inspect output - one just has to know how to read it.

Has anyone created a utility to easily rebuild the command from the output of the inspect command? All the responses that I've seen all refer to the wrong info, notably inspecting the Config.Cmd element, but ignoring the Mounts, the Config.Env, Config.ExposedPorts, Config.Volumes, etc elements.

Upvotes: 1

Views: 2188

Answers (3)

PST
PST

Reputation: 131

I've had the same issue and ended up looking at .bash_history file to find the command I used. This would give you all the docker create commands you'd run;

grep 'docker create' .bash_history

Note: if you ran docker create in that same session you'll need to logout/login for the .bash_history to flush to disk.

Upvotes: 0

user2915097
user2915097

Reputation: 32176

Of course docker inspect is the way to go, but if you just want to "reconstruct" the docker run command, you have

https://github.com/nexdrew/rekcod

it says

Reverse engineer a docker run command from an existing container (via docker inspect).

Another way is Christian G answer at

How to show the run command of a docker container

using bash-preexec

Upvotes: 1

mchawre
mchawre

Reputation: 12268

There are few utilities out there which can help you.

Give it a try

https://github.com/bcicen/docker-replay

https://github.com/lavie/runlike

If you want to know more such cool tools around docker check this https://github.com/veggiemonk/awesome-docker

Upvotes: 1

Related Questions