Docker

Stopping docker container and removing docker container

Stopping docker container

There are times, stopping the docker container becomes a necessity, so in order to perform the action, we need to understand the available options to do so.

Before going further make sure you have a basic understanding of docker if no then follow the following page -> Learn Docker for Free

Intial System State

We have two following containers running in our system, namely “container1” and “container2“.

list docker containers

sudo docker container ls

Stop the container with the specified container ID

With this, we can easily specify only the id of the container in order to stop it.

stop docker container with container id

sudo docker container stop <container-ID>

Stop the container with specified container name

We do not need to worry about remembering the names of the containers, instead we can do some actions only by giving the name of the container as we did as follows:

stop docker container with container name

sudo docker container stop <container-name>

From below, we can see that both the containers have been stopped.

list stopped docker containers

sudo docker container ls -a

Note: We cannot remove a container without stopping it.

Removing docker container

Sometimes we need to remove the docker containers, because of some following possible reasons:

  1. To clean up the clutter in the system.
  2. To free up the storage space for new containers.
  3. When we have accidentally created a container with unwanted parameters.

Remove the container with the specified “container ID”

A docker container can be removed with the help of container id, moreover we can also specify only just the initial letter of the id, as we did by just specifying the starting 4 letters of the id.

remove docker container with container id

sudo docker container rm <container-ID>

Remove the container with the specified “container name”

It is always easy to remember friendly names, and docker can do a lot by just having the names of the container, as we have done by removing the container with its name.

remove docker container with container name

sudo docker container rm <container-name>

Question: Is there any way with which we can kill a docker container?

Answer: Yes, for this docker provides a built-in command with the help of which we can forcefully stop a docker container.

Killing docker container OR Forcefully stop docker container

Forcefully stopping a docker container is helpful when for example the container is serving as a nginx or apache server and is being run as a high priority task, so in that case, on the first attempt, it always shows some error, and to avoid such situations we need some high priority command as well. So, here killing a docker container comes in picture.

Below picture states that we have created a docker container named as “container3“.

create a docker container with name

Now we are going to “kill” the “container3

kill a docker container

sudo docker container kill <container-name>

You can see that the “prompt” has been automatically gone and regular “host’s prompt” is now available.

Note: Killing a container is only going to stop it forcefully, you still have to remove it separately.

Stopping and removing containers at once

Current system state

To understand the “at once” concept, we have to first create a few containers, so we have created 3 containers named as: “container4“, “container5” and “container6“.

container4
created docker container
container5
created docker container
container6
list docker containers

Question: So is there a way by which we can “stop all the containers at once“?

Answer: Yes, docker has a dedicated command for this as well.

Stop all docker containers at once

Stop all docker containers at once

sudo docker container stop $(sudo docker container ls -a -q)

list docker container

Note: We can only remove a docker container after stopping it.

Now, the system state is as following:

From the above picture, we can see that after stopping the containers, they are not remove, we have to remove them separately.

Question: Can we remove all the docker containers “at once“?

Answer: Yes, of course, we can remove all the docker containers only with the help of one command, and the procedure is as follows.

Remove all docker containers at once

Remove all docker containers at once

sudo docker container rm $(sudo docker container ls -a -q)

Comment here