How to take actions on multiple docker containers at once
from luthis@lemmy.nz to linux@lemmy.ml on 28 Nov 2023 08:01
https://lemmy.nz/post/3841751

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’

#linux

threaded - newest

luthis@lemmy.nz on 28 Nov 2023 08:05 next collapse

You can of course do it this way too, it’s just extra typing:

docker container stop $(docker container ls -qf name=snikket)

RagingToad@feddit.nl on 28 Nov 2023 08:29 next collapse

If the containers are related you could use docker-compose, which has commands to stop / restart/ remove all containers at once.

luthis@lemmy.nz on 28 Nov 2023 08:51 next collapse

Cool, didn’t know that!

Just tested, so you have to cd to the directory with the docker-compose.yml file in it first

gratux@lemmy.blahaj.zone on 28 Nov 2023 09:21 collapse

you can also use the -f option to specify the compose file without going to it.

wcooley@lemmy.world on 28 Nov 2023 08:54 collapse

I use Compose even when I have a single container to run because I can put all the config bits I need into a file and can then do most of the work without remembering lots of command line options and often without even needing to mention the service name directly.

youngGoku@lemmy.world on 28 Nov 2023 11:51 next collapse

Same. I can’t remember the last time I started a docker container without a compose file.

YIj54yALOJxEsY20eU@lemm.ee on 28 Nov 2023 15:54 collapse

I keep a docker directory in my home dir that has a directory for each docker container/stack in a compose file. Taking down a container looks like so.

  • cd docker/wallabag
  • docker-compose down

Imo, the best way to work with docker.

Discover5164@lemm.ee on 28 Nov 2023 21:49 collapse

i do the exact same

this_is_router@feddit.de on 28 Nov 2023 08:35 next collapse

This works but I’d just create a function and use that instead of creating an alias that creates a function and then calls itself.

if your containers are created with a docker compose file you can use docker-compose to target them all

luthis@lemmy.nz on 28 Nov 2023 08:51 collapse

Is there a way to do this without cd-ing to the directory with the compose file first?

JASN_DE@feddit.de on 28 Nov 2023 08:57 collapse

You can use -f /path/to/compose.yaml to call it from wherever you like.

knfrmity@lemmygrad.ml on 28 Nov 2023 09:29 next collapse

You can use the container names to address containers. Whether this is a randomly generated name (docker run… with no --name flag), the compose working dir and service name, or the compose container_name var.

I also rarely use the container command. docker is sufficient, or docker compose … while in the working dir of a given compose stack.

hanke@feddit.nu on 28 Nov 2023 11:02 next collapse

I often just do

docker ps | awk "{print $1}" | xargs docker stop

Add some filtering in there and you’re golden

MangoPenguin@lemmy.blahaj.zone on 28 Nov 2023 13:35 next collapse

How tf do you remember that lol, I’m always amazed by CLI focused people being able to remember so much!

hanke@feddit.nu on 28 Nov 2023 13:40 next collapse

I think it has to do with creativity!

The CLI tools are just small simple tools. The power comes from having the understanding of how each tool works and how they can be combined.

I don’t remember this string of commands, I know docker, awk and xargs. When I need this, that is the solution I always end up with.

luthis@lemmy.nz on 28 Nov 2023 18:49 collapse

Dude, I use the CLI all day, every day and I can’t freakin remember half the commands I need.

If it’s something I use often, I’ll make an alias even if it’s just so I can run ‘alias’ in the terminal to get a list of things I use often.

crmsnbleyd@sopuli.xyz on 28 Nov 2023 16:28 collapse

This is what I do as well

FishFace@lemmy.world on 28 Nov 2023 12:05 next collapse

Why create the function _dcl()?

luthis@lemmy.nz on 28 Nov 2023 18:52 collapse

I needed a way to pass an argument into the command so it can be used in name=“$1”

FishFace@lemmy.world on 28 Nov 2023 19:04 collapse

you could instead do:

dcl() { docker container ls -aq -f name="$1" }

in bashrc or wherever you’re setting this up.

MangoPenguin@lemmy.blahaj.zone on 28 Nov 2023 13:35 next collapse

My better way is just using Portainer, select some containers and hit the stop button.

luthis@lemmy.nz on 28 Nov 2023 18:46 collapse

If I eventually get around to using a GUI, I’ll check out portainer

kwozyman@lemmy.world on 28 Nov 2023 15:16 next collapse

I don’t know if this works in docker (usually there is 1:1 equivalency between the two), but with podman you can do something like:

podman stop --filter name=foo

man podman-stop tells us:

   --filter, -f=filter
       Filter what containers are going to be stopped.  Multiple filters can be given with multiple uses of the --filter flag.  Filters with the same  key  work
       inclusive with the only exception being label which is exclusive. Filters with different keys always work exclusive.
luthis@lemmy.nz on 28 Nov 2023 18:45 collapse

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]

Options:
  -s, --signal string   Signal to send to the container
  -t, --time int        Seconds to wait before killing the container

Unfortunately no filter here

Shareni@programming.dev on 28 Nov 2023 18:57 next collapse

I’m using docker packages for Doom Emacs. The main one is docker.el. On top of being faster and easier to use than the cli, you can also do some pretty neat stuff like use dired+tramp to browse files and open them in Emacs.

xlash123@sh.itjust.works on 28 Nov 2023 20:15 collapse

Just a few shortcuts that may help:

  • docker ps is an alias for docker 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.