If the containers are related you could use docker-compose, which has commands to stop / restart/ remove all containers at once.
post
I often just do
docker ps | awk "{print $1}" | xargs docker stop
Add some filtering in there and you're golden
How tf do you remember that lol, I'm always amazed by CLI focused people being able to remember so much!
I think it has to do with creativity!
The CLI tools are just small simple tools. The power comes from having the understanding of how each tool works and how they can be combined.
I don't remember this string of commands, I know docker, awk and xargs. When I need this, that is the solution I always end up with.
Dude, I use the CLI all day, every day and I can't freakin remember half the commands I need.
If it's something I use often, I'll make an alias even if it's just so I can run 'alias' in the terminal to get a list of things I use often.
This works but I'd just create a function and use that instead of creating an alias that creates a function and then calls itself.
if your containers are created with a docker compose file you can use docker-compose to target them all
Just a few shortcuts that may help:
docker psis an alias fordocker container ls- as long as it can be uniquely identified, a prefix of the container ID can be used instead of copy pasting the entire ID
- you can use container names instead of IDs
- tab completion works for container names
As someone else suggested though, docker compose is probably best suited for this job, but hopefully this helps in other situations.
I don't know if this works in docker (usually there is 1:1 equivalency between the two), but with podman you can do something like:
podman stop --filter name=foo
man podman-stop tells us:
--filter, -f=filter
Filter what containers are going to be stopped. Multiple filters can be given with multiple uses of the --filter flag. Filters with the same key work
inclusive with the only exception being label which is exclusive. Filters with different keys always work exclusive.
My better way is just using Portainer, select some containers and hit the stop button.
Why create the function _dcl()?
I needed a way to pass an argument into the command so it can be used in name="$1"
You can of course do it this way too, it's just extra typing:
docker container stop $(docker container ls -qf name=snikket)
I'm using docker packages for Doom Emacs. The main one is docker.el. On top of being faster and easier to use than the cli, you can also do some pretty neat stuff like use dired+tramp to browse files and open them in Emacs.
all 25 comments