How To Get Docker Run Command From Running Container

As a developer, I often find myself in situations where I need to inspect or troubleshoot running containers in Docker. One crucial piece of information that can be extremely helpful in these scenarios is the original docker run command used to create the container. Fortunately, Docker provides us with a few different methods to retrieve this information, allowing us to understand how the container was initially configured.

Method 1: Using Docker Inspect

The first method involves using the docker inspect command-line tool. This tool provides a wealth of information about Docker resources, including containers. To retrieve the docker run command from a running container, follow these steps:

  1. First, identify the container ID or name of the running container you’re interested in.
  2. Next, open your terminal and run the following command:

docker inspect --format='{{.Config.Cmd}}' CONTAINER_ID_OR_NAME

Make sure to replace CONTAINER_ID_OR_NAME with the actual ID or name of the container you want to inspect. This command will display the docker run command that was used to create the container, including any arguments and options.

Method 2: Using Docker History

Another method to obtain the docker run command from a running container is by using the docker history command. This method provides a different perspective by showing the history of changes made to the container’s image.

  1. Similar to the previous method, start by identifying the container ID or name.
  2. Open your terminal and run the following command:

docker history --no-trunc CONTAINER_ID_OR_NAME

Replace CONTAINER_ID_OR_NAME with the actual ID or name of the container you want to inspect. The --no-trunc flag ensures that the full command is displayed, even if it exceeds the default output limit. The docker history command will display a list of layers in the container’s image, along with the command executed for each layer.

By examining the bottom-most layer, you can obtain the docker run command that was used to create the container.

Method 3: Using Container’s Environment Variables

In some cases, the docker run command may not be easily accessible using the previous methods. However, if the container was launched with environment variables, you can often extract valuable information from them, including parts of the original run command.

  1. Again, begin by identifying the container ID or name.
  2. Access the container’s environment variables by running the following command:

docker inspect --format='{{range $index, $value := .Config.Env}}{{println $value}}{{end}}' CONTAINER_ID_OR_NAME

Replace CONTAINER_ID_OR_NAME with the actual ID or name of the container you want to inspect. This command will display a list of all environment variables defined within the container.

Scan through the list and look for any variables that resemble Docker command-line options and arguments. This could include variables such as DOCKER_OPTS or DOCKER_CMD. Extract the relevant information and use it to reconstruct the original docker run command.

Conclusion

Retrieving the docker run command from a running container can be immensely useful when troubleshooting or understanding the container’s configuration. By using the docker inspect, docker history, or examining the container’s environment variables, we can gain valuable insights into how the container was initially created.

These methods provide different approaches to uncovering the docker run command, allowing us to dive deep into the container’s setup and assist in debugging or recreating the container if necessary. Next time you need to analyze a running Docker container, give these techniques a try!