How To List / Start / Stop Docker Containers

How to List, Start and Stop Docker Containers

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:

  1. How to list Docker containers.
  2. How to start Docker containers.
  3. 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:

How to List all docker containers

To list only running Docker containers, run the following command:

1
docker ps

Or

1
docker container ls

You should get the following output:

How to list running docker containers

To list only stopped Docker containers, run the following command:

1
docker container ls -f "status=exited"

You should get the following output:

How to list stopped dokcer containers

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:

How to list docker containers by ID

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 list docker containers by name

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:

How to start docker container

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:

How to start a new docker container

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:

How to stop all docker containers

Recommended Reading

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva