How to List, Start, Stop, and Delete Docker Containers?

Photo of author

By admin

Whenever a product is designed, the only goal remains to reduce the inconvenience faced by the users. But, there are certain technical issues that most probably occur whenever a developer designs any application. Think like this, you’re working on a particular project, and the application seems to be running absolutely fine in your machine. But when that product is moved to the server in the production state, it fails to give the same performance or optimization as that on your machine.

For example, when someone builds a website using PHP or asp.net and moves that project to the web server, many uncertainties might occur like images don’t get loaded properly or there are some glitchy parts. This can raise debate from the developer’s side as that particular project is working fine in his or her machine. So how to avoid facing this issue?

Here comes Docker, which is designed to specifically address the issue of “It works on a developer’s machine”. One good thing about docker is that it is compatible with any programming language or any kind of project.

What is Docker Container?

Docker is one kind of virtualization tool which is used to deliver software in packages. These packages are called Containers. The container image is launched by Docker as ‘read-only’ images. The command used to run a container is called “docker run”. Portability is one of the most important traits of the docker. It is important to manage the containers for working in the docker. The container images are called containers after these are operated on the docker engine.

Docker Containers help applications to perform fast and efficiently from one computing domain to another domain. Docker container images are lightweight, standalone, and easily executable software packages required to run an application like code, runtime, system tools settings, and system libraries.

The basic format of docker is:

docker command [options]

Listing the Docker Containers

It is very easy to list a docker container. For this you have to follow some simple steps mentioned here:

Just run the below-written command into a terminal window:

docker ps

If you want to list both the running and stopped containers, then run the following command:

docker ps -a

If you want to list the containers by their ID use, then you can try the following command:

docker ps -aq

Suppose you want to list the total file size of each container, then run the below-given command:

docker ps -s

If you want to list the latest created containers, run the following command:

docker ps -1

“Ps” command can provide several pieces of information:

Container ID

It is a unique alpha-numeric number used for each container. This is created by docker for you to identify the following:

Image

It is the base of the operating system image on which the container is based. These are the names of the images that are used to create a container.

Command

It launches the container. These are generally the commands that are used to create a container.

Created

It defines how long ago the particular container was created. This is the date and time when a particular container is created.

Status

It can be uptime or downtime. It is just the status of the container.

Ports

The port numbers are forwarded to the docker host to communicate with the external world. If there is any, it describes the list of ports of the container made to the host.

Name

These are the funny memorable names assigned by the docker software. We can also specify the names of the container in our way. But if we don’t, the docker sets a random and weird kind of name for us.

If you want to list only the container ID then run the following command:

docker ps -aq

eeae1186ea78

52249ba75f0f

709773bb7128

Note: We assume that you already have an image.

If you don’t have any images, then use the following command:

docker pull name: tag

These were all about the basics of mastering to list a docker container.

Starting or Creating a Docker Container

You are probably scratching your head on how to start a docker container. For this, you have to follow the below-mentioned methods:

If you want to start or launch a single or multiple stopped docker container run the following command:

docker start [options] container_id

You can also specify the container by its name or ID.

If you want to create a new container from an image and then start it, run the following command:

docker start [options] image [command] [argument]

If you do not specify any name for the newly created container, the daemon can generate a random name. If you want to define the name then you can use the –name option.

If you run a docker command using “-t”, you will be attached to the container immediately. You can also use the “exit” command in case you want to exit the container.

If you are using a docker command and the image is not available on the system locally, then it will be downloaded from the registry using the ‘docker search’ and ‘docker pull’ commands even before running the docker run command.

You can keep the container running even after logging out using the -d option.

-d: It helps to operate the container in the background. After that, it prints the container ID.

Stopping a Docker Container

After learning how to list and start a docker container, you must be thinking of how to stop it. You have the option to stop one container at a time or all the containers together. It is quite easy. In that case, run the below-mentioned command to stop the container:

docker stop [-t] –time[=10]] CONTAINER [CONTAINER…]

-time or -t: It is the grace period to wait before the container gets stopped.

If you want to stop all the containers together, run the following command:

docker stop (docker ps -a -q)

Deleting a Docker Container

Deleting containers is also easy. You just have to follow the below-mentioned commands.

If you want to delete the container, run the following command:

docker rm [options] CONTAINER [CONTAINER]

If you want to delete the container, you have to stop it first. To do so, use the following command:

docker stop (docker ps -a -q)

After this command, run the below-given command to delete it:

docker rm [options] CONTAINER [CONTAINER]

Suppose you have to delete a container forcefully, then simply type the below-mentioned command:

docker rm [options] -f CONTAINER [CONTAINER]

Now the container will be deleted.

Conclusion

In this context, you have learned the following methods:

  • How to list a container
  • How to start a container
  • How to stop a container
  • How to delete a container

Now you know that docker is basically used to create, run and then utilize the applications in the containers. These commands are very simple and even if you are a beginner, you can master these commands easily.

As we know that the docker container originated to automate the utilizing process of the applications inside the software containers. After that, it also proceeds in the automation of the operating system level visualization on the “Linux”.

Leave a Comment