3 Commits

Author SHA1 Message Date
8e042d4f31 Add post about docker commands 2025-06-24 13:19:10 -04:00
96624b75bd Add some analytics 2025-05-11 20:02:33 -04:00
21c3c7d429 Merge pull request 'Added article and updated hugo' (#1) from blog-post/iac into master
Reviewed-on: #1
2025-04-24 12:18:13 -04:00
4 changed files with 120 additions and 2 deletions

View File

@ -14,9 +14,9 @@ minifyOutput = true
[params]
env = "production"
title = "Hackanooga"
description = "ExampleSite description"
description = "Hackanooga - Confessions of a homelab hacker"
keywords = [ "Blog", "Portfolio", "PaperMod" ]
author = "Me"
author = "Mike Conrad"
images = [ "<link or path of image for opengraph, twitter-cards>" ]
DateFormat = "January 2, 2006"
defaultTheme = "auto"

Binary file not shown.

View File

@ -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

View File

@ -0,0 +1,5 @@
<script
src="https://logs.hackanooga.com/api/script.js"
data-site-id="2"
defer
></script>