iconoclast
iconoclast

Reputation: 22610

Create your own Docker (sub-)commands

Things like Git allow you to create your own (2nd-level) commands by creating a script with the appropriate name and saving it in the appropriate place (anywhere in your PATH if I recall correctly). So you could have

$ git my-cool-command 

if you wanted.

Is there a way to do such things with Docker? Google DuckDuckGo doesn't return anything useful.

Upvotes: 0

Views: 259

Answers (2)

Thom Parkin
Thom Parkin

Reputation: 346

I have been doing this with BASH scripts. For example:

```

~/.bash_aliases

Kill all running containers.

alias dockerkill='docker kill $(docker ps -a -q)'

Delete all stopped containers.

alias dockercleanc='printf "\n>>> Deleting stopped containers\n\n" && docker rm $(docker ps -a -q)'

Delete all untagged images.

alias dockercleani='printf "\n>>> Deleting untagged images\n\n" && docker rmi $(docker images -q -f dangling=true)'

Delete all stopped containers and untagged images.

alias dockerclean='dockercleanc || true && dockercleani'

```

Upvotes: 0

iconoclast
iconoclast

Reputation: 22610

I'm hoping someone else has a better answer, using a "docker-approved" officially supported approach that is not such a kludge, but here's an approach that should work if nothing else appears.

Write your own function named docker. It should take precedence over anything in your PATH.

The function should take the first argument passed into it (we'll use foo as an example) and check whether a docker-foo command exists in the PATH. If not, it should just call the docker executable with all arguments, so your command would work as expected. If there is a docker-foo, it should call that (with the hyphen) and all other arguments appended.

Upvotes: 1

Related Questions