DEV Community

Raunak Jain
Raunak Jain

Posted on

How to delete images from a private docker registry?

A private docker registry lets you store your docker images in your own space. Sometimes, you may want to delete old images to free up disk space or keep your registry clean. In this guide, I will show you how to delete images from a private docker registry in simple words. I use short sentences and basic grammar so it is easy to follow.


Introduction

When you work with docker images, they build up over time. A private registry is useful because you control it. However, as you push many images, your registry can grow large. Deleting unwanted images is important. It helps with saving space and keeping your repository organized.

Before you start, you need to understand some basics of docker registries. You may want to read about what are docker image registries and how do they work to get an idea of how the system stores your images.

Also, knowing about docker repositories and how they work can be useful. Repositories hold your images and often have many tags. This makes image management a bit more challenging.


Understanding Image Deletion in a Private Registry

By default, a private docker registry does not let you delete images easily. Many times, deletion is disabled. This is done to avoid accidental loss of data. To remove images, you need to do a few extra steps.

There are two parts to deleting an image:

  1. Removing the manifest – This is a file that tells the registry about the image and its layers.
  2. Running garbage collection – This step cleans up the storage by removing unused layers.

Before you delete, make sure you have the correct tag and digest. Docker tags are important because they let you know which version of an image you are working with. For more on this, check out what are docker tags and why are they important.


Step 1: Enable Deletion in the Registry

If you set up your private registry from the official image, deletion might be disabled by default. You must enable deletion in the registry configuration file. Here is a simple step:

  1. Locate your registry configuration file (commonly named config.yml).
  2. Under the storage section, add or set the delete option:
   storage:
     delete:
       enabled: true
Enter fullscreen mode Exit fullscreen mode
  1. Restart your registry container so that changes take effect.

By enabling deletion, you allow the registry API to accept DELETE requests. This is a required step before you remove any images.


Step 2: Identify the Image to Delete

You must know the image name and its tag or digest. An image digest is a unique identifier for a particular image. The deletion process works with the digest, not the tag.

To find the digest, use the docker registry API. For example, you can list tags and get the manifest with a command like:

curl -s -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
  http://<registry_host>:<port>/v2/<repository_name>/manifests/<tag>
Enter fullscreen mode Exit fullscreen mode

Replace <registry_host>, <port>, <repository_name>, and <tag> with your values. In the output, look for a field named Docker-Content-Digest. This is the digest you need to delete.


Step 3: Delete the Image Manifest

Now that you have the digest, you can delete the image by sending a DELETE request to the registry API. Use a command like this:

curl -v -X DELETE http://<registry_host>:<port>/v2/<repository_name>/manifests/<digest>
Enter fullscreen mode Exit fullscreen mode

For example, if your registry runs on localhost at port 5000 and your repository is called "myapp", the command may look like:

curl -v -X DELETE http://localhost:5000/v2/myapp/manifests/sha256:123456abcdef...
Enter fullscreen mode Exit fullscreen mode

This command tells the registry to remove the manifest. When the manifest is deleted, the image is no longer available for pulling. However, the storage layers might still exist.


Step 4: Run Garbage Collection

Deleting the manifest does not immediately free the disk space. Docker registry uses a garbage collection process to remove unreferenced layers. To run garbage collection, follow these steps:

  1. Stop the registry container. This is important because garbage collection should run when the registry is not active.
  2. Run the garbage collection command inside the registry container. If you use the official registry image, use a command like:
   docker exec <registry_container_name> \
     bin/registry garbage-collect /etc/docker/registry/config.yml
Enter fullscreen mode Exit fullscreen mode

Replace <registry_container_name> with the name of your container. This command goes through the storage and deletes layers that are no longer referenced by any manifest.

  1. Restart your registry container.

After garbage collection, the deleted image’s layers will be removed from storage and you will free up disk space.


Step 5: Verify the Deletion

To check that the image has been deleted, you can try to pull the image from your private registry. If you get an error saying that the image is not found, then the deletion was successful.

You can also use the registry API to list repositories and tags to see if the image no longer appears. Keeping your registry tidy is important in managing your docker environment.


Common Issues and Troubleshooting

Sometimes, the deletion process might not work as expected. Here are some common issues and fixes:

1. Deletion Not Enabled

