222

I use a third party GUI (Synology Docker package) to setup a docker container. However, its limitation makes me need to run the container from the command line. (I want to map another host ip to bind the port)

Now, since there are lots of settings that already done, I would like to retrieve the original run command that start this container, then I can change the port mapping port to new one. eg. "docker run -p 80:8080 gitlab"

I can't find the way to do so, event use "docker inspect", no such information provided.

Please provide some advice to solve this problem.

5
  • Possible duplicate of See full command of running/stopped container in Docker Apr 25, 2016 at 17:44
  • 13
    @capitalistpug that question is the exact opposite of this one - it's about the command inside the container, this is about the one used to launch the container. May 30, 2016 at 11:00
  • 2
    history | grep 'docker run'
    – Adrian May
    Nov 24, 2018 at 16:45
  • 7
    It is amazing Docker does not have an in-built option for this.
    – Macindows
    Mar 9, 2020 at 13:02
  • @AdrianMay Ah yes, posix to the rescue.
    – mtyson
    Jun 12, 2021 at 21:35

13 Answers 13

245

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
8
  • 2
    It's a little hard to setup runlike on Synology DSM, but I'll try it. Thank you!
    – Jack Yu
    Sep 27, 2015 at 8:10
  • 1
    I had to use sudo pip install runlike
    – Simon
    Feb 1, 2017 at 13:45
  • 39
    run like: docker run -v /var/run/docker.sock:/var/run/docker.sock \ assaflavie/runlike YOUR-CONTAINER
    – Tilo
    Mar 27, 2018 at 23:07
  • 1
    Thanks @Tilo. Works like a charm on my Synology NAS. Apr 21, 2018 at 8:21
  • 5
    Would be better to mount docker.sock read-only for security, ie: alias runlike="docker run --rm -v/var/run/docker.sock:/var/run/docker.sock:ro assaflavie/runlike"
    – Joao Costa
    Aug 17, 2021 at 9:16
155

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
9
  • 8
    This answer needs more upvotes, this really is the best solution !
    – Joao Costa
    Aug 17, 2021 at 9:09
  • Great answer. To be fair: It is around 4 years younger than the ones with more upvotes :) I really like, that it works without any external depedency!
    – KingCrunch
    Mar 15, 2022 at 21:15
  • How do I run this in Windows, i.e. Docker Desktop?
    – ontherocks
    Nov 4, 2022 at 16:52
  • 1
    Actually the template has been forked and updated to support the --mount options. See here: gist.github.com/efrecon/… - Updating answer to include it. Apr 5, 2023 at 14:24
  • 1
    @ontherocks Depends. If you are using WSL for the docker engine for instance, then open the WSL terminal where you could type docker ps -a for example. If you are not usgin WSL, then I'd suggest to install git bash gitforwindows.org which comes with a nice bash for windows. You can do docker commands there connecting to the same engine than Docker Desktop is connecting to. Apr 5, 2023 at 14:29
66

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
3
  • 3
    yes I prefer jq too, a slight tweak on the command (a bit easier to type and read IMHO) is: docker inspect <CONTAINER> | jq ".[0].Config.Cmd" Jan 14, 2019 at 3:19
  • 3
    "Cmd": null, Maybe this only works if the container was not started automatically be the docker daemon?
    – Michael
    Aug 5, 2019 at 10:23
  • 35
    This shows the CMD launched at container start. Definitely not what was asked.
    – nvidot
    Jan 13, 2021 at 14:57
40

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
2
  • Nice tool. Another alternative (in Python) is runlike. (I prefer NodeJS myself though, assuming feature parity)
    – Venryx
    Aug 29, 2021 at 0:23
  • I know its against policy to say thanks or +1 but fk that, you sir are a gentleman and a scholar and you saved me several hours of copy pasting with your beautiful tool
    – JSelser
    Mar 29, 2023 at 19:59
10

Use following command to get the arguments for all containers:

docker inspect -f "{{.Name}} {{.Path}} {{.Args}}" $(docker ps -a -q)
1
  • 6
    This lists the command and arguments for whats running inside container, not how the container was started (i.e. answer to the question you posted the runlike answer 4 minutes after this! :) ).
    – OzgurH
    Apr 24, 2019 at 10:43
6

If you do not want to install anything into your current running Docker server setup, you can simply execute (replace $CONTAINER_NAME with the container name you want to have the run arguments):

