Removing containers

Let's check the containers we have running using the docker container ls -a command. When I run the command, I can see that I have two containers with an Exited status and all of the others are running:

To remove the two exited containers, I can simply run the prune command:

$ docker container prune

When doing so, a warning pops up and you are asked to confirm whether you are really sure:

You can choose which container you want to remove using the rm command; like this, for example:

$ docker container rm nginx4

However, attempting to remove a running container will result in an error:

Error response from daemon: You cannot remove a running container 5bbc78af29c871200539e7534725e12691aa7df6facd616e91acbe41f204b734. Stop the container before attempting removal or use -f

As you can see from the preceding output, the error very kindly suggests that using the -f flag will forcibly remove the container by stopping it and then removing it, requiring the following command:

$ docker container rm -f nginx4

Another alternative would be to string the stop and rm commands together:

$ docker container stop nginx3 && docker container rm nginx3

However, given that you can use the prune command now, this is probably way too much effect, especially as you are trying to remove the containers and probably don't care too much how gracefully the process is terminated.

Feel free to remove the remainder of your containers using whichever method you like.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset