394

How can I see the full command of a running container/process in Docker?

$ docker ps --all
CONTAINER ID    IMAGE          COMMAND                 CREATED          STATUS                     PORTS    NAMES
5b6291859b61    nginx:1.7.8    "nginx -g 'daemon of    4 minutes ago    Exited (0) 4 minutes ago            thirsty_brattain

I can only see "nginx -g 'daemon of".. here, not the full command.

1

6 Answers 6

792

docker ps --no-trunc will display the full command along with the other details of the running containers.

5
  • 5
    This didn't work for me. It gave me the command but not all the switches (which is what I think of when using "full command"). The runlike command mentioned other link above worked better for me. Nov 18, 2019 at 15:17
  • 1
    for the full command of only running containers just remove the all command. docker ps --no-trunc Feb 17, 2020 at 16:57
  • Thanks -- corrected. My previous command was for all containers and not just running containers which was the original question.
    – Scott S.
    Feb 19, 2020 at 2:20
  • The untruncated command can be very long, see only the first 400 characters each line with docker ps --all --no-trunc|cut -c-400
    – rubo77
    Apr 16, 2020 at 23:18
  • 3
    For a more convenient reading the command could be piped to less like so: docker ps --no-trunc | less -S. This removes line breaks and allows you to scroll horizontally. Jan 6, 2021 at 17:29
195

Use:

docker inspect -f "{{.Name}} {{.Config.Cmd}}" $(docker ps -a -q)

... it does a "docker inspect" for all containers.

5
  • 11
    This doesn't display the docker ps command. The docker ps command corresponds to docker inspect Path and Args.
    – JDiMatteo
    Sep 13, 2016 at 22:08
  • 3
    No bueno as of Jan 2018
    – s g
    Jan 9, 2018 at 2:16
  • 6
    i.e. docker inspect -f "{{.Name}} {{.Path}} {{.Args}}" $(docker ps -a -q)
    – Paul
    Feb 26, 2018 at 12:19
  • 2
    If you're just throwing sudo in front of the command, you'll get "docker inspect" requires at least 1 argument(s). because of the second call to get all of the container names, you'll probably want to add a sudo just inside the $(. Apr 6, 2018 at 4:00
  • and for those who want better understanding of the -f query, I've found a good explanation here container-solutions.com/docker-inspect-template-magic
    – intijk
    Mar 11, 2019 at 2:39
23

Use:

docker inspect -f "{{.Path}} {{.Args}} ({{.Id}})" $(docker ps -a -q)

That will display the command path and arguments, similar to docker ps.

2
  • How would you modify this to search for a specific command such as kube-apiserver ?
    – Jonathan
    May 22, 2017 at 8:44
  • @Jonathan docker inspect -f "{{.Path}} {{.Args}} ({{.Id}})" $(docker ps -a -q) | grep kube-apiserver
    – rrw
    Sep 15, 2017 at 3:18
22

TL-DR

docker ps --no-trunc and docker inspect CONTAINER provide the entrypoint executed to start the container, along the command passed to, but that may miss some parts such as ${ANY_VAR} because container environment variables are not printed as resolved.

To overcome that, docker inspect CONTAINER has an advantage because it also allow to retrieve separately env variables and their values defined in the container from the Config.Env property.

docker ps and docker inspect provide information about the executed entrypoint and its command. Often, that is a wrapper entrypoint script (.sh) and not the "real" program started by the container. To get information on that, requesting process information with ps or /proc/1/cmdline help.


1) docker ps --no-trunc

It prints the entrypoint and the command executed for all running containers. While it prints the command passed to the entrypoint (if we pass that), it doesn't show value of docker env variables (such as $FOO or ${FOO}).
If our containers use env variables, it may be not enough.

For example, run an alpine container :

docker run --name alpine-example -e MY_VAR=/var alpine:latest sh -c 'ls $MY_VAR'

When use docker -ps such as :

