diff --git a/content/about/mikeconrad-resume.pdf b/content/about/mikeconrad-resume.pdf new file mode 100644 index 0000000..094f297 Binary files /dev/null and b/content/about/mikeconrad-resume.pdf differ diff --git a/content/post/2025-06-18-docker-shell-tricks/index.md b/content/post/2025-06-18-docker-shell-tricks/index.md new file mode 100644 index 0000000..e0f51bf --- /dev/null +++ b/content/post/2025-06-18-docker-shell-tricks/index.md @@ -0,0 +1,113 @@ +--- +title: "Docker Shell Tricks" +date: 2025-06-18T09:10:13-04:00 +categories: + - Tips and Tricks +tags: + - Docker + - CI/CD + - Linux + - Containers + - DevOps +--- + +# Overview + +One of my all time favorite tricks when working with Docker. Getting information on or performing an action on multiple containers with similar naming schemes. + +Passing the --filter name= flag allows for fuzzy matching. + +docker container ps -q returns only container ids by default and is perfect for combining with commands like docker container stop or docker container rm. + +Of course if you are using docker compose, simply running `docker compose down` will accomplish the same thing. + +I wanted to share one of my all time favorite tricks when working with Docker. It is fairly common (for me at least) to want to perform some action on a number of containers at once. For example, sometimes I have a lot of random containers running and I want to shut them all down (and sometimes remove them) at once. Of course if you are using compose you can simply run `docker compose down` but sometimes you have containers not managed by a compose file. + +In the past I would write shell scripts to handle this. Something like: + +```bash +$ for i in $(docker container ps --format '{{.ID}}'); do docker container rm --force $i; done +5d5b3004003f +038b809fb0af +de4abb80414c +530440f8848e +3aff02eddfe1 +3e29e7db168c +46275b44f744 +b3cd33cd7658 +8e9f226f107e +29f67eea6ac8 +597b72330d3d + +``` + +This works ok, but if you also only want to kill certain containers it gets a bit trickier. For instance, we use Docker to spin up preview environments where each environment may have up to 10 containers. They are all prefixed with a PR number so I could do something like this: + +```bash +$ for i in $(docker container ps --format '{{.ID}} {{.Names}}' | grep pr1 | awk '{print $1}'); do docker container rm --force $i; done +``` + +While both of those solutions work, they are a bit messing and fortunately Docker has a much better solution built in. Passing the `-q` flag to certain commands will by default just return a list of ids + +```bash +$ docker container ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +31c3dbd33795 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp pr9 +899384beaf6f nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp pr8 +e3aa660916d7 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp pr7 +5fd2998a800a nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp pr6 +898450246c0c nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp pr5 +9e22f39b9810 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp pr4 +3a0ee53664cf nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp pr3 +7e3a512739a4 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp pr2 +af96f09686ef nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp pr1 + +$ docker container ps -q +31c3dbd33795 +899384beaf6f +e3aa660916d7 +5fd2998a800a +898450246c0c +9e22f39b9810 +3a0ee53664cf +7e3a512739a4 +af96f09686ef + +``` + +One of the best parts about this trick though is that you can combine it with the `--filter` flag like this: + +```bash +$ docker container ps --filter name=pr -q +31c3dbd33795 +899384beaf6f +e3aa660916d7 +5fd2998a800a +898450246c0c +9e22f39b9810 +3a0ee53664cf +7e3a512739a4 +af96f09686ef +``` + +Now stopping all my containers is as easy as: + +```bash +$ docker container rm $(docker container ps -qa) --force +31c3dbd33795 +899384beaf6f +e3aa660916d7 +5fd2998a800a +898450246c0c +9e22f39b9810 +3a0ee53664cf +7e3a512739a4 +af96f09686ef +``` +Or, if I just want to stop specific containers, say all the ones that start with pr11217 in the name: +```bash +$ docker container rm $(docker container ps --filter name=pr11217 -qa) --force +``` +Pretty slick! No more messing with `awk,grep,head,tail, etc` , instead just do it in one simple command. I would love to hear your tips and tricks for working with Docker! + +#docker #cicd #containers #softwaredevelopment \ No newline at end of file