Remove a Docker Image

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: July 26, 2024

The short answer

To remove and un-tag a single Docker image from your local machine, you can use the docker rmi command followed the the identifier of the image you want to remove:

Bash
$ docker rmi <image_id>

Note that you will first need to stop and remove all the containers currently using this image in order to be able to remove it.

You can learn more about this by reading our article on how to remove all stopped Docker containers.

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering docker remove image in the AI Command Search will prompt an docker command that can then quickly be inserted into your shell by doing CMD+ENTER.

Removing an image from the local machine

A Docker image can be removed using either its short or long identifier, its tag, or its digest. To get this information, you can run the docker images command, which will list all the locally downloaded images:

Bash
$ docker images

You can then use one of the methods described below to remove one or more images. Alternatively, you can also read our article on how to remove all Docker images at once.

Removing an image using its tag

A tag is a string of characters used to identify a specific version of an image (e.g. latest, v1.0).

To remove an image based on its tag, you can use the following syntax:

Bash
$ docker rmi <image:tag>

For example, the following command will remove the mysql image version 8:

Bash
$ docker rmi mysql:8

Note that if an image has one or more tags referencing it, you must remove all of them before the image is removed.

Removing an image using its digest

A Docker image digest is an immutable identifier created during the build time of the image.

Since a digest cannot be altered or tampered with, unlike a name or tag, it is sometimes preferable to use it in order to prevent the accidental removal of unwanted images, especially when working with scripts or automation.

To remove a Docker image using its digest, you can use the following syntax:

Bash
$ docker rmi <image>@<digest>

For example, the following command will remove the mysql image based on its digest value:

Bash
$ docker rmi mysql@sha256:15f069202c46cf861ce429423ae3f8dfa6423306fbf399eaef36094ce30dd75c

Note that to list all images including their digest, you can use the docker images --digests command.

Forcing the image removal

To force the removal of a Docker image, regardless of whether it is currently used by containers or has multiple tags associated with it, you can use the docker rmi command with the -f flag (short for force) as follows:

Bash
$ docker rmi -f <image>

Note that this command should be used with caution as it may provoke the unexpected disruption of running applications.

Removing an image from a Docker registry

To remove an image from an online Docker registry, you must first authentication to the registry using the docker login command:

Bash
$ docker login <registry_url>

List all the images present in the registry using the docker search command:

Bash
$ docker search <registry_url>

And finally, run the docker rmi command using the following syntax:

Bash
$ docker rmi <registry_url>/<image>:<tag>

For example:

Bash
$ docker rmi docker.io/library/nginx:latest

Removing an image from Artifactory

To remove a Docker image from Artifactory, you can use the following curl command:

Bash
$ curl -u <username>:<password> -X DELETE "<artifactory_url>/artifactory/<repository>/<image>:<tag>"

Where:

  • username is your Artifactory username.
  • password is your Artifactory password.
  • artifactory\_url is the URL of your Artifactory instance.
  • repository is the name of the repository where the Docker image is stored.
  • image is the name of the Docker image you want to remove.
  • tag is the tag of the Docker image you want to remove.
Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Learning Docker (The Easy Way) Using LazyDocker & Warp

A concise guide to learning Docker using Lazydocker. Highlights Docker’s benefits and takes advantage of Warp's AI features for a quick setup.

Run SSH In Docker

Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.

Override the Container Entrypoint With docker run

Learn how to override and customize the entrypoint of a Docker container using the docker run command.

The Dockerfile ARG Instruction

Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.

Start a Docker Container

Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.

Stop All Docker Containers

How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.

Set Docker Container Hostname

Learn how to set, change and match a docker container hostname.

How To Use An .env File In Docker Compose

Learn how define and pass environment variables to Docker containers using an .env file in Docker Compose.

Use An .env File In Docker

Learn how to write and use .env files in Docker to populate the environment of containers on startup.

Restart Docker Containers

Learn how to restart Docker containers automatically with restart policies and manually using the docker restart, docker start, docker stop and docker kill commands.

Run Bash Shell In Docker

Start an interactive shell in Docker container

Launch MySQL Using Docker Compose

Learn how to launch a MySQL container in Docker Compose.