Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one remove a Docker image?

Tags:

docker

I'm running Docker under Vagrant under OS X 10.8.4 (Mountain Lion), and whenever I try to delete a saved image, I get an error:

$ docker rmi some-image-id
2013/07/15 hh:mm:ss unexpected JSON input

According to the rmi help, the proper syntax is docker rmi IMAGE [IMAGE...], and I'm not sure what to make of that.

How can I delete an image?

$ docker version
Client version: 0.4.8
Server version: 0.4.8
Go version: go1.1

 

$docker info
Containers: 1
Images: 3

Interestingly, when I run docker ps, no containers show up at all. Running docker images shows four (4) base images and one (1) node image.

like image 397
Jules Avatar asked Jul 15 '13 22:07

Jules


People also ask

How do I stop and remove docker images?

To stop and remove the container in one command, you can add the force option -f.

How do I remove an existing docker container?

To remove one or more Docker containers, use the docker container rm command, followed by the IDs of the containers you want to remove. If you get an error message similar to the one shown below, it means that the container is running. You'll need to stop the container before removing it.


15 Answers

Try docker rmi node. That should work.

Seeing all created containers is as simple as docker ps -a.

To remove all existing containers (not images!) run docker rm $(docker ps -aq)

like image 109
Nick Stinemates Avatar answered Oct 01 '22 11:10

Nick Stinemates


The following are some of the ways to remove docker images/containers:

Remove single image

docker rmi image_name:version/image-id

Remove all images

docker rmi $(docker images -qf "dangling=true")

Kill containers and remove them:

docker rm $(docker kill $(docker ps -aq))

Note: Replace kill with stop for graceful shutdown

Remove all images except "my-image"

Use grep to remove all except my-image and ubuntu

docker rmi $(docker images | grep -v 'ubuntu\|my-image' | awk {'print $3'})

Or (without awk)

docker rmi $(docker images --quiet | grep -v $(docker images --quiet ubuntu:my-image))

like image 26
Ulises Avatar answered Oct 01 '22 11:10

Ulises


Delete all docker containers

docker rm $(docker ps -a -q)

Delete all docker images

docker rmi $(docker images -q)
like image 27
Chrysalis Avatar answered Oct 01 '22 12:10

Chrysalis


