A Docker registry serves as a centralized repository for storing, sharing, and managing Docker images. Whether you’re using a public service like Docker Hub or hosting your own registry, it enables efficient collaboration and deployment of containerized applications.
This article explores Docker registries, their types, and the essential commands for pushing and pulling images.
Table of Contents
What is a Docker Registry?
A Docker registry is a storage system where Docker images are stored and managed. It supports the following operations:
- Pushing: Uploading Docker images for others to use.
- Pulling: Downloading images for use in local or remote environments.
- Versioning: Managing different versions of the same application image.
Docker Hub and Self-Hosted Registries
Docker Hub (Public Registry)
- A widely used public registry maintained by Docker Inc.
- Provides pre-built images for popular applications like Nginx, MySQL, and Redis.
- Free tier available, but private repositories may require a subscription.
Self-Hosted Registries (Private Registry)
- Organizations often host their own registries for greater control and security.
- Tools like Docker’s registry image or third-party solutions (e.g., Harbor, Nexus) are used for self-hosting.
Example of running a simple self-hosted registry:
docker run -d -p 5000:5000 --name registry registry:2
Pulling and Pushing Images to a Registry
Pulling an Image
Example:
docker pull <registry>/<image>:<tag>
From Docker Hub:
docker pull nginx:latest
From Private Registry:
docker pull myregistry.com/myimage:1.0
Tagging an Image
Add a registry and repository name to your local image:
docker tag <local-image>:<tag> <registry>/<repository>/<image>:<tag>
Example:
docker tag myapp:latest myregistry.com/myapp:1.0
Pushing an Image
Authenticate with the registry if needed:
docker login <registry>
Push the tagged image:
docker push <registry>/<repository>/<image>:<tag>
Example:
docker push myregistry.com/myapp:1.0
Example: Setting Up and Using a Self-Hosted Registry
Run the Registry:
docker run -d -p 5000:5000 --name registry registry:2
Tag and Push an Image:
docker tag nginx:latest localhost:5000/nginx:latest
docker push localhost:5000/nginx:latest
Pull the Image from the Registry:
docker pull localhost:5000/nginx:latest
Local Repository Example
docker run -d --name registry -p 5000:5000 --restart always registry:latest
docker pull nginx
docker pull redis
docker tag nginx:latest localhost:5000/my-repo-nginx:latest
docker tag redis:latest localhost:5000/my-repo-redis:latest
curl http://localhost:5000/v2/_catalog
docker pull localhost:5000/my-repo-redis
docker exec -it <ctr-id> sh
/var/lib/registry/docker/registry/v2/repositories
Public Repository Example
docker login
docker build -t bxtgeek/todo:latest .
docker push bxtgeek/todo
docker tag bxtgeek/todo localhost:5000/my-repo-todo
Building Containers from Public and Local Repositories
docker run -d --name public-todo -p 80:8080 bxtgeek/todo
docker run -d --name local-todo -p 81:8080 localhost:5000/my-repo-todo
Lab Exercise
Set up a Docker registry, push images to it, and pull them back to verify functionality.
Steps
- Set up the Registry
- Run the following command to start the Docker registry:
docker run -d -p 5000:5000 --name registry registry:2
- Pull Base Images
- Pull the required images from Docker Hub:
docker pull nginx:latest docker pull redis:latest
- Tag Images
- Tag the images with the local registry URL:
docker tag nginx:latest localhost:5000/nginx:latest docker tag redis:latest localhost:5000/redis:latest
- Push Images to Registry
- Push the tagged images to the registry:
docker push localhost:5000/nginx:latest docker push localhost:5000/redis:latest
- List Available Images
- Use the following command to list available images in the registry:
curl http://localhost:5000/v2/_catalog
- Pull Images from Registry
- Pull the images from the registry:
docker pull localhost:5000/nginx:latest docker pull localhost:5000/redis:latest
- Clean Up
- Stop and remove the registry container:
docker stop registry docker rm registry
Benefits of Using a Docker Registry
- Centralized Storage: Simplify access to Docker images.
- Collaboration: Share images with team members or the public.
- Version Control: Manage multiple versions of the same application.
- Security: Control access with private registries and authentication.
[Video] Docker Registry Explained: Managing Your Container Images Effectively
Conclusion
Docker registries are indispensable for managing container images efficiently. Whether leveraging Docker Hub for public images or setting up a private registry for internal use, mastering these workflows enhances collaboration and deployment flexibility.
Explore Docker registries today to streamline your containerization journey!