How to show the run command of a docker container

前端 未结 11 1238
萌比男神i
萌比男神i 2020-11-27 10:24

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

相关标签:
11条回答
  • 2020-11-27 10:27

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

    https://github.com/nexdrew/rekcod

    0 讨论(0)
  • 2020-11-27 10:30

    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;
    
    0 讨论(0)
  • 2020-11-27 10:37

    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.

    0 讨论(0)
  • 2020-11-27 10:40

    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
    
    0 讨论(0)
  • 2020-11-27 10:43

    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.

    0 讨论(0)
  • 2020-11-27 10:44

    Use following command to get the arguments for all containers docker inspect -f "{{.Name}} {{.Path}} {{.Args}}" $(docker ps -a -q)

    0 讨论(0)
提交回复
热议问题