How to List Containers in Docker

How to List Containers in Docker

Docker is a containerization platform that allows you to quickly build, test, and deploy applications as portable, self-sufficient containers that can run virtually anywhere. It is the de-facto standard for container deployment, and it is an essential tool for DevOps engineers and their continuous integration and delivery pipeline. In this post, we'll explain how to list Docker containers

Docker is a containerization platform that allows you to quickly build, test, and deploy applications as portable, self-sufficient containers that can run virtually anywhere. It is the de-facto standard for container deployment, and it is an essential tool for DevOps engineers and their continuous integration and delivery pipeline. In this post, we'll explain how to list Docker containers.

List Docker Containers

The Docker command for listing containers takes the following form:

docker container ls [options]

Older Docker versions before 1.13 are using a different command to list the containers:

docker ps [options]

The command above is still supported in newer Docker versions where the ps command is an alias to container ls.

To list the running container run the docker container ls command without any option:

docker container ls

The output will look something like this:

CONTAINER ID    IMAGE        COMMAND                  CREATED        STATUS        PORTS       NAMES
c8bded53da86    postgres     "docker-entrypoint.s…"   2 hours ago    Up 2 hours    5432/tcp    pg
571c3a115fcf    redis        "docker-entrypoint.s…"   4 hours ago    Up 4 hours    6379/tcp    cache
05ef6d8680ba    nginx        "nginx -g 'daemon of…"   2 hours ago    Up 2 hours    80/tcp      web

Each line of the output includes the following columns:

  • Container ID – A unique alphanumeric string that identifies each container.
  • Image – The Docker image used to create the container.
  • Command – The command that is executed when starting the container.
  • Created – The creation time of the container.
  • Status – The status of the container.
  • Ports – The container's published ports.
  • Name – The name of the container.

If there are no running containers, only the header line is displayed.

The -a, --all option tells docker container ls to print a list of all containers:

docker container ls -a

CONTAINER ID    IMAGE        COMMAND                  CREATED        STATUS                    PORTS       NAMES
b28cbaa91f15    couchbase    "/entrypoint.sh couc…"   5 hours ago    Exited (0) 3 hours ago                db
c8bded53da86    postgres     "docker-entrypoint.s…"   2 hours ago    Up 2 hours                5432/tcp    pg
571c3a115fcf    redis        "docker-entrypoint.s…"   4 hours ago    Up 4 hours                6379/tcp    cache
05ef6d8680ba    nginx        "nginx -g 'daemon of…"   2 hours ago    Up 2 hours                80/tcp      web

By default, columns with a length exceeding a specified limit are truncated. Use the --no-trunc option to disable truncation:

docker container ls --no-trunc

To only display the containers’ IDs pass the -q, --quiet option:

docker container ls -q

c8bded53da86
571c3a115fcf
05ef6d8680ba

The --format allows you to format the output using a Go template. For example to print only the containers’ names and status including the header you would run:

docker container ls --format 'table {{.Names}}\t{{.Status}}'

NAMES    STATUS
pg       Up 2 hours
cache    Up 4 hours
web      Up 2 hours

Use the -s, --size option to view the size of the containers:

docker container ls -s

Each line will include a column named SIZE that shows the container size:

CONTAINER ID    IMAGE        COMMAND                  CREATED        STATUS        PORTS       NAMES    SIZE
c8bded53da86    postgres     "docker-entrypoint.s…"   2 hours ago    Up 2 hours    5432/tcp    pg       63B (virtual 394MB)
571c3a115fcf    redis        "docker-entrypoint.s…"   4 hours ago    Up 4 hours    6379/tcp    cache    0B (virtual 98.2MB)
05ef6d8680ba    nginx        "nginx -g 'daemon of…"   2 hours ago    Up 2 hours    80/tcp      web      2B (virtual 126MB)

The --last, -n option tells the command to display n last created containers, including all states. For example, to view the latest two created containers you would run:

docker container ls -n 2

