Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute Docker query command on all containers?

Tags:

docker

In docker I can make docker inspect -f "{{ .NetworkSettings.IPAddress }}" CONTAINER to run command on a particular container.

What I need is to do something like docker inspect -f "{{ .NetworkSettings.IPAddress }}" all or docker inspect -f "{{ .NetworkSettings.IPAddress }}" * to run a command on all containers, not mentioning their names explicitly?

It relates to other docker commands as well. Is there such a way without bash scripting?

like image 839
f1yegor Avatar asked May 06 '15 07:05

f1yegor


People also ask

What command will list all running docker containers all containers?

In order to list the Docker containers, we can use the “docker ps” or “docker container ls” command. This command provides a variety of ways to list and filter all containers on a particular Docker engine. Let's start by listing all the running containers.

How do I run a command in a docker container?

If you need to run a command inside a running Docker container, but don't need any interactivity, use the docker exec command without any flags: docker exec container-name tail /var/log/date.

How do I start all docker containers in one command?

If you mean to create multiple containers at the same time then you will have to make use of docker-compose. This will startup any stopped containers.


1 Answers

You can list multiple container to a docker inspect.

docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]

But the only way to list those container is through bash $(sudo docker ps -aq).

For example:

docker inspect --format='{{.Name}}' $(sudo docker ps -aq)
docker inspect -f "{{ .NetworkSettings.IPAddress }}" $(sudo docker ps -aq)

The OP f1yegor proposes in the comments:

all=$(sudo docker ps -aq) docker inspect -f "{{ .NetworkSettings.IPAddress }}" $all 
like image 55
VonC Avatar answered Sep 21 '22 18:09

VonC