docker run -it --rm --volume /var/run/docker.sock:/var/run/docker.sock --privileged docker sh -c "apk add --no-cache nodejs nodejs-npm && npm i -g rekcod && rekcod $CONTAINER_NAME"

(for the rekcod method)

or

docker run -it --rm --volume /var/run/docker.sock:/var/run/docker.sock --privileged docker sh -c "apk add --no-cache py-pip && pip install runlike && runlike $CONTAINER_NAME"

(for the runlike method)

2
  • I think there should be a space between -it and --rm
    – IpsRich
    Sep 21, 2020 at 14:17
  • @IpsRich you're correct, I've modified my answer thanks!
    – Anthony O.
    Sep 21, 2020 at 14:32
5

Currently it seems we have to go 'docker inspect ' and then manually recreate the run command.

I have found someone attempting to write a bash script to do this: https://gist.github.com/miracle2k/c85b7b077fdb8d54bc89

but it is incomplete and depends on jq.

3

What could be a simpler (robust) option would be to use something like bash-preexec to capture commands that start with "docker run". You could then store these commands somewhere and retrieve them later.

For example, you could add something like this in your bash profile:

[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh
docker_run_history=~/.docker_run_history
docker_clear_history(){
    echo -n > $docker_run_history
}
docker_search_history(){
    search_for="$@"
    [[ -z $search_for ]] && search_for=".*"
    \cat $docker_run_history | grep "$search_for" | tail -1
}
docker_ps_mod(){
    for c in $(docker ps --format "{{.Image}}"); do 
        echo "Container $c was run using:"
        echo -e "\t$(docker_search_history $c)"
    done
}
docker_hook(){
    if [[ $@ =~ ^"docker run".*$ ]]; then
        \echo "$@" >> $docker_run_history 
    fi
}
preexec(){ 
    docker_hook $@
}

Then you could just run your things:

source ~/.bash_profile
docker run -it --rm -v $(pwd)/data:/data -p 8080:80 image
docker run -d daemon
docker_ps_mod

Which outputs:

Container image was run using:
    docker run -it --rm -v $(pwd)/data:/data -p 8080:80 image
Container daemon was run using:
    docker run -d daemon
3
  • Interesting alternative. +1
    – VonC
    Jun 19, 2017 at 9:05
  • 1
    It is absurd that docker does not record commands history and have a simply piping like this.
    – Macindows
    Mar 16, 2020 at 7:06
  • Interesting. But using the 'image' name seems a bit weird since multiple container might use the same image. It might be possible to use the value returned from the docker run ... command to get back the name of the container?
    – nvidot
    Jan 13, 2021 at 17:42
3

Curious how most answers rely on third parties for that while everything is in the docker inspect JSON. There’s even no need for jq since docker has a JSON formatting option.

Use

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

E.g. for the 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

the above docker inspect command 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 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.

2

That docker run command isn't specified in the Dockerfile or any other docker-related documents.

Either you find an example in the documentation associated to your container, or you can infer that docker run with (at least for the command and port mapping) a docker ps -a (but that won't give you the possible --volumes-from options)

Check also /usr/syno/etc/packages/Docker-GitLab/config

This differ from the gitlab config itself, which on Synology is available in /usr/syno/etc/packages/Docker/synology_gitlab.config

1
  • Thanks for mention the config file location of Synology Docker. But the config can't help much more. It's content is much the same as you export a container from Web UI.
    – Jack Yu
    Sep 27, 2015 at 7:50
1

To reverse the docker run command there is also the following npm package.

https://github.com/nexdrew/rekcod

2
  • 3
    Haha.. docker in reverse ==> rekcod
    – Macindows
    Mar 16, 2020 at 6:58
  • 1
    @Macindows Just a coincidence. ;)
    – Venryx
    Aug 29, 2021 at 0:24
1

As noted by @Chris_Lamb, you can use docker inspect to see this and much more. Here is how to load this info to a python dictionary:

import subprocess,json
im_inspect = subprocess.check_output(["docker", "image", "inspect", "datalab-nginx-ui"])
# print(im_inspect.decode("utf-8") ) #if you want to print it
d_inspect = json.loads(im_inspect)
d_inspect
d_inspect[-1]['ContainerConfig']['Cmd']

Example run: enter image description here

0

All docker files are here you can find cmd and mounts for example ls -la /proc/1 just cat it

cat /proc/1/cmdline
nginx: master process nginx -g daemon off;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.