docker
run command
container
tutorial
command-line

How to show the run command of a docker container

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Understanding Docker Containers

Before diving into viewing the run command of a Docker container, it's essential to understand what a Docker container is. A Docker container is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings. Containers are an abstraction layer on top of a host operating system that allow applications to run reliably across different computing environments.

Why Access the Run Command?

There are several reasons why you might want to see the run command of a Docker container:

  1. Debugging: When a container is not behaving as expected, reviewing the run command can help identify configuration issues.
  2. Documentation: Understanding how the container was started can be crucial for documentation purposes and knowledge transfer.
  3. Reproduction: To replicate the environment or set up a similar container with minor modifications.
  4. Security Auditing: Reviewing run commands for security compliance and vulnerabilities.

Viewing the Run Command

Using Docker Inspect

The primary method to view the run configuration of a Docker container is by using the docker inspect command. This command returns detailed information about a container in JSON format.

Basic Usage

To inspect a container, use the following command:

bash
docker inspect <container_id_or_name>

Example:

bash
docker inspect my_container

This command will output a JSON object containing all configuration details, including the entrypoint, command, environment variables, and more.

Extracting Specific Information

To extract the precise information related to the run command, particularly the entrypoint and arguments, you can use tools like jq to parse JSON data:

bash
docker inspect -f '{{json .Config}}' <container_id_or_name> | jq '.Entrypoint, .Cmd'

In this command:

  • -f '&#123;&#123;json .Config&#125;&#125;' formats the output to include only the configuration section.
  • jq is used to parse and format the JSON to display the entrypoint and command arrays.

Using Simpler Commands

While docker inspect provides comprehensive details, sometimes simpler output suffices. An alternative is using docker ps with formatting options:

bash
docker ps --format "{{.ID}}: {{.Names}}: {{.Command}}"

This outputs the ID, name, and run command for each container in a concise format.

Quick Reference

  • For the full configuration, run docker inspect CONTAINER_NAME.
  • For just the entrypoint and command, run docker inspect -f '{{json .Config}}' CONTAINER_NAME | jq '.Entrypoint, .Cmd'.
  • For a compact overview of running containers, run docker ps --format "{{.ID}}: {{.Names}}: {{.Command}}".
  • For environment variables, run docker inspect -f '{{.Config.Env}}' CONTAINER_NAME.

Additional Considerations

Environment Variables

Besides the command itself, it is crucial to note the environment variables, as they often influence the container’s behavior:

bash
docker inspect -f '{{.Config.Env}}' <container_id_or_name>

This command lists all environment variables set within the container's configuration.

Volumes and Networking

Often, the way a container is run is closely tied to its volume mounts and networking configuration:

  • Volumes: To see volume information, look at the "Mounts" section from the docker inspect output.
  • Networking: Check the "NetworkSettings" for information on ports and network configurations.

Conclusion

Knowing how to view a Docker container's run command is a crucial part of Docker literacy. Whether for debugging, replicating environments, or ensuring security compliance, mastering this aspect enhances your technical command over containerized applications. Use the tools and command strategies outlined in this article to bolster your Docker expertise and efficiency in managing containerized environments.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.