How to show the run command of a docker container

Solution 1:

So how to reverse engineering docker run command?

There is a github repository which try to reverse engineering docker run command, but it is not perfect currently, version is 0.1.2. You should follow it for updating. Maybe one day you can use it to get correct run command with it.

$ sudo pip install runlike

# run the ubuntu image
$ docker run -ti ubuntu bash

$ docker ps -a  
# suppose you get the container ID 1dfff2ba0226

# Run runlike to get the docker run command. 
$ runlike 1dfff2ba0226
docker run --name=elated_cray -t ubuntu bash

Github repository: runlike

Updates:

Run without installing (Thanks @tilo)

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock:ro \
    assaflavie/runlike YOUR-CONTAINER

or set alias and put it in your shell's profile

alias runlike="docker run --rm -v /var/run/docker.sock:/var/run/docker.sock:ro assaflavie/runlike"

docker ps

runlike YOUR-CONTAINER

Solution 2:

Use docker inspect:

$ docker inspect foo/bar
[
    {
        # …
        "Config": {
            # …
            "Cmd": [
                "/usr/local/bin/script.sh"
            ],
            # …
        }
    }
]

You can programatically parse this with jq:

$ docker inspect foo/bar | jq -r '.[0]["Config"]["Cmd"][0]'
/usr/local/bin/script.sh

Solution 3:

I wrote a simple Node-based CLI tool to generate a docker run command from an existing container.

https://www.npmjs.com/package/rekcod

Here's an example:

$ npm i -g rekcod
$ rekcod redis_container

docker run -d --name redis_container --restart always -h a44159e148e1 \
--expose 6379/tcp -e PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
-e REDIS_VERSION=3.0.7 -e REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-3.0.7.tar.gz \
-e REDIS_DOWNLOAD_SHA1=e56b4b7e033ae8dbf311f9191cf6fdf3ae974d1c \
--entrypoint "/entrypoint.sh" redis "redis-server"

Also handles links and mounted volumes and other stuff.

Not super robust at the moment, but handles more than some of the other things mentioned, and it was more of what I was looking for.

EDIT: In a single command, without installing any software:

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock nexdrew/rekcod redis_container

Solution 4:

A simpler (?) alternative is to run this docker inspect template, which uses the builtin Go templating capabilities to output a docker run compatible command. The template only covers the most usual command-line options, but it can easily be extended.

This solution has no dependencies on other tools, except docker itself.

docker inspect \
  --format "$(curl -s https://gist.githubusercontent.com/efrecon/8ce9c75d518b6eb863f667442d7bc679/raw/run.tpl)" \
  name_or_id_of_your_running_container