How to take actions on multiple docker containers at once
Again, please tell me if there is a better way to do this.
While testing docker, frequently I need to start/stop/rm containers. I got real sick of having to ls them and copy paste the container ID.
Using this alias, I just have to remember a single part of the name of the container, and I will get the container IDs that can then be included as part of another command:
$ alias dcl='_dcl(){ docker container ls -aq -f name="$1";}; _dcl'
$ dcl snikket
b3fcbc808cc9
1947885fbb24
054d67d2e8b9
d8fe9df5f61f
So now that I'm getting a list of IDs, I can easily, for example, pause all of them:
$ docker container pause $( dcl snikket )
Error response from daemon: container is not running
Error response from daemon: container is not running
Error response from daemon: container is not running
Error response from daemon: container is not running
The containers weren't actually running, but this shows the alias working.
I use Compose even when I have a single container to run because I can put all the config bits I need into a file and can then do most of the work without remembering lots of command line options and often without even needing to mention the service name directly.
I keep a docker directory in my home dir that has a directory for each docker container/stack in a compose file. Taking down a container looks like so.
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.
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Options:
-s, --signal string Signal to send to the container
-t, --time int Seconds to wait before killing the container
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.