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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Niklas9
  • 8,816
  • 8
  • 37
  • 60

6 Answers6

790

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

Scott S.
  • 8,040
  • 2
  • 14
  • 3
  • 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. – Dylan Smith Nov 18 '19 at 15:17
  • 1
    for the full command of only running containers just remove the all command. `docker ps --no-trunc` – Jacob Morris Feb 17 '20 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 '20 at 02: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 '20 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. – Aleksandr Erokhin Jan 06 '21 at 17:29
195

Use:

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
st0ne
  • 4,165
  • 2
  • 23
  • 24
  • 11
    This doesn't display the docker ps command. The docker ps command corresponds to docker inspect Path and Args. – JDiMatteo Sep 13 '16 at 22:08
  • 3
    No bueno as of Jan 2018 – s g Jan 09 '18 at 02:16
  • 6
    i.e. `docker inspect -f "{{.Name}} {{.Path}} {{.Args}}" $(docker ps -a -q)` – Paul Feb 26 '18 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 `$(`. – RandomInsano Apr 06 '18 at 04:00
  • and for those who want better understanding of the -f query, I've found a good explanation here https://container-solutions.com/docker-inspect-template-magic/ – intijk Mar 11 '19 at 02: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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JDiMatteo
  • 12,022
  • 5
  • 54
  • 65
  • How would you modify this to search for a specific command such as `kube-apiserver` ? – Jonathan May 22 '17 at 08:44
  • @Jonathan `docker inspect -f "{{.Path}} {{.Args}} ({{.Id}})" $(docker ps -a -q) | grep kube-apiserver` – rrw Sep 15 '17 at 03: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}}"'
davidxxx
  • 125,838
  • 23
  • 214
  • 215
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.

Gunar Gessner
  • 2,331
  • 23
  • 23
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>
Abhishek Jain
  • 3,815
  • 2
  • 26
  • 26
  • Doesn't work. Show "Command '['docker', 'inspect', u'06e6a369f909']' returned non-zero exit status 1" – fstang Mar 28 '19 at 01:26
  • Have you installed runlike as I mentioned – Abhishek Jain Mar 28 '19 at 07: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``` – Dylan Smith Nov 18 '19 at 15:19