CONTAINER ID    IMAGE        COMMAND                  CREATED        STATUS                    PORTS       NAMES
b28cbaa91f15    couchbase    "/entrypoint.sh couc…"   5 hours ago    Exited (0) 3 hours ago                db
c8bded53da86    postgres     "docker-entrypoint.s…"   2 hours ago    Up 2 hours                5432/tcp    pg

There is also an option to list only the latest created container --latest , -l which is same as -n 1:

docker container ls -l

The --filter, -f option allows you to filter the output based on certain criteria. For example, to view only the containers with status exited you would run:

docker container ls -f "status=exited"

CONTAINER ID    IMAGE        COMMAND                  CREATED        STATUS                    PORTS       NAMES
b28cbaa91f15    couchbase    "/entrypoint.sh couc…"   5 hours ago    Exited (0) 3 hours ago                db

For a list of all supported filters check the Docker documentation

Conclusion

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.

If you have any questions, please leave a comment below. Thank you for reading !

List all containers in Docker(Docker command)

List all containers in Docker(Docker command)

We can get a list of all containers in docker using `docker container list` or `docker ps` commands.

We can get a list of all containers in docker using docker container list or docker ps commands.

List Docker Containers

To list down docker containers we can use below two commands

  • docker container list
  • docker ps

docker container ls command introduced in docker 1.13 version. In older versions we have to use docker ps command.

List all Containers in docker, using docker ls command

The below command returns a list of all containers in docker.

docker container list -all

or

docker container ls -all

List all containers in docker, using docker ps command

In older version of docker we can use docker ps command to list all containers in docker.

$ docker ps -all

or

$ docker ps -a

List all Running docker containers

The default docker container ls command shows all running docker containers.

$ docker container list

or

$ docker container ls

or

To get list of all running docker containers use the below command

$ docker ps

List all stopped docker containers command

To get list of all stopped containers in docker use the below commands

$ docker container list -f "status=exited"

or

$ docker container ls -f "status=exited"

or you can use docker ps command

$ docker ps -f "status=exited"

List all latest created docker containers

To list out all latest created containers in docker use the below command.

$ docker container list --latest

Show n last created docker containers

To display n last created containers in docker use the below command.

$ docker container list --last=n

How to use `docker container run` command to run Docker containers?

How to use `docker container run` command to run Docker containers?

In this Docker Run Command tutorial, you'll learn how to use `docker container run` command to create and run Docker containers.

Docker is a platform that allows you to develop, test, and deploy applications as portable, self-sufficient containers that run virtually anywhere.

The docker run command creates a container from a given image and starts the container using a given command. It is one of the first commands you should become familiar with when starting to work with Docker.

In this article, we’ll use the official Nginx image to show various ways to run a Docker container.

Docker Run Command

The docker run command takes the following form:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

The name of the image from which the container should be created is the only required argument for the docker run command. If the image is not present on the local system, it is pulled from the registry.

If no command is specified, the command specified in the Dockerfile’s CMD or ENTRYPOINT instructions is executed when running the container.

Starting from version 1.13, the Docker CLI has been restructured, and all commands have been grouped under the object they interacting with.

Since the run command interacts with containers, now it is a subcommand of docker container. The syntax of the new command is as follows:

docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]

The old, pre 1.13 syntax is still supported. Under the hood, docker run command is an alias to docker container run. Users are encouraged to use the new command syntax.

A list of all docker container run options can be found on the Docker documentation page.

Run the Container in the Foreground

By default, when no option is provided to the docker run command, the root process is started in the foreground. This means that the standard input, output, and error from the root process are attached to the terminal session.

docker container run nginx

The output of the nginx process will be displayed on your terminal. Since, there are no connections to the webserver, the terminal is empty.

To stop the container, terminate the running Nginx process by pressing CTRL+C.

Run the Container in Detached Mode

To keep the container running when you exit the terminal session, start it in a detached mode. .

Use the -d option to start a detached container:

docker container run -d nginx
050e72d8567a3ec1e66370350b0069ab5219614f9701f63fcf02e8c8689f04fa

The detached container will stop when the root process is terminated.

