A Docker registry is a stateless, highly scalable server-side application that stores and lets you distribute Docker images. The registry is open source under the permissive Apache license. It is a storage and distribution system where all your Docker servers can connect and upload and download images as and when needed. It acts as a distribution site for your images.
A Docker registry contains several Docker repositories. A Docker repository holds several versions of a specific image. For example, all the versions of the nginx image are stored within a single repository within Docker Hub called nginx.
By default, Docker interacts with its public Docker registry instance, called Docker Hub, which helps you distribute your images to the broader open source community.
Not all images can be public and open source, and many proprietary activities are ongoing. Docker allows you to use a private Docker registry for a scenario you can host within your infrastructure called Docker Trusted Registry. Several online options are available, including using a SaaS service, such as GCR, or creating private repositories at Docker Hub.
While the SaaS option is readily available and intuitive, let’s consider hosting our private Docker registry.
Hosting your private Docker registry
Docker provides an image that you can run on any server that has Docker installed. Once the container is up and running, you can use that as the Docker registry. Let’s have a look:
$ docker run -d -p 80:5000 –restart=always –name registry registry:2 Unable to find image ‘registry:2’ locally
- Pulling from library/registry 8a49fdb3b6a5: Already exists 58116d8bf569: Pull complete 4cb4a93be51c: Pull complete cbdeff65a266: Pull complete 6b102b34ed3d: Pull complete Digest: sha256:20d08472…
Status: Downloaded newer image for registry:2 ae4c4ec9fc7b17733694160b5b3b053bd1a41475dc4282f3eccaa10…
Since we know that the registry is running on localhost and listening on port 80, let’s try to push an image to this registry. First, let’s tag the image to specify localhost as the registry. We will add a registry location at the beginning of the Docker tag so that Docker knows where to push the image. We already know that the structure of a Docker tag is <registry_url>/<user>/<image_ name>:<image_version>. We will use the docker tag command to give another name to an existing image, as shown in the following command:
$ docker tag your_dockerhub_user>/nginx-hello-world:latest \ localhost/<your_dockerhub_user>/nginx-hello-world:latest
Now, we can go ahead and push the image to the local Docker registry:
$ docker push localhost/<your_dockerhub_user>/nginx-hello-world:latest
The push refers to repository [localhost/your_dockerhub_user/nginx-hello-world]
2b7de406bdcd: Pushed
5f70bf18a086: Pushed
845348333310: Pushed
96a9e6a097c6: Pushed
548a79621a42: Pushed
latest: digest: sha256:6ad07e74… size: 1366