0%

Build Your Own Docker Registry

Most Docker images normally come from the official Docker index, but in some environments that is either too slow or not appropriate for private images that include internal code. In that case, setting up your own Docker image registry becomes a practical option.

Docker provided a tool called Docker Registry for self-hosting an image index on a local network, which works well for sharing images inside a company.

This post records one installation process as a reference.

Environment

I used CentOS 6.5 x64. Docker Registry was written in Python, so the environment requirements were not especially strict.

Installation

Install dependencies

1
yum install python-devel libevent-devel python-setuptools gcc xz-devel

Add a docker user and storage directory

For safety, create a dedicated docker user and a storage directory for registry data:

1
2
3
useradd docker
mkdir -p /opt/docker_data/registry
chown -R docker:docker /opt/docker_data/registry

Install with easy_install

Since Docker Registry is a Python application, it can be installed directly with easy_install (or pip):

1
easy_install docker-registry

Configure Docker Registry

After installation with easy_install, the package may end up in a path like:

1
/usr/lib/python2.6/site-packages/docker_registry-0.7.3-py2.6.egg

Go into its config directory and copy the sample config:

1
2
cd /usr/lib/python2.6/site-packages/docker_registry-0.7.3-py2.6.egg/config
cp config_sample.yml config.yml

Then update at least these two settings in config.yml:

1
2
3
4
5
sqlalchemy_index_database: _env:SQLALCHEMY_INDEX_DATABASE:sqlite://///opt/docker_data/registry/docker-registry.db

local: &local
storage: local
storage_path: _env:STORAGE_PATH:/opt/docker_data/registry

Configure Nginx

For convenience, you can register a domain such as docker.knktc.com in your local DNS and proxy it through Nginx.

Example configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
upstream docker-registry {
server localhost:5000;
}

server {
listen 80;
server_name docker.knktc.com;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;

client_max_body_size 0;
chunked_transfer_encoding on;

location / {
proxy_pass http://docker-registry;
}
}

Test startup

After starting Nginx, test the registry as the docker user:

1
/usr/bin/gunicorn --debug -k gevent -b 0.0.0.0:5000 -w 8 docker_registry.wsgi:application

You may hit several import errors at first. If so, just install the missing Python modules one by one.

If browsing to http://docker.knktc.com returns:

1
"docker-registry server (dev) (v0.7.3)"

then the registry is running successfully.

Manage it with supervisord

Both official and community guides recommended using supervisord to keep Docker Registry running.

Example program section:

1
2
3
4
5
6
7
8
[program:docker-registry]
user=docker
command=/usr/bin/gunicorn --debug -k gevent -b 0.0.0.0:5000 -w 8 docker_registry.wsgi:application
redirect_stderr=true
stderr_logfile=/var/log/supervisor/docker-registry-error.log
stdout_logfile=/var/log/supervisor/docker-registry.log
autostart=true
autorestart=true

Usage test

After the registry is ready, you can tag a local image to push it there:

1
2
3
docker tag localimage docker.knktc.com/knktc/testimage
docker push docker.knktc.com/knktc/testimage
docker pull docker.knktc.com/knktc/testimage

References

如果我的文字帮到了您,那么可不可以请我喝罐可乐?