See full command of running/stopped container in Docker
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Docker is a powerful platform for developing, shipping, and running applications in containers. Containers, a type of lightweight virtualization, are isolated environments that contain everything an application needs to run. During development and deployment, it's common to check the configuration and execution parameters of containers. An understanding of how to retrieve and interpret the full command of running or stopped containers is crucial for debugging, optimizing, and managing applications.
Understanding the Docker Command Structure
Docker commands generally have the following structure:
For working with running or stopped containers, the key command is ps, which lists containers. The ps command is usually enhanced with various options to display desired information, such as the full set of arguments used to start containers.
Using docker ps to View Container Information
The docker ps command provides a list of all active containers. By default, it shows information like container ID, name, image used, status, and ports, in a concise format. To view containers that are not currently running, you can use the -a or --all option:
Viewing the Full Command
To get deeper insights, such as the full command used to run a container, you can combine the docker ps command with formatting options. The --format option allows you to specify which fields to display and how:
To ensure you're viewing the entire command with all of its arguments, you might also use docker inspect, a command that provides detailed metadata about a container:
This command returns a JSON object with comprehensive details, but for the full startup command, focus on the Config.Cmd field. This field contains the command and arguments that were executed in the container.
Example
Suppose you want to know the full command used for a container running a web server. Here is an example using docker inspect:
This command will output something like:
This indicates that the NGINX process was started with the command nginx -g 'daemon off;'.
Advanced Techniques
Exporting and Analyzing Data
You might want to export this data for analysis or logging purposes. The docker inspect command can direct output to a file:
You can then use JSON parsing tools like jq for more detailed analysis. For example, to extract only the Cmd information:
Handling Long Command Outputs
When dealing with containers that have long command parameters, the command might be truncated in the ps display. This can be mitigated by using the terminal to increase character limits or using docker inspect for unabridged content.
Summary Table
Here's a table summarizing key Docker commands and options related to viewing container commands:
| Command/Option | Functionality | Example Usage |
docker ps | Lists running containers | docker ps |
docker ps -a | Lists all containers (running and stopped) | docker ps -a |
docker ps --format | Customizes displayed fields | docker ps --format "table {{.ID}}" |
docker inspect <id> | Provides detailed container metadata | docker inspect <container_id> |
--format '{{.Config.Cmd}}' | Extracts the command and arguments | docker inspect --format='{{.Config.Cmd}}' <id> |
jq | JSON parsing tool for detailed analysis | jq '.[0].Config.Cmd' container.json |
Additional Considerations
- Security: Always verify that you're accessing container commands and configurations in a secure manner. Exposure of sensitive command-line arguments can create security vulnerabilities.
- Performance: Checking command arguments can be part of performance troubleshooting. Ensure that containers have the right constraints and that no redundant arguments degrade performance.
- Version Consideration: Docker commands and output formats can slightly differ between versions. Always refer to the official Docker documentation corresponding to the version you are using.
By mastering these Docker command-line techniques, you enhance your ability to efficiently manage and troubleshoot containers, ensuring robust deployment workflows and application reliability.
Related reading
- SELinux is not supported with the overlay graph driver
- sending udp broadcast from a docker container
- Service selection from only one pod of one statefulset
- Set value in dependency of Helm chart
- Seeing escape characters when pressing the arrow keys in python shell
- Seeing partition doesn't exist warnings/failures after kafka using kafka partition re-assignment tool
- setting image pull policy using kubectl
- Setting up MySQL and importing dump within Dockerfile

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.