Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run docker commands using glob / wildcard on container names

I'm using docker-compose up --scale to create multiple versions of the same container. As a result I end up with containers named container_foo_1, container_foo_2 etc.

Does docker support any kind of glob / wildcard matching on container names in it's command line tools? What I want to do is this:

docker inspect container_foo_*

What I'm doing right now in the short term is just using:

docker-inspect container_foo_{1,2} (using bash brace expansion)

but I'd love if there was a way where I didn't know how many containers there were / what the numbers were ahead of time.

like image 236
Nic Barker Avatar asked Aug 30 '18 23:08

Nic Barker


1 Answers

You can use the argument --filter | -f at docker ps with docker inspect.

Usage: docker ps --filter key=value, where value accept regular expressions.

The currently supported filters are:

  • id Container’s ID
  • name Container’s name
  • label An arbitrary string representing either a key or a key-value pair. Expressed as or =
  • exited An integer representing the container’s exit code. Only useful with --all.
  • status One of created, restarting, running, removing, paused, exited, or dead
  • ancestor Filters containers which share a given image as an ancestor. Expressed as * [:], , or image@digest
  • before or since Filters containers created before or after a given container ID or name
  • volume Filters running containers which have mounted a given volume or bind mount.
  • network Filters running containers connected to a given network.
  • publish or expose Filters containers which publish or expose a given port. Expressed as <port>[/<proto>] or <startport-endport>/[<proto>]
  • health Filters containers based on their healthcheck status. One of starting, healthy, unhealthy or none.
  • isolation Windows daemon only. One of default, process, or hyperv.
  • is-task Filters containers that are a “task” for a service. Boolean option (true or false)

Ex: docker inspect $(docker ps --filter name=^/server --quiet)

References:

  • Filtering
  • How to filter docker ps by exact name?
like image 160
Tuxpilgrim Avatar answered Nov 15 '22 05:11

Tuxpilgrim