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:
$ alias dcl='_dcl(){ docker container ls -aq -f name="$1";}; _dcl'
$ dcl snikket
b3fcbc808cc9
1947885fbb24
054d67d2e8b9
d8fe9df5f61f
So now that I’m getting a list of IDs, I can easily, for example, pause all of them:
$ docker container pause $( dcl snikket )
Error response from daemon: container is not running
Error response from daemon: container is not running
Error response from daemon: container is not running
Error response from daemon: container is not running
The containers weren’t actually running, but this shows the alias working.
dcl obviously stands for ‘docker container ls’
If the containers are related you could use docker-compose, which has commands to stop / restart/ remove all containers at once.
Cool, didn’t know that!
Just tested, so you have to cd to the directory with the docker-compose.yml file in it first
you can also use the -f option to specify the compose file without going to it.
Just a few shortcuts that may help:
docker ps
is an alias fordocker container ls
- as long as it can be uniquely identified, a prefix of the container ID can be used instead of copy pasting the entire ID
- you can use container names instead of IDs
- tab completion works for container names
As someone else suggested though, docker compose is probably best suited for this job, but hopefully this helps in other situations.