What is a container registry? In this article, we’ll dive into what a container registry is, how it differs from a repository, and why both are essential for containerized applications. We’ll also cover how to use public and private repositories on Docker Hub. So, first things first: What exactly is a container registry? The terms registry and repository in Docker are closely related but refer to different things. Here’s a breakdown of each:
Container Registry
A container registry is a service where Docker images are stored and managed. It’s essentially a centralized location that hosts one or more repositories, allowing users to store, share, and distribute Docker images. Think of it as a server or library where images are kept and managed.
Common examples of container registries include:
- Docker Hub: A public registry hosted by Docker where users can share images publicly or privately.
- Amazon ECR (Elastic Container Registry): A private registry service by AWS.
- Google Container Registry: A private registry service by Google Cloud.
- Private Registries: Organizations can also set up their own private container registries within their infrastructure, often using open-source tools like Harbor.
Repository
A repository is a specific location within a registry where a particular image, or a set of related images, is stored. Repositories are used to organize images, often by software name or project. For example, you might have a repository for nginx or my-app.
Each repository can contain multiple versions (or tags) of an image. For example, the nginx repository on Docker Hub has tags like latest, 1.19, 1.20, which represent different versions of the nginx image.
Example to Illustrate:
- Registry: Docker Hub (the entire platform).
- Repository: nginx (a specific project or application within Docker Hub).
When you refer to an image with docker pull nginx:latest, you are:
- Accessing the registry (Docker Hub by default unless specified otherwise).
- Pulling an image from a specific repository (nginx).
- Fetching a specific tag or version (latest).
Docker Hub is the most popular public registry out there, and it’s an excellent resource for anyone working with Docker. Docker Hub hosts official images for many popular applications, libraries, and even complete environments. You’ll find images for popular applications like MySQL, NGINX, Redis, and even the operating systems you may want to use in your containers.
Working with a Public Repository
Let’s jump into some hands-on examples. First, we’ll start with public repositories, which are visible to everyone.
- Install Docker Desktop. This is the application you need to run Docker commands and it’s available for Windows, Mac, and Linux.
- Go to Docker Hub, and if you don’t have an account, create one—it’s free and only takes a minute.
- Once you’re logged in, you can search for any public image you might need. Look up the ‘nginx’ image, a popular web server.
- Make sure you’re logged into your Docker Hub account in Docker Desktop so that you can pull and push images to and from your registry (we’ll get into that in a bit).
- Switch to your terminal in Docker Desktop. We can pull the nginx image from Docker Hub by running this command:
docker pull nginx
You can verify that the image has been pulled by going to the images tab.
This command pulls the latest ‘nginx’ image from Docker Hub to your local machine. After pulling, you can run it using a simple command like this:
docker run -d -p 8080:80 nginx
And just like that, you’ve started an nginx server on your machine! Now, you can access it at http://localhost:8080. With public images, it’s that easy to start running well-maintained applications. But let’s say you want to store this image in your own public repository.
Go to Docker Hub and on the repositories tab, click create repository. Name the repository nginx and set it to public. Then, click Create. Notice how it shows you the docker command to push to this repository. You just need to have your username in the tag for the image so let’s go ahead and set that up.
Back in the terminal, type the command:
docker tag nginx username/nginx:v1
This will set a tag that is linked to your repository and it includes the version which is a best practice. Now we can push this to our public repository on docker hub by typing:
docker push username/nginx:v1
Once that’s done, you can verify that the image was pushed by going to the repository in Docker Hub.
Working with a Private Repository
Public images are great, but what if you want to store a private image that only you or your team members can access? Docker Hub offers private repositories for this.
- On Docker Hub, go to your dashboard and click on ‘Create Repository.’
- Give your repository a name, I’ll call this nginx-private, set it to ‘Private,’ and click ‘Create.’
- If you were adding team members to this repository, you would do that on the collaborators tab which requires a paid subscription.
- Now, let’s tag and push an image from your local environment to this private repository.
Back in the terminal, we’re going to do the same thing we did earlier for the public repository. We’ll tag our image:
docker tag nginx username/nginx-private:v1
Then, we can push this image to our private repository by typing:
docker push sbenhoff/nginx-private:v1
This command works because we are logged into Docker Hub via Docker Desktop. If you’re using a different terminal, you would have to log into your Docker Hub account using the docker login command. And again, once that’s done, you can verify that the image was pushed by going to the repository in Docker Hub.
To pull the private image from Docker Hub on another machine or by a team member, they’ll need Docker Hub credentials and permission to access the repository.
Once logged in, they can pull the image using the standard command format:
docker pull username/nginx-private:v1
After pulling, they can run the image just like any other container. With Docker Hub’s private repositories, you have more control over who can access your sensitive or work-specific images.
Conclusion
So, to summarize:
- A container registry, like Docker Hub, stores your Docker images and can be public or private.
- Public images on Docker Hub are freely available for everyone, which is fantastic for quick access to popular software.
- Private repositories on Docker Hub let you secure images for specific teams or purposes, giving you added privacy and control.
Get Valuable Learning Resources Delivered To Your Inbox Every Wednesday!
You can learn more about this topic in my Tech Career Newsletter. This newsletter will give you insight into multiple stages of your career so that you can form your career plan, overcome learning blockers, and stay competitive in today’s crowded job market. It includes featured content from authors and influencers in my network to help you navigate your career and learn new skills. Subscribe today and set yourself up for career success!
Top comments (0)