1

I am not asking this question: See full command of running/stopped container in Docker

What I am asking is, if someone runs a docker container from the host with a command such as:

docker run --lots --of --mysterious --parameters --volumes --etc image-name

Is there some way for someone else to obtain the original command line in the host system that was used to create the container?

My guess is that no, that you would have to reconstruct it manually by putting together all information obtained from docker inspect... but I may be wrong.

Edit

It seems there is no way to get this information from docker itself, but as explained in the comments below, you can always type history | grep "docker run" and search for the one that launched the container.

Luis Crespo
  • 738
  • 7
  • 20
  • 1
    The Linux history command gives info on the last executed command so if you are using a common user to execute that command should be visible in history or in the .bash_history profile – error404 Mar 07 '19 at 09:24
  • you might be able to see the full command in `ps -ef | grep 'docker'` – scipsycho Mar 07 '19 at 09:31
  • 1
    @yashbagarka I did not think about this cool workaround. I typed `history | grep "docker run"` and there it was :-). Thank you! – Luis Crespo Mar 07 '19 at 10:17
  • @scipsycho I tried the `ps -ef | grep "docker"` and found that the `docker` command-line tool launches other processes and then dies. So what I see in the ps output is not the original docker command, but something else with very different parameters. – Luis Crespo Mar 07 '19 at 10:20
  • @LuisCrespo update the question with answer. Pls accept it – error404 Mar 07 '19 at 10:46

2 Answers2

2

Marking the answer.

linux history would be the best way to get the docker command you had executed earlier.

error404
  • 2,684
  • 2
  • 13
  • 21
1

Bash history often gets lost, e.g. after HISTSIZE is reached or when resizing your VM.

But indeed options can be retrieved through docker inspect.

Here is an example. Actual bash command :

docker run -t -i --name my-osrm-image -v "/custom/path/to/data-osrm:/data" -p 80:5000 osrm/osrm-backend osrm-routed --algorithm mld /data/europe-latest.osrm

As you said, docker ps --no-trunc (and -a if the container is not running) may help but some information (e.g. directory bindings) will be missing.

However we can retrieved all the previous information from

docker inspect  -f 'docker run --name {{.Name}} -v {{join .HostConfig.Binds " "}} -p {{.HostConfig.PortBindings}} {{.Config.Image}} {{join .Config.Cmd " "}}' my-osrm-image

This will output

 docker run --name /my-osrm-image -v /custom/path/to/data-osrm:/data -p map[5000/tcp:[{ 80}]] osrm/osrm-backend osrm-routed --algorithm mld /data/europe-latest.osrm

It requires a bit of cleaning for the port binding but everything is there, and it can be extended to include further options accordingly with the docker inspect JSON output. See also this documentation for the output formatting.

Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76