If you get a 405 Method Not Allowed error, deletion might not be enabled in your registry config. Check your config.yml file and restart the registry.

2. Incorrect Digest

Ensure that you are using the correct digest from the manifest. A wrong digest means the DELETE command will not work. Use the proper API calls to retrieve the digest.

3. Garbage Collection Fails

Garbage collection must run when the registry is stopped. If it fails, check the registry logs for any errors. Sometimes, permissions or corrupted layers may cause issues.

4. Cache Issues

If you are using a proxy or caching layer in front of your registry, you may see old images. In this case, clear the cache or wait for it to update.

By understanding these common problems, you can better manage your private registry and keep it in good shape.


Advanced Considerations

For more advanced users, deleting images from a private registry in a production environment may need extra care. Some things to think about include:

  • Automating the Deletion Process:

    If you regularly need to delete old images, you can write scripts that use the registry API. Automating garbage collection can also help maintain a clean registry.

  • Multi-Cloud Deployments:

    If you use your private registry in multi-cloud deployments, the deletion process may need to be integrated with your cloud storage solution. For more on using docker in multi-cloud scenarios, you can read about using docker for multi-cloud deployments.

  • Retention Policies:

    Consider setting up retention policies. These policies can automatically delete images that are older than a certain date. This is useful when you have many builds and do not need all of them.

  • Security and Access Control:

    Be careful when deleting images. Ensure that only authorized users can delete images from the registry. This can help prevent accidental or malicious deletion.

Taking these extra steps can help you manage your registry in a secure and efficient way.


Best Practices for Deleting Images

When you delete images from a private docker registry, follow these best practices:

  • Always Back Up Important Data:

    Before deleting, make sure you have a backup if the image is still needed. This is important if you accidentally delete the wrong image.

  • Document the Process:

    Write clear documentation of your deletion process. This helps you and your team understand the steps involved.

  • Test on a Staging Environment:

    Try the deletion process on a staging registry before doing it on production. This way, you can see how the process works without risking important data.

  • Use Proper API Calls:

    Rely on the official docker registry API to delete images. This ensures that you are using a supported and tested method.

  • Monitor Your Registry:

    After deletion and garbage collection, monitor your registry to ensure that the storage space is reclaimed and that no old data remains.

Following these practices will help maintain a healthy and organized private docker registry.


Real-World Example

Imagine you have a private docker registry that stores several versions of a web application. You want to remove old images that are no longer used. Here is a step-by-step example:

  1. Enable Deletion: Update your config.yml with:
   storage:
     delete:
       enabled: true
Enter fullscreen mode Exit fullscreen mode

Then restart your registry container.

  1. Find the Image Digest: List the manifest for your image by running:
   curl -s -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
     http://localhost:5000/v2/mywebapp/manifests/latest
Enter fullscreen mode Exit fullscreen mode

Note the Docker-Content-Digest from the output.

  1. Delete the Manifest: Use the digest in a DELETE request:
   curl -v -X DELETE http://localhost:5000/v2/mywebapp/manifests/sha256:abcdef123456...
Enter fullscreen mode Exit fullscreen mode
  1. Run Garbage Collection: Stop the registry container and run:
   docker exec registry_container_name bin/registry garbage-collect /etc/docker/registry/config.yml
Enter fullscreen mode Exit fullscreen mode

Restart the registry after completion.

  1. Verify: Try to pull the image or list tags to ensure it is deleted.

This real-world workflow shows how you can remove unwanted images safely from your registry.


Conclusion

Deleting images from a private docker registry is not as simple as running a single command. You need to enable deletion in your registry configuration, delete the image manifest using the API, and then run garbage collection to reclaim disk space. Each step is important to ensure that your registry remains clean and efficient.

Remember, understanding how docker repositories and tags work is key to managing your images. You may also want to check out topics like how do you push and pull docker images from docker hub to improve your overall image management process.

For those looking to scale and manage registries in more complex environments, reading about docker image registries will provide additional insight. And if you plan to deploy in varied environments, consider the challenges of using docker for multi-cloud deployments as part of your long-term strategy.

I hope this guide helps you understand how to delete images from a private docker registry. Follow the steps carefully and always test your changes in a safe environment. Happy containerizing and best of luck with your projects!

Top comments (0)