Docker has a free public registry, Docker Hub, that can host your custom Docker images, but there are situations where you will not want your image to be publicly available. Images typically contain all the code necessary to run an application, so using a private registry is preferable when using proprietary software.
In this post, you will set up and secure your own private Docker Registry.
Run a local registry
Creating a Local Registry is not different to running any other container, so we will follow the usual steps we learn on previous posts. Use a command like the following to start the registry container:
#docker run -d -p 5000:5000 --restart=always --name registry registry:2
Unable to find image 'registry:2' locally
2: Pulling from library/registry
c87736221ed0: Pull complete
1cc8e0bb44df: Pull complete
54d33bcb37f5: Pull complete
e8afc091c171: Pull complete
b4541f6d3db6: Pull complete
Digest: sha256:3b00e5438ebd8835bcfa7bf5246445a6b57b9a50473e89c02ecc8e575be3ebb5
Status: Downloaded newer image for registry:2
99d9b2ede2ea389a4b1837afadf89868108dd62f3af585de9c38303ddbd0f6bb
Lets check now that the local registry is running:
#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99d9b2ede2ea registry:2 "/entrypoint.sh /etc…" 36 seconds ago Up 35 seconds 0.0.0.0:5000->5000/tcp registry
We can see the local registry is UP and Running, listening on port 5000/tcp.
The registry is now ready to use.
Pushing an image to our new registry
Now that we have our local registry, we are going to push the image we created in the previous post to the local registry.
Before we can push the image to the registry we need to tell Docker that we want to push it to the local registry instead to Docker Hub.
To do that, we tag the image as follows:
docker tag static_web localhost:5000/static_web
This command will tag the image as localhost:5000/static_web. This creates an additional tag for the existing image. When the first part of the tag is a hostname and port, Docker interprets this as the location of a registry, when pushing.
Now we can try pushing the image to the local registry:
#docker push localhost:5000/static_web
docker push localhost:5000/static_web
The push refers to repository [localhost:5000/static_web]
23cc06f319e8: Pushed
f7c6590a6a8a: Pushed
2d6225362742: Pushed
26e16f1d4b32: Pushed
297fd071ca2f: Pushed
2f0d1e8214b2: Pushed
7dd604ffa87f: Pushed
aa54c2bc1229: Pushed
latest: digest: sha256:5f1ad3f9df94315b1b47542aa178f3a5fbee713d88aeba6cbfcf2696f3f1bed8 size: 1987
Now Lets Remove the locally-cached static_web and localhost:5000/static_web images, so that you can test pulling the image from your registry. This does not remove the localhost:5000/static_web image from your registry.
#docker image remove static_web
Untagged: static_web:latest
#docker image remove localhost:5000/static_web -f
Untagged: localhost:5000/static_web:latest
Untagged: localhost:5000/static_web@sha256:5f1ad3f9df94315b1b47542aa178f3a5fbee713d88aeba6cbfcf2696f3f1bed8
Deleted: sha256:85190ea11dca718966e65ce1a5445eb34b7e7fb2a2d0ecbabf275f80088c5468
Now, if we run docker images we shouldn't see the static_web image:
#docker images static_web
REPOSITORY TAG IMAGE ID CREATED SIZE
#
Now, if we run docker images we shouldn’t see the static_web image:
#docker images static_web
REPOSITORY TAG IMAGE ID CREATED SIZE
#
At Last we can Pull the localhost:5000/static_web image from your local registry.
# docker pull localhost:5000/static_web
Using default tag: latest
latest: Pulling from static_web
34667c7e4631: Already exists
d18d76a881a4: Already exists
119c7358fbfc: Already exists
2aaf13f3eff0: Already exists
644067bf57c7: Already exists
27c1fcd390b1: Already exists
29ddb2eff7f5: Already exists
6dd2bd08049b: Already exists
Digest: sha256:5f1ad3f9df94315b1b47542aa178f3a5fbee713d88aeba6cbfcf2696f3f1bed8
Status: Downloaded newer image for localhost:5000/static_web:latest
root@ip-172-31-89-180:/home/ubuntu# docker images static_web
REPOSITORY TAG IMAGE ID CREATED SIZE
root@ip-172-31-89-180:/home/ubuntu# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost:5000/static_web latest 85190ea11dca 18 hours ago 212MB
Stopping the local registry
To stop the registry, use the same docker container stop command as with any other container.
#docker container stop registry
To remove the container, use docker container rm.
#docker container stop registry && docker container rm -v registry