How to take actions on multiple docker containers at once
Again, please tell me if there is a better way to do this.
While testing docker, frequently I need to start/stop/rm containers. I got real sick of having to ls them and copy paste the container ID.
Using this alias, I just have to remember a single part of the name of the container, and I will get the container IDs that can then be included as part of another command:
<span style="color:#323232;">$ alias dcl='_dcl(){ docker container ls -aq -f name="$1";}; _dcl'
</span><span style="color:#323232;">
</span><span style="color:#323232;">$ dcl snikket
</span><span style="color:#323232;">b3fcbc808cc9
</span><span style="color:#323232;">1947885fbb24
</span><span style="color:#323232;">054d67d2e8b9
</span><span style="color:#323232;">d8fe9df5f61f
</span>
So now that I’m getting a list of IDs, I can easily, for example, pause all of them:
<span style="color:#323232;">$ docker container pause $( dcl snikket )
</span><span style="color:#323232;">Error response from daemon: container is not running
</span><span style="color:#323232;">Error response from daemon: container is not running
</span><span style="color:#323232;">Error response from daemon: container is not running
</span><span style="color:#323232;">Error response from daemon: container is not running
</span>
The containers weren’t actually running, but this shows the alias working.
dcl obviously stands for ‘docker container ls’
Add comment