4

I am trying to remove running containers and delete all downloaded images by using below commands:

docker ps -aq | xargs docker rm -f

docker images -aq | xargs docker rmi -f

But I got this

"docker rm" requires at least 1 argument.
See 'docker rm --help'.

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

and

"docker rmi" requires at least 1 argument.
See 'docker rmi --help'.

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

this is my environment

Client:
 Version:      17.09.0-ce
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:42:18 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.09.0-ce
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:40:56 2017
 OS/Arch:      linux/amd64
 Experimental: false
docker-compose version 1.16.1, build 6d1ac21
docker-py version: 2.5.1
CPython version: 2.7.13
OpenSSL version: OpenSSL 1.0.1t  3 May 2016

Is something wrong here? please help! Thanks

neo
  • 618
  • 1
  • 10
  • 29
  • Please check https://stackoverflow.com/questions/45121627/remove-all-stopped-containers-docker-rm-requires-at-least-1-argument maybe useful for you – Ashish Detroja Nov 01 '17 at 09:57

5 Answers5

6

Use -r with xargs, it means not to execute if empty:

docker ps -aq | xargs -r docker rm -f
docker images -aq | xargs -r docker rmi -f
David Buck
  • 3,752
  • 35
  • 31
  • 35
5

You need to first stop running container then remove them

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

For images try to remove dangling images

docker rmi $(docker images -f dangling=true -q)

To remove all images use

docker rmi $(docker images -q)
Farhan Yaseen
  • 2,507
  • 2
  • 22
  • 37
3

Following Docker rm doc:

Force-remove a running container

This command will force-remove a running container.

$ docker rm --force containerName
containerName

To remove all stopped containers:

$ docker rm $(docker ps -a -q)

For the Docker image:

In the Docker Image doc:

If you use the -f flag and specify the image’s short or long ID, then this command untags and removes all images that match the specified ID.

Example:

$ docker images

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
test1                     latest              fd484f19954f        23 seconds ago      7 B (virtual 4.964 MB)
test                      latest              fd484f19954f        23 seconds ago      7 B (virtual 4.964 MB)
test2                     latest              fd484f19954f        23 seconds ago      7 B (virtual 4.964 MB)

$ docker rmi -f fd484f19954f

Untagged: test1:latest
Untagged: test:latest
Untagged: test2:latest
Deleted: fd484f19954f4920da7ff372b5067f5b7ddb2fd3830cecd17b96ea9e286ba5b8

In my personal experience when I need to force the deletion of a container I usually do:

docker container rm -f containerName

And, if I no longer need the image I use:

 docker image rm -f imageName

Please note that the above command will throw an error if the image is still in use by a container.

juanlumn
  • 6,155
  • 2
  • 30
  • 39
1

Looks like you passing arguments incorrectly. try something like that to delete all containers:

docker rm -f $(docker ps -qa)

And to delete all your images:

docker rmi $(docker images -q)

0

It is most likely that xargs docker rm -f retunrs nothing (you should try running just this) and therefore there is no argument passed to docker ps -aq.

PS - generally speaking, it is recommended to try running the expression that you'd use as an argument later, to see what it'd evaluate to, especially before deletion.

oba2311
  • 373
  • 4
  • 12