To attach your terminal to the detached container root process, use the docker container attach command.

Remove the Container After Exit

By default, when the container exits, its file system persists on the host system.

The --rm options tells docker run command to remove the container when it exits automatically:

docker container run --rm nginx

The Nginx image may not be the best example to clean up the container’s file system after the container exits. This option is usually used on foreground containers that perform short-term tasks such as tests or database backups.

Set the Container Name

In Docker, each container is identified by its UUID and name. By default, if not explicitly set, the container’s name is automatically generated by the Docker daemon.

Use the --name option to assign a custom name to the container:

docker container run -d --name my_nginx nginx

The container name must be unique. If you try to start another container with the same name, you’ll get an error similar to this:

docker: Error response from daemon: Conflict. The container name "/my_nginx" is already in use by container "9...c". You have to remove (or rename) that container to be able to reuse that name.

Run docker container ls -a to list all containers, and see their names:

docker container ls
CONTAINER ID  IMAGE  COMMAND                 CREATED         STATUS         PORTS   NAMES
9d695c1f5ef4  nginx  "nginx -g 'daemon of…"  36 seconds ago  Up 35 seconds  80/tcp  my_nginx

The meaningful names are useful to reference the container within a Docker network or when running docker CLI commands.

Publishing Container Ports

By default, if no ports are published, the process running in the container is accessible only from inside the container.

Publishing ports means mapping container ports to the host machine ports so that the ports are available to services outside of Docker.

To publish a port use the -p options as follows:

-p host_ip:host_port:container_port/protocol
  • If no host_ip is specified, it defaults to 0.0.0.0.
  • If no protocol is specified, it defaults to TCP.
  • To publish multiple ports, use multiple -p options.

To map the TCP port 80 (nginx) in the container to port 8080 on the host localhost interface, you would run:

docker container run --name web_server -d -p 8080:80 nginx

You can verify that the port is published by opening http://localhost:8080 in your browser or running the following curl command on the Docker host:

curl -I http://localhost:8080

The output will look something like this:

HTTP/1.1 200 OK
Server: nginx/1.17.6
Date: Tue, 26 Nov 2019 22:55:59 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 19 Nov 2019 12:50:08 GMT
Connection: keep-alive
ETag: "5dd3e500-264"
Accept-Ranges: bytes
Sharing Data (Mounting Volumes)

When a container is stopped, all data generated by the container is removed. Docker Volumes are the preferred way to make the data persist and share it across multiple containers.

To create and manage volumes, use the -p options as follows:

-v host_src:container_dest:options
  • The host_src can be an absolute path to a file or directory on the host or a named volume.
  • The container_dest is an absolute path to a file or directory on the container.
  • Options can be rw (read-write) and ro (read-only). If no option is specified, it defaults to rw.

To explain how this works, let’s create a directory on the host and put an index.html file in it:

mkdir public_html
echo "Testing Docker Volumes" > public_html/index.html

Next, mount the public_html directory into /usr/share/nginx/html in the container:

docker run --name web_server -d -p 8080:80 -v $(pwd)/public_html:/usr/share/nginx/html nginx

Instead of specifying the absolute path to the public_html directory, we’re using the $(pwd) command, which prints the current working directory.

Now, if you type http://localhost:8080 in your browser, you should see the contents of the index.html file. You can also use curl:

curl http://localhost:8080
Testing Docker Volumes
Run the Container Interactively

When dealing with the interactive processes like bash, use the -i and -t options to start the container.

The -it options tells Docker to keep the standard input attached to the terminal and allocate a pseudo-tty:

docker container run -it nginx /bin/bash

The container’s Bash shell will be attached to the terminal, and the command prompt will change:

[email protected]:/#

Now, you can interact with the container’s shell and run any command inside of it.

In this example, we provided a command (/bin/bash) as an argument to the docker run command that was executed instead of the one specified in the Dockerfile.

Conclusion

Docker is the standard for packaging and deploying applications and an essential component of CI/CD, automation, and DevOps.

