A Docker container is a lightweight and executable package of software that has everything you need to run an application, In simple term, a container is a running instance of an image. You can run multiple containers using the same image at the same time on the host operating system.
In this tutorial, we will learn the following:
- How to list Docker containers.
- How to start Docker containers.
- How to stop Docker containers.
If you want to host Docker on Cloud and don’t know how to choose the best Cloud hosting provider. You can read my guide on The 8 Best Docker Hosting Platforms in 2023.
Prerequisites
- A Linux system with Docker installed and running.
- A root user or user account with sudo privileges.
How to List Docker Containers
There are several commands to list Docker containers. The basic syntax to list Docker containers is shown below:
1 | docker ps [option] |
Or
1 | docker containers ls [option] |
To list both running and stopped Docker containers, run the following command:
1 | docker ps -a |
Or
1 | docker container ls -a |
You should get the following output:
To list only running Docker containers, run the following command:
1 | docker ps |
Or
1 | docker container ls |
You should get the following output:
To list only stopped Docker containers, run the following command:
1 | docker container ls -f "status=exited" |
You should get the following output:
To list all Docker containers by their ID, run the following command:
1 | docker ps -qa |
Or
1 | docker container ls -qa |
You should get the following output:
To list the latest created containers, run the following command:
1 | docker ps -l |
You should get the following output:
1 2 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ad6d2272092a nginx "/docker-entrypoint.…" 31 minutes ago Exited (0) 30 minutes ago nginx-container |
To list all containers by their size, run the following command:
1 | docker ps -s |
Or
1 | docker container ls -s |
You should get the following output:
1 2 3 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE 71c9525c6a07 httpd "httpd-foreground" 16 hours ago Up 16 hours 80 /tcp apacheweb-new 2B (virtual 138MB) 0f89f3dadc9b httpd "httpd-foreground" 17 hours ago Up 17 hours 0.0.0.0:8080->80 /tcp apacheweb 7B (virtual 138MB) |
To list all containers by their name and status, run the following command:
1 | docker container ls -- format 'table {{.Names}}\t{{.Status}}' |
You should get the following output:
How to Start Docker Container
The basic syntax to start a single or multiple stopped Docker container, use the following syntax:
1 | docker start [container-name] |
Or
1 | docker start [container- id ] |
For example, to start a container named nginx-container, run the following command:
1 | docker start nginx-container |
You should see the following output:
You can also start a container by specifying its ID. In this case, you will need to find the ID of the container using the “docker ps” command.
For example, to start a container with ID ad6d2272092a, run the following command:
1 | docker start ad6d2272092a |
To start multiple stopped containers named nginx-container and httpd-container, run the following command:
1 | docker start nginx-container httpd-container |
To start a new Docker container from an image, use the following syntax:
1 | docker run [options] [image-name] |
For example, to start a new Docker container in interactive mode, run the following command:
1 | docker run -itd --name=nginx-container nginx |
You should see the following output:
For more information of docker run command, please follow my guide on How to Use Docker Run Command with Examples.
How to Stop Docker Container
The basic syntax to stop a single or multiple running Docker container, use the following syntax:
1 | docker stop [container-name] |
Or
1 | docker stop [container- id ] |
For example, to stop the container named nginx-container, run the following command:
1 | docker stop nginx-container |
To stop all running containers, run the following command:
1 | docker stop $(docker ps -aq) |
You should see the following output:
Recommended Reading
- How to Stop / Remove all Docker Containers and Images
- The Ultimate Docker Cheat Sheet – Quick Reference
- How to Install and Use Docker Compose on Ubuntu 20.04 | 22.04
- How to Install and Use Docker on Ubuntu 20.04 | 22.04
- Docker Exec – How to Connect to a Docker Container
- How To Remove Docker Images, Containers & Volumes
- How to Use Docker Push & Pull Command with Examples
- How to Use Docker Run Command with Examples