Docker container commands – list, run, and remove

Docker commands are split module-wise. The docker command follows the parent-child architecture. It means every docker command will have a parent and a child.

>> docker parent child

The docker image commands will come under the image module. The image commands will start with: docker image *

>> docker image ls

image – parent module or management command
ls – child module or command

The docker container commands will come under the container module. The container commands will start with: docker container *

>> docker container run

container – parent module or management command
run – child module or command

The help command will show the parent modules and child modules.

>> docker –help

It shows all the Parent modules in Management Commands and Child modules in Commands

Container commands

List

>> docker container ls

Displays all the running containers
Old Way to display list of running containers : docker ps

>> docker container ls -a

Displays all the running and stopped containers
Old Way to display list of containers : docker ps -a

Run

>> docker container run

Runs a command in a new container

Run the first container :

>> docker container run ubuntu cat /etc/os-release

ubuntu – It is a image name . If this ubuntu image is not available on local system then it fetches the image from docker repository .

cat /etc/os-release – This is a command which needs to be executed once the container is up . This command prints the OS related information .

>> docker container ls [ This command shows no containers ]

We have triggered the run command above, still no containers are displayed. Why?

Because in the above run command we started a container. Ran a cat command inside that container and we came out of the container and the container is stopped. Hence we are not able to see any container.

If we want to check the stopped container as well then go with the below command

>> docker container ls -a

Remove

>> docker container rm

Removes one or more containers ( -f option forces the removal of running container )

>> docker container rm Container_Id [ If it is a stopped container ]

>> docker container rm -f Container_Id [ If it is a running container ]

Note* We don’t have to give complete Container_Id just a unique sequence of the Container_Id is fine.

We can remove multiple containers as well

>> docker container rm Container_Id_1 Container_Id_2

In this article, we have covered docker commands to list, run and remove the container with examples. I hope you found this article interesting and valuable. Please share this article with your friends and help me grow. If you are having any concerns or questions about this article please comment below. If you want to get in touch with me please visit the Contact Me page and send me an email.