Docker tutorial on private repositories

  • 2020-05-30 21:42:26
  • OfStack

Private warehouses

Sometimes it may not be convenient to use a public repository like Docker Hub, where users can create a local repository for private use.

This section describes how to use a local repository.

docker-registry is an official tool that can be used to build private mirror repositories.

Install and run docker-registry

Container operation

Once you have installed Docker, you can run it by getting the official registry image.


$ sudo docker run -d -p 5000:5000 registry

This will use the official registry image to start the local private repository. Users can configure the private repository location by specifying parameters, such as configuring the image store to the Amazon S3 service.


$ sudo docker run \
   -e SETTINGS_FLAVOR=s3 \
   -e AWS_BUCKET=acme-docker \
   -e STORAGE_PATH=/registry \
   -e AWS_KEY=AKIAHSHB43HS3J92MXZ \
   -e AWS_SECRET=xdDowwlK7TJajV1Y7EoOZrmuPEJlHYcNP2k4j49T \
   -e SEARCH_BACKEND=sqlalchemy \
   -p 5000:5000 \
   registry

In addition, you can specify a configuration file under a local path such as /home/user/ registry-conf.


$ sudo docker run -d -p 5000:5000 -v /home/user/registry-conf:/registry-conf -e 
DOCKER_REGISTRY_CONFIG=/registry-conf/config.yml registry

By default, the repository is created under /tmp/registry of the container. You can use the -v parameter to store the mirror file in the specified path locally. For example, the following example puts the uploaded image into the /opt/data/registry directory.


$ sudo docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry registry

The local installation

For distributions such as Ubuntu or CentOS, you can install directly from the source.

Ubuntu


$ sudo apt-get install -y build-essential python-dev libevent-dev python-pip liblzma-dev
$ sudo pip install docker-registry

CentOS


$ sudo yum install -y python-devel libevent-devel python-pip gcc xz-devel
$ sudo python-pip install docker-registry

You can also download the source code from the docker-registry project for installation.


$ sudo apt-get install build-essential python-dev libevent-dev python-pip libssl-dev liblzma-dev libffi-dev
$ git clone https://github.com/docker/docker-registry.git
$ cd docker-registry
$ sudo python setup.py install

Then modify the configuration file, mainly changing the storage_path path to the local repository in the dev template segment.


$ cp config/config_sample.yml config/config.yml

Then start the Web service.


$ sudo gunicorn -c contrib/gunicorn.py docker_registry.wsgi:application

or


$ sudo gunicorn --access-logfile - --error-logfile - -k gevent -b 0.0.0.0:5000 -w 4 --max-requests 100 docker_registry.wsgi:application

At this point, access the local 5000 port using curl and see the output version of docker-registry indicating a successful run.

Note: the config/ config_sample.yml file is a sample configuration file.

Upload, download, and search images from private repositories

Once you have created a private repository, you can tag an image with docker tag, then push it to the repository, where it can be downloaded from other machines. For example, the private warehouse address is 192.168.7.26:5000.

View the existing image locally first.


$ sudo docker run \
   -e SETTINGS_FLAVOR=s3 \
   -e AWS_BUCKET=acme-docker \
   -e STORAGE_PATH=/registry \
   -e AWS_KEY=AKIAHSHB43HS3J92MXZ \
   -e AWS_SECRET=xdDowwlK7TJajV1Y7EoOZrmuPEJlHYcNP2k4j49T \
   -e SEARCH_BACKEND=sqlalchemy \
   -p 5000:5000 \
   registry
0

This mirror will use docker tag ba58 marked as 192.168.7.26:5000 / test (format for docker tag IMAGE [: TAG] [REGISTRYHOST /] [USERNAME /] NAME [: TAG]).


$ sudo docker run \
   -e SETTINGS_FLAVOR=s3 \
   -e AWS_BUCKET=acme-docker \
   -e STORAGE_PATH=/registry \
   -e AWS_KEY=AKIAHSHB43HS3J92MXZ \
   -e AWS_SECRET=xdDowwlK7TJajV1Y7EoOZrmuPEJlHYcNP2k4j49T \
   -e SEARCH_BACKEND=sqlalchemy \
   -p 5000:5000 \
   registry
1

Upload the image of the tag using docker push.


$ sudo docker run \
   -e SETTINGS_FLAVOR=s3 \
   -e AWS_BUCKET=acme-docker \
   -e STORAGE_PATH=/registry \
   -e AWS_KEY=AKIAHSHB43HS3J92MXZ \
   -e AWS_SECRET=xdDowwlK7TJajV1Y7EoOZrmuPEJlHYcNP2k4j49T \
   -e SEARCH_BACKEND=sqlalchemy \
   -p 5000:5000 \
   registry
2

View the mirror in the warehouse with curl.


$ curl http://192.168.7.26:5000/v1/search
{"num_results": 7, "query": "", "results": 
[{"description": "", "name": "library/miaxis_j2ee"},
 {"description": "", "name": "library/tomcat"},
 {"description": "", "name": "library/ubuntu"}, 
{"description": "", "name": "library/ubuntu_office"}, 
{"description": "", "name": "library/desktop_ubu"}, 
{"description": "", "name": "dockerfile/ubuntu"}, 
{"description": "", "name": "library/test"}]}

Here you can see {"description": ", "name": "library/test"}, indicating that the image has been uploaded successfully.

You can now go to another machine and download the image.


$ sudo docker run \
   -e SETTINGS_FLAVOR=s3 \
   -e AWS_BUCKET=acme-docker \
   -e STORAGE_PATH=/registry \
   -e AWS_KEY=AKIAHSHB43HS3J92MXZ \
   -e AWS_SECRET=xdDowwlK7TJajV1Y7EoOZrmuPEJlHYcNP2k4j49T \
   -e SEARCH_BACKEND=sqlalchemy \
   -p 5000:5000 \
   registry
4

You can use this script to batch upload the local image to the registration server. The default is the local registration server 127.0.0.1:5000. Such as:


$ sudo docker run \
   -e SETTINGS_FLAVOR=s3 \
   -e AWS_BUCKET=acme-docker \
   -e STORAGE_PATH=/registry \
   -e AWS_KEY=AKIAHSHB43HS3J92MXZ \
   -e AWS_SECRET=xdDowwlK7TJajV1Y7EoOZrmuPEJlHYcNP2k4j49T \
   -e SEARCH_BACKEND=sqlalchemy \
   -p 5000:5000 \
   registry
5

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: