How to show the run command of a docker container

2019-01-21 01:28发布

I use a third party GUI (Synology Docker package) to setup a docker container. However, it's 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.

标签: docker
10条回答
Lonely孤独者°
2楼-- · 2019-01-21 01:40

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-21 01:44

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

https://github.com/nexdrew/rekcod

查看更多
太酷不给撩
4楼-- · 2019-01-21 01:47

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
查看更多
仙女界的扛把子
5楼-- · 2019-01-21 01:50

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 \
    assaflavie/runlike YOUR-CONTAINER
查看更多
ら.Afraid
6楼-- · 2019-01-21 01:51

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

查看更多
看我几分像从前
7楼-- · 2019-01-21 01:53

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
查看更多
登录 后发表回答