Docker Tip #83: Stop Docker Containers by Name Using Patterns


Docker makes it easy to stop containers by a specific container name, but here's how you can stop containers by a wild card pattern.
I had this recently come up on a server where I wanted to stop a bunch of containers that all started with a specific name, but I didn’t want to stop every container on the system.
We’re all aware of the docker container stop command which allows us to do things like docker container stop hello to stop a container that is named hello. It also supports stopping more than 1 container at a time if you separate each name or ID by a space. That gets us a third of the way there.
The next step is to get a list of running container IDs that we want to stop – ideally, we want to get this list of container IDs by supplying a wild card pattern to match on.
We can do that by running docker container ls -q --filter name=myapp* where myapp is the pattern we want to match. That will get all running containers whose names start with myapp and thanks to -q (quiet) it will return only the IDs. By the way, you can change that to *myapp to get containers that end with myapp or use * anywhere in between.
The last piece of the puzzle is to combine both commands together. Fortunately with Bash that’s really easy to do with sub-shells.
We can feed the output of the docker container ls command into docker container stop by running docker container stop $(docker container ls -q --filter name=myapp*).
Note: if you run the command and ls finds no matches, you will get a "docker container stop" requires at least 1 argument error from Docker, which is normal. That just means no IDs were passed into the stop command since no containers were found.[Source]- https://nickjanetakis.com/blog/docker-tip-83-stop-docker-containers-by-name-pattern


Comments

Popular posts from this blog

Is Data Scientist & Data Analyst are same? Learn the Differences Now!

What Is Java? A Beginner’s Guide to Java and Its Evolution

Full Stack Development : All that you need to know