To get information about Docker containers in the terminal, you can use a variety of Docker command line interface (CLI) commands. Here’s a list of common commands that can help you retrieve information about your Docker containers:
1. List all containers
To see a list of all containers (both running and stopped), use:
bash
docker ps -a
To show only running containers:
bash
docker ps
2. Get detailed information about a specific container
To obtain detailed information about a specific container, use the command:
bash
docker inspect <container_id_or_name>
This command will return a JSON object with comprehensive details about the container, including its configuration, network settings, and volumes.
3. View container logs
To view the logs of a specific container, you can use:
bash
docker logs <container_id_or_name>
4. Display the resource usage of a container
To see resource usage statistics (CPU, memory, etc.) for a specific container:
bash
docker stats <container_id_or_name>
5. Get a quick summary of containers
For a brief summary of container status, you can use:
bash
docker container ls
This is equivalent to docker ps, showing only the running containers.
6. Get the exit code of a container
To see the exit code of a specific container (helpful for troubleshooting), use:
To open an interactive terminal in a running container, you can use:
bash
docker exec -it <container_id_or_name> /bin/bash
or, if the container has sh instead of bash:
bash
docker exec -it <container_id_or_name> /bin/sh
8. Check the environment variables set in a container
To see the environment variables defined in a container:
bash
docker exec <container_id_or_name> printenv
9. View the container's network information
To fetch detailed networking information of a container, you can use:
bash
docker network inspect <network_name> # To see all containers in a specific network
These commands will help you to manage and obtain information about Docker containers effectively. Make sure you have Docker installed and running on your machine to execute these commands.