Saturday, August 6, 2016

TIP: find specific Docker container without using "cut" or other Shell silliness

People on the Interwebs love doing things in a leaden, verbose, laborious, and dusty manner.  Like piping a command through egrep then cut and sed then awk, when just "awk" will do.

Take Docker. We often want to find out which container is running something specific. Once we find our container, we want to "exec" a shell into it, or we want to kill it.

Here's the best way that doesn't mess with cut/sed/awk:

$ docker ps --filter ancestor=web,status=running --quiet
8fbc117716c3

In the previous command I was looking for the container ID that is descended from my "web" image. I'm not interested in my database nor Redis nor anything else, I want my web container.

Here's how to use it in practice, by killing the web container:

$ docker rm -f $(docker ps --filter ancestor=web,status=running --quiet)
8fbc117716c3

Also, here's how to use the same technique to hop into the web container, and run a shell.  This I use constantly, so I can examine a container to see what's going on, what it's doing, or what it's not doing:

$ docker exec -it $(docker ps --filter ancestor=web,status=running --quiet) bash
root@23b5a26eb252:/#

No comments:

Post a Comment