Baeldung Pro – Ops – NPI EA (cat = Baeldung on Ops)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (cat=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Introduction

While troubleshooting containers in Docker, sometimes we need to view the command that was run inside the container. Luckily, Docker provides several ways to view complete commands of both stopped and running containers.

In this tutorial, we’ll explore how to view the command of containers using docker ps, docker inspect, and runlike. Additionally, we’ll examine the scenario where the command doesn’t contain complete information.

2. Using docker ps

First, let’s run an nginx container and try to view its command:

$ docker run -dp 8080:80 nginx:alpine
d00809d57....
admin@baeldung:~$ docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                   NAMES
d00809d57ae9   nginx:alpine   "/docker-entrypoint.…"   7 seconds ago   Up 7 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   confident_rubin

The normal output does not show the complete command. To solve this, docker ps has a –no-trunc option which tells the Docker client to display complete output related to the containers.

Here we’re viewing the complete command using the –no-trunc option:

$ docker ps -a --no-trunc
CONTAINER ID       IMAGE          COMMAND                                          CREATED         STATUS         PORTS                                   NAMES
d00809d57ae90...   nginx:alpine   "/docker-entrypoint.sh nginx -g 'daemon off;'"   4 minutes ago   Up 4 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   confident_rubin

However, the output contains more information than required. Let’s shrink it down to the information that we need:

$ docker ps -a --no-trunc --format "table{{.Names}}\t{{.CreatedAt}}\t{{.Command}}" 
NAMES             CREATED AT                      COMMAND
confident_rubin   2025-01-19 13:33:15 +0000 UTC   "/docker-entrypoint.sh nginx -g 'daemon off;'"

Although this method works in most cases, it doesn’t print the values of environment variables, if used in the command.

3. Using docker inspect

docker inspect is a more powerful subcommand that gives you a different insight into a container’s current state information, such as environment variables, which isn’t printed in the output of docker ps. It also supports go’s template engine which gives us the flexibility of viewing the information in our desired form.

Let’s view the entrypoint and the arguments of our nginx container using docker inspect:

$ docker inspect confident_rubin
[
    {
...
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
...
    }
]

The default output prints many things, let’s filter the output using jq:

$ docker inspect confident_rubin | jq '.[] | {Path,Args}'
{
  "Path": "/docker-entrypoint.sh",
  "Args": [
    "nginx",
    "-g",
    "daemon off;"
  ]
}

The docker inspect formatting template can be used for much more. To demonstrate this, let’s run an alpine:3.21 container with a few environment variables:

$ docker run -dit -e OS_RELEASE_PATH=/etc/os-release alpine:3.21 sh -c 'cat ${OS_RELEASE_PATH}'

Now let’s view the name, environment variables, and the complete command using the formatting template:

$ docker inspect trusting_cohen -f '{{.Name}} {{.Config.Env}} {{.Path}} {{.Args}}'
  fervent_hoover [OS_RELEASE_PATH=/etc/os-release PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin] sh [-c cat ${OS_RELEASE_PATH}]

Similarly, we can format the output of docker inspect and view the missing information related to the command.

4. Using runlike

runlike is an open-source tool that takes the ID or name of an existing docker container and prints the command line necessary to run a copy of it.

Let’s view the command for the alpine:3.21 container we ran earlier by running runlike directly as a docker container:

$ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock
  assaflavie/runlike trusting_cohen
docker run --name=trusting_cohen --hostname=90753df1e7f0 --env=OS_RELEASE_PATH=/etc/os-release --network=bridge --workdir=/ --runtime=runc -t alpine:3.21 sh -c 'cat ${OS_RELEASE_PATH}'

The output contains the complete command of the container, the environment variables, and other information related to the container.

5. Viewing /docker-entrypoint.sh Command

We often come across many containers that have only a /docker-entrypoint.sh script as the complete command for the container. Consequently, in such situations, we must manually view the command that the container runs.

Let’s take an example by running the mysql:8.0-debian image:

$ docker run -d -e MYSQL_ROOT_PASSWORD=passwd123 mysql:8.0-debian

Let’s view the complete command of this container using one of the methods we saw earlier:

$ docker ps --no-trunc --format "table{{.Names}}\t{{.Command}}\t{{.Image}}"
NAMES                  COMMAND                         IMAGE
pedantic_stonebraker   "docker-entrypoint.sh mysqld"   mysql:8.0-debian

It mentions the docker-entrypoint.sh script. In this scenario, we can check the init process of the container to view the complete command that the container ran:

$ docker exec pedantic_stonebraker ps -1
    PID TTY      STAT   TIME COMMAND
      1 ?        Ssl    0:43 mysqld

Some Linux distributions, such as Debian, don’t contain the ps command by default. In such cases, we can use /proc/1/cmdline:

$ docker exec pedantic_stonebraker cat /proc/1/cmdline
mysqld

Additionally, we can use tools like top or install ps to view the init process.

6. Conclusion

In this article, we saw how Docker, by default, supports viewing the complete commands of stopped and running containers.

While docker ps shows the complete command, it does not contain information related to environment variables. docker inspect, on the other hand, is a much more powerful subcommand to view environment variables as well as the complete commands.

Additionally, we saw how to use commands like tools like runlike to view the complete commands and the environment variables.

Finally, we saw how some containers have a special docker-entrypoint.sh script and how to view the complete command that was run by the entrypoint script.