Docker Registry 私有仓库搭建背景与版本说明
Docker Registry 私有仓库搭建
虽然官方提供了丰富的 Linux 镜像供直接从公共仓库下载,但出于安全考量及提升内部访问速度,在局域网内自建私有仓库以存放专属镜像成为常见需求,而 Docker-Registry 便是满足这一需求的理想工具。
本次搭建
docker-registry server (dev) (v0.9.0)
修改HOSTS文件加上域名
为提升安全性,建议创建一个非 root 的 docker 用户来运行 registry 程序,并明确指定镜像存储路径,此处设定为 /home/docker_registry 目录。
useradd docker
mkdir -p /home/docker_registry
chown -R docker.docker /home/docker_registry/
从 GitHub 克隆最新版本的 registry 源码,进入其 config 子目录,并将模板配置文件复制一份作为实际配置:
git clone https://github.com/docker/docker-registry.git
cd docker-registry/config
cp config_sample.yml config.yml
随后需编辑 config.yml 文件,重点调整以下两项关键配置:
#配置sqlite数据库位置
sqlalchemy_index_database: _env:SQLALCHEMY_INDEX_DATABASE:sqlite:////home/docker_registry/docker-registry.db
#配置本地存储位置
local: &local
storage: local
storage_path: _env:STORAGE_PATH:/home/docker_registry
安装构建及运行 Docker-Registry 所需的必要软件包,包括相关的 Python 工具和库:
apt-get update
apt-get install build-essential python-dev liblzma-dev libevent-dev python-pip libssl-dev
在使用 apt-get 安装软件包时,系统常会提示插入 netinst 光盘:
Media change: please insert the disc labeled
若未插入光盘则无法继续安装,此时需编辑 /etc/apt/sources.list 文件,注释掉包含 cdrom 的行。
接着执行 apt-get update 命令更新 deb 软件源列表。
完成上述操作后,后续使用 apt-get 安装时将不再尝试搜索 cdrom 设备。
在 hosts 文件中添加域名映射,以便后续通过域名访问服务。
vim /etc/hosts
127.0.0.1 docker.registry.com
安装Nginx
apt-get install nginx
#配置Nginx config
vim /etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
upstream docker-registry {
server localhost:5000;
}
server {
listen 80;
server_name docker.registry.com;
proxy_set_header Host $http_host; # required for docker client's sake
proxy_set_header X-Real-IP $remote_addr; # pass on real client's IP
client_max_body_size 0; # disable any limits to avoid HTTP 413 for large image uploads
# required to avoid HTTP 411: see Issue #1486 (https://github.com/dotcloud/docker/issues/1486)
chunked_transfer_encoding on;
#
location / {
proxy_pass http://docker-registry;
}
}
}
启动Nginx
service nginx start
访问浏览器测试
http://192.168.124.130/

安装python依赖
cd /opt/docker-registry
pip install .
若在连接过程中遇到错误:Cannot connect to proxy. Socket error: [Errno -2] Name or service not known.
可通过手动安装依赖包并添加代理参数来解决此问题。
pip install -i http://pypi.v2ex.com/simple .
#注销下面的 pip install . 安装全部
--pip install -i http://pypi.v2ex.com/simple gunicorn
建立软连接
ln -s /usr/local/bin/gunicorn /usr/bin/gunicorn
Nginx 启动成功后,可使用 docker 用户执行以下命令进行初步测试启动:
gunicorn --access-logfile - --error-logfile - -k gevent -b 0.0.0.0:5000 -w 8 --max-requests 100 docker_registry.wsgi:application 访问浏览器
http://docker.registry.com

若页面显示正常输出,则表明 Docker Registry 已成功安装并运行。
需为数据库目录赋予相应权限,否则上传文件时将因无法写入数据库而失败。
chmod 777 /home/docker_registry/repositories/docker-registry.db
使用 supervisord 对进程进行监控和管理。
apt-get install supervisor
配置supervisor [docker-registry]
vim /etc/supervisor/conf.d/docker-registry.conf
[program:docker-registry]
directory=/opt/docker-registry
#使用docker用户
user=docker
command=/usr/local/bin/gunicorn --access-logfile - --error-logfile - -k gevent -b 0.0.0.0:5000 -w 8 --max-requests 100 --graceful-timeout 3600 -t 3600 docker_registry.wsgi:application
redirect_stderr=true
stderr_logfile=none
stdout_logfile=/var/log/supervisor/docker-registry.log
autostart=true
autorestart=true
#重新加载 supervisor 配置:
supervisorctl
supervisor> reread
docker-registry: available
supervisor> update
docker-registry: added process group
supervisor> status
docker-registry RUNNING pid 4371, uptime 0:00:01
查看端口占用
netstat -apn | grep 5000
启动重启
service supervisor start #/etc/init.d/supervisord {start|stop|restart|force-reload|status|force-stop}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!