見出し画像

Stopping or Killing a Docker container

To stop or kill the container "busybox" we created, just initiate the docker stop or kill command together with the container id.

#Stopping a container
docker stop (container id)

#Killing a container
docker kill (container id)

Let's first start a demo by first creating a container that pings to google.com.

docker run busybox ping google.com  

It's possible to run certain linux commands in busybox because a container is essentially a localized snapshot of linux. That's why if you run 

docker exec -it (container id) sh

docker will prompt you to a new shell within the container itself as the root user. "ls" in the currently directory and you will find a filesystem similar to Linux.

/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var

 We will elaborate more about that in future lectures. 

Going back to out topic, let's check the container id of busybox.

#Input
docker ps

#Output
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
42a98806e4d1        busybox             "ping google.com"        17 hours ago        Exited (137) 17 hours ago                       goofy_bassi

For a soft exit (docker stop), which is a form of exit whereby the docker client sends a "SIGTERM" to the container. The Docker client will wait for all processes to stop running in the container before terminating the container. The opposite is true for a hard exit (docker kill), the Docker client sends a "SIGKILL" to terminate all processes running in the container instantaneously. It may take up a few seconds to a few minutes to in the case of soft exit.

#Stopping a container
docker stop 42a98806e4d1 

#Killing a container
docker kill 42a98806e4d1 



この記事が気に入ったらサポートをしてみませんか?