There are several ways to set up a private repository using Docker:
-
- Docker Hub Official Private Image Repository
-
- Local Private Repository
Setting up an official private image repository is very simple (it just requires some magic, otherwise you can go to the second method asynchronously), you only need a few commands like login, pull, tag, and push. On the other hand, setting up a local private image repository is a bit more complicated and can only be stored locally and not publicly.
Now let's briefly introduce how to set up your own private repository!
Official Image Repository#
First, we need to apply for and log in to an account on Docker Hub, and also log in to Docker Desktop. After logging in, we first use the command line to log in
docker login
If Login Succeeded appears, it means that the login was successful!
Note: Your authentication credentials will be stored in the .dockercfg file in the local directory.
Next, let's check the local images. You can see that there is an nginx
here, we will use this for testing later!
docker images
First, tag the image, which means renaming it.
Note: The renaming format should be username/image name number
docker tag nginx codegetters/nginx:1.0.0
Check to see if the tag
was successfully added
At this point, we have completed most of the work, we just need to push
it to our own account.
Note: The image name for push should include the full name, including the version number, otherwise it will fail
docker push codegetters/nginx:1.0.0
After completion, we can delete the local image, but deletion is not our focus, so we won't show it too much. We then go to Docker Hub and log in to our own account. Click on Repositories to see that we already have nginx
in our repository.
At this point, we are done. If you want to pull from your own repository, just execute the following command
docker pull codegetters/nginx:1.0.0
Local Private Repository#
Why use a private repository?
Generally, companies do not allow us to upload projects to public repositories like Docker Hub, so it is necessary to learn how to create a private repository. Although images can be stored on hub.docker, the network speed is relatively slow. It is a better solution to set up a private repository for internal use.
The official Docker Hub provides a public centralized repository. However, accessing Docker Hub locally is often slow, and many times a local private repository is needed for internal use only.
The registry is the service that manages Docker images. The following operations mainly use the registry image.
First, let's pull the registry
image
docker pull registry
Then, let registry
run and mount the /var/lib/registry
of the image to our local directory E:\software_cache\docker
, and map the local port 5000 to the image
docker run -d -p 5000:5000 -v E:\software_cache\docker:/var/lib/registry --restart=always registry
After successful execution, you can access ip:5000/v2
. If the status is the same as the image below, it means it has been successfully run.
Access ip:5000/v2/_catalog
to view the image repository. The empty array here indicates that no images have been uploaded yet.
Next, open Docker Desktop and go to settings, add your own ip:5000
to the array below (if it doesn't exist, you need to add it yourself) to allow local image pushing. If you are using Linux, you need to find /etc/docker/daemon.json
(create it if it doesn't exist)
"insecure-registries": [
"http://xxx.xxx.xxx.xxx:5000"
],
After completing the settings, we can follow the above steps. First, tag the image and check it
Note: The renaming format should be username/image name number
docker tag nginx xxx.xxx.xxx.xxx:5000/nginx:1.0.0
docker images
After tagging, push the image to our local repository
Note: The image name for push should include the full name, including the version number, otherwise it will fail
docker push xxx.xxx.xxx.xxx:5000/nginx:1.0.0
At this point, we can access the page we just visited, and it will show the image we just pushed - nginx
Of course, we can also use the command to check if the push was successful
curl http://xxx.xxx.xxx.xxx:5000/v2/_catalog
You can also use this command to access the tag list of the local repository
curl http://xxx.xxx.xxx.xxx:5000/v2/_catalog | python -m json.tool
Let's run an image in the registry
to see if it runs successfully. Here, I mapped the local port 80
to port 80
in nginx
docker run -d -p 80:80 --name nginx xxx.xxx.xxx.xxx:5000/nginx:1.0.0
Let's access the local ip:80
and if the nginx page appears, it means the setup was successful!
Using Harbor to Manage Repositories#
What is Harbor?
Harbor is an enterprise-level container image repository open-sourced by VMware. It has the following features:
User interface for management
Role-based access control
LDAP/AD integration and log auditing for basic operations
Harbor has three installation methods (here I choose offline installation):
- Online installation: Download Harbor-related images from Docker Hub, so the installation package is very small
- Offline installation: The installation package contains the deployed images, so the installation package is relatively large
- OVA installation program (third-party): When users have a vCenter environment, they can use this installation program to deploy OVA and start Harbor
Prerequisites: Download the Harbor online installation package and extract the harbor-offline-installer-vx.x.x.tgz locally. Then copy and rename the harbor.yml.tmpl
file to harbor.yml
, and open it with a text editor to modify the hostname
to the local IP address. If you are not using https
for access (usually for internal use), then just comment out the related configurations. Only comment out the content related to https, and it is better not to modify other content.
Then open the command line and go to this directory, use docker to load the local image
docker load -i harbor.v2.10.1.tar.gz
After loading is complete, check that the Harbor local image has been successfully loaded
Use ./prepare
to preprocess some files and initialize files
Next, execute install.sh
to start the installation