Docker List Containers
Docker List Containers: Docker is a containerization platform that can run virtually from anywhere. Using Docker you are able to quickly build, test, and deploy applications as portable, self-sufficient containers. The container deployment is based on a de-facto standard. It is an indispensable tool for DevOps engineers that supports the features of continuous integration and delivery pipeline. This blog mainly covers the topics concerning how to list Docker containers, start and stop the containers by using docker commands.
Docker List Containers
For listing the containers the following Docker command will be used:
docker container ls [options]
Older Docker versions before 1.13 are using a different command to list the containers:
docker ps [options]
Enter the following command to list all running Docker containers:
$ docker ps
IMAGE
To list all containers, both running and stopped, add –a :
$ docker ps –a
IMAGE
To list containers by their ID use –aq (quiet):
$ docker ps –aq
IMAGE
To list the total file size of each container, use –s (size):
$ docker ps –s
To list the latest created containers, use –l (latest):
$ docker ps –l
IMAGE
The command above is still supported in newer Docker versions where the ps command is an alias to container ls.
Docker Training
- Master Your Craft
- Lifetime LMS & Faculty Access
- 24/7 online expert support
- Real-world & Project Based Learning
To list the running containers, execute the docker container ls command without providing any option:
docker container ls
The output will appear similar to this:
CONTAINER ID IMAGE COMMAND CREATED
C8bded53da86 postgres "docker-entrypoint.s…" 2 hours ago
571c3a115fcf redis "docker-entrypoint.s…" 4 hours ago
05ef6d8680ba nginx "nginx -g 'daemon of…" 2 hours ago
STATUS PORTS NAMES
Up 2 hours 5432/tcp pg
Up 4 hours 6379/tcp cache
Up 2 hours 80/tcp web
Each line of the output comprises the following columns:
- Container ID – An ID that identifies each container with its unique alphanumeric string.
- Image – This is a Docker image used for creating a container.
- Command – The command that is executed when starting the container.
- Created – This notifies about the container at which time it is created.
- Status – This notifies about the status of the container.
- Ports – This notifies about the published ports of the container.
- Name – This specifies the name of the container.
If there are no running containers, only the header line is displayed.
The options “-a”, “--all” conveys the docker container ls to print a list of all containers:
$ docker container ls -a
Output:
CONTAINER ID IMAGE COMMAND CREATED
b28cbaa91f15 couchbase "/entrypoint.sh couc…" 5 hours ago
c8bded53da86 postgres "docker-entrypoint.s…" 2 hours ago
571c3a115fcf redis "docker-entrypoint.s…" 4 hours ago
05ef6d8680ba nginx "nginx -g 'daemon of…" 2 hours ago
STATUS PORTS NAMES
Exited (0) 3 hours ago db
Up 2 hours 5432/tcp pg
Up 4 hours 6379/tcp cache
Up 2 hours 80/tcp web
By default, columns with a length exceeding a specified limit are truncated. Use the option “--no-trunc” to disable truncation:
$ docker container ls --no-trunc
To display only IDs of containers, you need to pass the option “-q, --quiet”:
$ docker container ls -q
Output:
c8bded53da86
571c3a115fcf
05ef6d8680ba
The output is formatted using the option “--format” by using a Go template. For instance, to print only the names and status of containers', including the header, you should run the following command:
$ docker container ls --format 'table {{.Names}}\t{{.Status}}'
Output:
NAMES STATUS
pg Up 2 hours
cache Up 4 hours
web Up 2 hours
The size of containers is viewed using the options “-s, --size”.
docker container ls -s
Subscribe to our youtube channel to get new updates..!
The name SIZE will be included in each line that displays the container size:
Output:
CONTAINER ID IMAGE COMMAND CREATED
c8bded53da86 postgres "docker-entrypoint.s…" 2 hours ago
571c3a115fcf redis "docker-entrypoint.s…" 4 hours ago
05ef6d8680ba nginx "nginx -g 'daemon of…" 2 hours ago
STATUS PORTS NAMES SIZE
Up 2 hours 5432/tcp pg 63B (virtual 394MB)
Up 4 hours 6379/tcp cache 0B (virtual 98.2MB)
Up 2 hours 80/tcp web 2B (virtual 126MB)
The options “--last, -n” conveys the command to display “n” last created containers, including all states. For instance, to view the latest two created containers, you would run the following command:
$ docker container ls -n 2
Output:
CONTAINER ID IMAGE COMMAND CREATED
b28cbaa91f15 couchbase "/entrypoint.sh couc…" 5 hours ago
c8bded53da86 postgres "docker-entrypoint.s…" 2 hours ago
STATUS PORTS NAMES
Exited (0) 3 hours ago db
Up 2 hours 5432/tcp pg
There is also an option to list only the latest created container --latest, -l which is the same as -n 1:
$ docker container ls -l
The --filter, -f option allows you to filter the output based on certain criteria.
For instance, to view only the containers with status exited, you would run:
$ docker container ls -f "status=exited"
Output:
CONTAINER ID IMAGE COMMAND CREATED
b28cbaa91f15 couchbase "/entrypoint.sh couc…" 5 hours ago
STATUS PORTS NAMES
Exited (0) 3 hours ago db
Start Docker Container
The containers are launched by using the docker commands. The main command which is used to launch or start a single or multiple stopped Docker containers is docker start:
$ docker start [options] container_id
IMAGE
You can specify the container by using its name or ID. To create a new container from an image and start it, use docker run:
$ docker run [options] image [command] [argument]
If a name is not defined for your newly created container, the daemon will automatically generate a random string name. To define the container name, use the ––name option:
docker run ––name=Ubuntu_Test ubuntu:14.04
The command which is specified above will create the “Ubuntu_test” container based on the “ubuntu:14.04” image and start it.
A container may be running, but you may not be able to interact with it. To start the container in interactive mode, use the –i and –t options:
$ docker run –it ––name=Ubuntu_Test ubuntu:14.04
IMAGE
In the above example, the system creates the “Test_2” container from the “ubuntu” image and connects to it enabling you to run commands directly on the container.
Use the attach command instead of using -i or -t options to connect to a running container:
docker attach container_id
Stop Docker Container
You can stop the container by passing the docker stop command as follows:
$ docker stop [option] container_id
Replace “container_id” with the container’s name or ID.
By default, you will get a grace period of 10 seconds. The “stop” command instructs the container to stop services after that period. Use the “--time” option to define a different grace period expressed in seconds:
$ docker stop --time=20 container_id
IMAGE
To kill a docker container immediately without waiting for the grace period to end-use, you can use the following command:
$ docker kill [option] container_id
To stop all running containers, enter the following command:
$ docker stop $(docker ps –a –q)
The same command could be used with the kill. This would stop all containers without giving them a chance to exit.
Conclusion Docker List Containers:
A Docker container is a standalone runtime instance of an image. To list Docker containers, use the “docker container ls” command or its alias “docker ps”. In this blog, we have explained various options to list, start, and stop, Docker containers. The development teams use Docker to ensure consistency across different machines.
Categories
- Azure DevOps Tutorial
- DevOps Lifecycle
- DevOps Skills
- Python For DevOps
- DevOps Periodic Table
- DevOps Tutorial
- Azure Pipelines
- Continuous Delivery vs Continuous Deployment
- Chef vs Ansible
- DevOps Testing Tools
- Azure Data Factory Tutorial
- Linux Commands For Devops
- DevOps Prerequisites
- DevOps Tools
- How to Become a DevOps Engineer
- DevOps Certification
- What is Puppet in DevOps
- DevOps vs Agile
- DevOps Engineer Skills
- What is Azure DevOps
- Chef vs Puppet
- What Does a DevOps Engineer Do
- DevOps Engineer Roles and Responsibilities
- Azure DevOps Certification
- Azure DevOps Interview Questions
- DevOps Interview Questions