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’

  • RagingToad@feddit.nl
    link
    fedilink
    English
    arrow-up
    10
    ·
    1 year ago

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

    • Wil Cooley@lemmy.world
      link
      fedilink
      English
      arrow-up
      8
      ·
      1 year ago

      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.

      • Goku@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

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

  • this_is_router@feddit.de
    link
    fedilink
    arrow-up
    3
    ·
    1 year ago

    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

  • hanke@feddit.nu
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    I often just do

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

    Add some filtering in there and you’re golden

  • ∟⊔⊤∦∣≶@lemmy.nzOP
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

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

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

  • knfrmity@lemmygrad.ml
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    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.