docker ps -a --filter name=alpine-example --no-trunc

It prints :

CONTAINER ID           IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES
5b064a6de6d8417...   alpine:latest       "sh -c 'ls $MY_VAR'"   2 minutes ago       Exited (0) 2 minutes ago                       alpine-example

We see the command passed to the entrypoint : sh -c 'ls $MY_VAR' but $MY_VAR is indeed not resolved.

2) docker inspect CONTAINER

When we inspect the alpine-example container :

docker inspect alpine-example | grep -4 Cmd

The command is also there but we don't still see the env variable value :

        "Cmd": [
            "sh",
            "-c",
            "ls $MY_VAR"
        ],

In fact, we could not see interpolated variables with these docker commands.
While as a trade-off, we could display separately both command and env variables for a container with docker inspect :

docker inspect  alpine-example  | grep -4 -E "Cmd|Env"

That prints :

        "Env": [
            "MY_VAR=/var",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "Cmd": [
            "sh",
            "-c",
            "ls $MY_VAR"
        ]

A more docker way would be to use the --format flag of docker inspect that allows to specify JSON attributes to render :

docker inspect --format '{{.Name}} {{.Config.Cmd}}  {{ (.Config.Env) }}'  alpine-example

That outputs :

/alpine-example [sh -c ls $MY_VAR]  [MY_VAR=/var PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]

3) Retrieve the started process from the container itself for running containers

The entrypoint and command executed by docker may be helpful but in some cases, it is not enough because that is "only" a wrapper entrypoint script (.sh) that is responsible to start the real/core process.
For example when I run a Nexus container, the command executed and shown to run the container is "sh -c ${SONATYPE_DIR}/start-nexus-repository-manager.sh".
For PostgreSQL that is "docker-entrypoint.sh postgres".

To get more information, we could execute on a running container docker exec CONTAINER ps aux.
It may print other processes that may not interest us.
To narrow to the initial process launched by the entrypoint, we could do :

docker exec CONTAINER ps -1

I specify 1 because the process executed by the entrypoint is generally the one with the 1 id.

Without ps, we could still find the information in /proc/1/cmdline (in most of Linux distros but not all). For example :

docker exec CONTAINER cat /proc/1/cmdline | sed -e "s/\x00/ /g"; echo    

If we have access to the docker host that started the container, another alternative to get the full command of the process executed by the entrypoint is : : execute ps -PID where PID is the local process created by the Docker daemon to run the container such as :

ps -$(docker container inspect --format '{{.State.Pid}}'  CONTAINER)

User-friendly formatting with docker ps

docker ps --no-trunc is not always easy to read.
Specifying columns to print and in a tabular format may make it better :

docker ps   --no-trunc  --format "table{{.Names}}\t{{.CreatedAt}}\t{{.Command}}"

Create an alias may help :

alias dps='docker ps   --no-trunc  --format "table{{.Names}}\t{{.CreatedAt}}\t{{.Command}}"'
1
19

Moving Dylan's comment into a full-blown answer because TOO USEFUL:

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

What does it do? Runs https://github.com/lavie/runlike inside a container, gets you the complete docker run command, then removes the container for you.

17

Use runlike from git repository https://github.com/lavie/runlike

To install runlike

pip install runlike

As it accept container id as an argument so to extract container id use following command

docker ps -a -q

You are good to use runlike to extract complete docker run command with following command

runlike <docker container ID>
3
  • Doesn't work. Show "Command '['docker', 'inspect', u'06e6a369f909']' returned non-zero exit status 1"
    – fstang
    Mar 28, 2019 at 1:26
  • Have you installed runlike as I mentioned Mar 28, 2019 at 7:33
  • 11
    Even better you can run runlike within a docker container and avoid installing it: docker run --rm -v /var/run/docker.sock:/var/run/docker.sock assaflavie/runlike YOUR-CONTAINER Nov 18, 2019 at 15:19

Your Answer

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

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