The docker container run command is used to create and run Docker containers.

If you have any questions, please leave a comment below.

Docker Container Full Course - Docker Tutorial For Beginners

Docker Container Full Course - Docker Tutorial For Beginners

This Docker Container Full Course video will help you understand and learn containerization in detail. This Docker tutorial is ideal for both beginners as well as professionals who want to master the container concepts.

This Docker Container Full Course video will help you understand and learn containerization in detail. This Docker tutorial is ideal for both beginners as well as professionals who want to master the container concepts. Below are the topics covered in this Docker tutorial for beginners video:

1:33 Introduction to Docker
1:38 Problems before Docker
4:28 How Docker solves the problem?
5:48 What is Docker?
6:38 Docker in a Nutshell
8:03 Docker Examples
9:40 Docker Case Study Indiana University
13:00 Docker Registry
14:30 Docker Images & Containers
18:45 Docker Compose
19:05 Install & Setup Docker
19:10 Install Docker
22:15 Docker for Windows
22:25 Why use Docker for Windows?
25:30 Docker For Windows Demo
39:52 DokerFile & Commands
39:57 DockerFile Syntax
41:22 DockerFile Commands
48:52 Creating an Image to Install Apache Web Server
56:57 DockerFile for Installing Nginx
1:03:37 Docker Commands
1:03:42 Most Used Docker Commands
1:04:42 Basic Docker Commands
1:45:47 Advanced Docler Commands
1:58:02 Docker Compose & Swarm
1:58:07 Docker Compose
1:58:12 What is Docker Compose?
2:03:27 What is MEAN Stack Application?
2:04:12 Demo
2:21:02 Docker Swarm
2:21:07 What is Docker Swarm?
2:28:37 Demo
2:32:42 Docker Swarm Commands
3:03:47 Docker Networking
3:05:02 Goals of Docker Networking
3:06:42 Container Network Model
3:08:27 Container Network Model Objects
3:11:27 Network Drivers
3:11:42 Network Driver: Bridge
3:12:22 Network Driver: Host
3:13:02 Network Driver: None
3:13:17 Network Driver: Overlay
3:14:22 Network Driver: Macvlan
3:14:57 Docker Swarm
3:15:22 Docker Swarm Clusters
3:15:47 Docker Swarm: Managers & Clusters
3:16:32 Hands-On -1
3:19:02 Hands-On - 2
3:28:42 Dockerizing Applications
3:29:42 What is Angular?
3:32:42 What is DevOps?
3:33:12 DevOps Tools & Techniques
3:34:12 Deploying an Angular Applications
3:37:17 Demo
4:01:12 Docker Jenkins
4:02:17 What is Jenkins?
4:03:40 How Jenkins Works?
4:04:40 Containerisation vs Virtualization
4:07:50 Docker Use-case
4:08::50 what are Microservices?
4:10:35 What are Microservices?
4:11:05 Advantages of Microservice Architecture
4:12:15 VMs vs Docker Containers for Microservices
4:13:35 Use Case
4:23:50 Node.js Docker
4:24:00 Why Use Node.js with Docker?
4:24:40 Demo: Node.js with Docker
4:38:00 Docker vs VM
4:38:30 Virtual Machines
4:38:35 What is a Virtual Machine?
4:39:25 Benefits of Virtual Machines
4:39:45 Popular Virtual Machine Providers
4:40:00 Docker Containers
4:40:05 What is Docker Container?
4:41:30 Types of Containers
4:42:10 Benefits of Containers
4:42:55 Major Differences
4:46:30 Use-case
4:46:45 How PayPal uses Docker & VM?
4:50:45 Docker Swarm vs Kubernetes
4:53:25 Installation & Cluster Configuration
4:55:00 GUI
4:56:30 Scalability
4:57:00 Auto-scaling
4:58:20 Load Balancing
4:59:25 Rolling Updates & Rollbacks
5:01:35 Data Volumes
5:02:10 Logging & Monitoring
5:03:35 Demo
5:14:55 Kubernetes vs Docker Swarm Mindshare