Docker

How to Kill a Docker Container?

In Docker, killing a container means stopping or terminating the container and its processes. While working on Docker, developers build and deal with multiple containers. Sometimes, they want to stop running Docker containers. In this situation, Docker provides two ways to stop containers, such as stopping a container forcefully or stopping a container gracefully.

This study will demonstrate the method to kill a Docker container.

How to Kill a Docker Container?

To kill the Docker container, follow the provided steps:

Step 1: Display All Containers

First, display all the Docker containers using the “docker ps -a” command and choose a particular container that needs to be killed:

docker ps -a

The output shows three Docker containers, and we select the “cont1” container.

Step 2: Kill Docker Container

Now, utilize the “docker kill” command along with the selected container name or ID to kill or stop it forcefully. In our case, “cont1” is selected as the container name:

docker kill cont1

The above command shows that “cont1” has been killed.

Alternatively, the “docker stop <container-name/ID>” command can also be used to stop the container gracefully:

docker stop cont1

The successful execution of the above command verifies that “cont1” has been stopped.

Step 3: Verify the Killed Docker Container

To verify whether the selected container has been killed or not, display all containers using the provided command:

docker ps -a

In the output, the status of the “cont1” container is “Exited” which indicates that it has been killed successfully.

Conclusion

To kill a Docker container, first, view all the Docker containers and select a particular container that needs to be killed. Then, execute the “docker kill <container-name/ID>” command to stop the selected container forcefully. Alternatively, the “docker stop <container-name/ID>” command can also be used to stop the container gracefully. This study has demonstrated the step-by-step procedure to kill a Docker container.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.