Situatie
Docker images can take up significant disk space over time, especially if you frequently build and pull images.
Solutie
Step 1: List Docker Images
Before deleting any images, it’s a good idea to first list all the images currently available on your system.
Command:
docker images
Explanation: This command will display a list of all images, showing the repository, tag, image ID, creation date, and size.
Step 2: Identify the Image(s) to Delete
From the list generated by the previous command, identify the image(s) you want to delete. You can choose an image by its IMAGE ID, REPOSITORY, or TAG.
Hint:
- IMAGE ID is a unique identifier for each image, which is the safest option to use.
- REPOSITORYand TAG refer to the name and version of the image, respectively.
Step 3: Delete the Docker Image
To delete a specific Docker image, use the docker rmi command followed by the IMAGE ID, REPOSITORY:TAG, or IMAGE NAME.
Command:
docker rmi <image_id>
Or, if you prefer to use the repository and tag:
docker rmi <repository>:<tag>
Explanation:
- This command will remove the image specified.
- If the image is being used by a running container, Docker will prevent you from deleting it to avoid disruptions.
Example:
docker rmi 7d9495d03763
or
docker rmi ubuntu:latest
Step 4: Force Delete an Image (Optional)
If an image has multiple tags or is used by a stopped container, Docker may not delete it immediately. In such cases, you can forcefully remove the image.
Command:
docker rmi -f <image_id>
Explanation:
- The -for –force flag forces Docker to delete the image even if it is tagged multiple times or is associated with stopped containers.
Warning:
- Use this command with caution as it can lead to unintended consequences if the image is still in use.
Step 5: Delete All Unused Images (Optional)
If you want to clean up all unused images (dangling images), you can use the docker image prune command.
Command:
docker image prune
Explanation:
- This command deletes all dangling images—images that are not tagged and are not referenced by any container.
- Docker will prompt you for confirmation before proceeding.
Hint: To remove all unused images, not just the dangling ones, use:
docker image prune -a
Step 6: Verify the Deletion
After deleting the image(s), you can verify that they have been removed by listing the images again.
Command:
docker images
Explanation:
- If the image was successfully deleted, it will no longer appear in the list.
Leave A Comment?