To remove an image from Docker using the image ID:

  1. Get the list of all Images

    docker images
    
  2. Identify the image ID of the image you want to delete, for example:

    REPOSITORY     TAG     IMAGE ID        CREATED        VIRTUAL SIZE
    kweku360/java  latest  08d3a9b8e166    2 weeks ago         5.733 GB`
    
  3. Finally remove the image using the image ID (only the first three digits are required)

    docker rmi 08d
    
like image 22
kweku360 Avatar answered Oct 01 '22 12:10

kweku360


Image:

  1. List images

    docker images

  2. Remove one image

    docker rmi image_name

  3. Force remove one image

    docker rmi -f image_name

Container:

  1. List all containers

    docker ps -a

  2. Remove one container

    docker rm container_id

  3. Force remove one container

    docker rm -f container_id

like image 34
hackfox code Avatar answered Oct 01 '22 11:10

hackfox code


Update, as commented by VonC in How to remove old Docker containers.

With Docker 1.13 (Q4 2016), you now have:

docker system prune will delete ALL unused data (i.e., in order: containers stopped, volumes without containers and images with no containers).

See PR 26108 and commit 86de7c0, which are introducing a few new commands to help facilitate visualizing how much space the Docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.

docker system prune

WARNING! This will remove:
    - all stopped containers
    - all volumes not used by at least one container
    - all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
like image 27
qkrijger Avatar answered Oct 01 '22 11:10

qkrijger


Removing Containers

  1. To remove a specific container

    docker rm CONTAINER_ID CONTAINER_ID
    
    • For single image

      docker rm  70c0e19168cf
      
    • For multiple images

      docker rm  70c0e19168cf c2ce80b62174
      
  2. Remove exited containers

    docker ps -a -f status=exited
    
  3. Remove all the containers

    docker ps -q -a | xargs docker rm
    


Removing Images

docker rmi IMAGE_ID
  1. Remove specific images

    • for single image

      docker rmi ubuntu
      
    • for multiple images

      docker rmi ubuntu alpine
      
  2. Remove dangling images
    Dangling images are layers that have no relationship to any tagged images as the Docker images are constituted of multiple images.

    docker rmi -f $(docker images -f dangling=true -q)
    
  3. Remove all Docker images

    docker rmi -f $(docker images -a -q)
    

Removing Volumes

To list volumes, run docker volume ls

  1. Remove a specific volume

    docker volume rm VOLUME_NAME
    
  2. Remove dangling volumes

    docker volume rm $(docker volume ls -f dangling=true -q)
    
  3. Remove a container and its volumes

    docker rm -v CONTAINER_NAME
    
like image 34
All Іѕ Vаиітy Avatar answered Oct 01 '22 11:10

All Іѕ Vаиітy


docker rm container_name

docker rmi image_name

docker help

rm Remove one or more containers

rmi Remove one or more images

like image 21
KunMing Xie Avatar answered Oct 01 '22 12:10

KunMing Xie


docker rmi  91c95931e552

Error response from daemon: Conflict, cannot delete 91c95931e552 because the container 76068d66b290 is using it, use -f to force FATA[0000] Error: failed to remove one or more images

Find container ID,

# docker ps -a

# docker rm  daf644660736 
like image 43
Lakshmikandan Avatar answered Oct 01 '22 12:10

Lakshmikandan


First of all, we have to stop and remove the Docker containers which are attached with the Docker image that we are going to remove.

So for that, first

  • docker stop container-id - To stop the running container
  • docker rm container-id - To delete/remove the container

then,

  • docker rmi image-id - To delete/remove the image
like image 36
Murali Manchadikkal Avatar answered Oct 01 '22 11:10

Murali Manchadikkal


For versions 1.13 and higher:

docker image rm [OPTIONS] IMAGE [IMAGE...]

Comparing:

  • the documention of docker image rm and
  • the documentation of docker rmi,

the [OPTIONS] seem to have no difference.

--force , -f        Force removal of the image
--no-prune          Do not delete untagged parents

From: Introducing Docker 1.13

CLI restructured

In Docker 1.13, we regrouped every command to sit under the logical object it’s interacting with. For example list and start of containers are now subcommands of docker container and history is a subcommand of docker image.

These changes let us clean up the Docker CLI syntax, improve help text and make Docker simpler to use. The old command syntax is still supported, but we encourage everybody to adopt the new syntax.

like image 38
tgogos Avatar answered Oct 01 '22 10:10

tgogos


Docker provides some command to remove images.

Show/Remove Images:

docker images
docker images -a # All images
docker images --no-trunc # List the full length image IDs

docker images --filter "dangling=true" // Show unstage images
docker rmi $(docker images -f "dangling=true" -q) # Remove on unstages images

docker rmi <REPOSITORY> or <Image ID> # Remove a single image

docker image prune # Interactively remove dangling images
docker image prune -a # Remove all images

or 

docker rmi -f $(sudo docker images -a -q)

Also, you can also use filter parameters to remove set of images at once:

Example:

$docker images --filter "before=<hello-world>" // It will all images before hello-world

$docker images --filter "since=<hello-world>" // It will all images since hello-world

So you can delete that filter images like this:

docker rmi $(docker images --filter "since=<hello-world>")
docker rmi $(docker images --filter "before=<hello-world>")
like image 41
Mr Singh Avatar answered Oct 01 '22 10:10

Mr Singh


Delete all of them using

Step 1: Kill all containers

for i in `sudo docker ps -a | awk '{ print $1 }'`; do sudo docker kill $i ; done

Step 2: RM them first

for i in `sudo docker ps -a | awk '{ print $1 }'`; do sudo docker rm $i ; done

Step 3: Delete the images using force

for i in `sudo docker images | awk '{ print $3}'`; do  sudo docker rmi --force $i ; done

Use the step 1 in case you are getting error saying it cant be deleted owing to child dependencies

like image 30
chandank Avatar answered Oct 01 '22 11:10

chandank


If you want to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download the image meltwater/docker-cleanup.

I use this on production since we deploy several times a day on multiple servers, and I don't want to go to every server to clean up (that would be a pain).

Just run:

docker run -d -v /var/run/docker.sock:/var/run/docker.sock:rw  -v /var/lib/docker:/var/lib/docker:rw --restart=unless-stopped meltwater/docker-cleanup:latest

It will run every 30 minutes (or however long you set it using DELAY_TIME=1800 option) and clean up exited containers and images.

More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md

like image 21
Innocent Anigbo Avatar answered Oct 01 '22 12:10

Innocent Anigbo


Here's a shell script to remove a tagged (named) image and it's containers. Save as docker-rmi and run using 'docker-rmi my-image-name'

#!/bin/bash

IMAGE=$1

if [ "$IMAGE" == "" ] ; then
  echo "Missing image argument"
  exit 2
fi

docker ps -qa -f "ancestor=$IMAGE" | xargs docker rm
docker rmi $IMAGE
like image 22
Alex M Avatar answered Oct 01 '22 12:10

Alex M