How To List / Start / Stop Docker Containers

Post Views: 508

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.

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 [option]

Or

?

1

docker containers [option]

To list both running and stopped Docker containers, run the following command:

?

1

docker -a

Or

?

1

docker container -a

You should get the following output:

How to List all docker containers

How to List all docker containers

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

?

1

docker

Or

?

1

docker container

You should get the following output:

How to list running docker containers

How to list running docker containers

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

?

1

docker container -f “status=exited”

You should get the following output:

How to list stopped dokcer containers

How to list stopped dokcer containers

To list all Docker containers by their ID, run the following command:

?

1

docker -qa

Or

?

1

docker container -qa

You should get the following output:

How to list docker containers by ID

How to list docker containers by ID

To list the latest created containers, run the following command:

?

1

docker -l

You should get the following output:

?

1

2

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMESad6d2272092a        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 -s

Or

?

1

docker container -s

You should get the following output:

?

1

2

3

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES               SIZE71c9525c6a07        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 –‘table {{.Names}}t{{.Status}}’

You should get the following output:

How to list docker containers by name

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-]

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

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

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-]

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 -aq)

You should see the following output:

How to stop all docker containers

How to stop all docker containers

You should also read the following articles

Leave a Comment

Your email address will not be published. Required fields are marked *