创建Docker Hub账户
登录和上传镜像到Hub.docker.com
docker login //登陆hub.docker.com
docker tag ubutun1404-baseimage:1.0 jwu049/ubutun1404-baseimage:1.0 //标记本地镜像,将其归入某一仓库。
docker images
-------
REPOSITORY TAG IMAGE ID CREATED SIZE
jwu049/ubutun1404-baseimage 1.0 80e9c65ef26f 47 hours ago 228MB
ubutun1404-baseimage 1.0 80e9c65ef26f 47 hours ago 228MB
-------
docker push jwu049/ubutun1404-baseimage:1.0 //上传镜像
docker search centos //搜索镜像
docker pull centos //下载镜像
镜像地址修改
在国内从Docker Hub中拉取镜像,有时会超时,可以通过以下方法进行添加国内镜像:
- 每次下载镜像时加镜像地址(一次下载有效)
$ docker pull registry.docker-cn.com/library/ubuntu:16.04
- docker守护进程启动时加国内镜像地址(一次守护进程运行有效)
docker --registry-mirror=https://registry.docker-cn.com daemon
- 修改 /etc/docker/daemon.json 文件并添加上 registry-mirrors 键值(永久有效)
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
私有仓库
安装docker-registry
- 使用镜像方式安装
docker run -p 5000:5000 registry //直接使用容器模式下载registry
docker run -e SETTING_FLAVOR=s3 -e AWS_BUCKET=mybucket -e STORAGE_PATH=/registry -e AWS_KEY=myawskey -e AWS_SECRET=myawssecret
-e SEARCH_BACKEDN=sqlalchemy -p 5000:5000 registry //设置环境变量,即使用Amazon S3存储镜像
- centos使用rpm包方式
yum install docker-registry-y
service docker-registry start
service docker=registry status
netstat -ltnp | grep 5000
ps -ef | grep 31079
配置文件
默认情况下docker registry 使用config_sample.yml进行各项配置,配置文件使用yml格式,并提供各种不同的模板,官方提供的config_sample.yml链接地址如下:
config_sample.yml
文件里的示例模板解释如下:
- common: 公共基础配置,其他模板可引用
- local: 存储数据到本地文件系统
- s3: 存储数据到AWS s3
- ceph-s3: 通过Ceph 对象网关奖数据存储到Ceph集群
- dev:使用local模板的基本配置
- test:单元测试使用
- prod:生产环境配四款(基本和s3配置类似)
- gcs:存储数据到google的云存储
- swift:存储数据到Openstack Swift服务
- glance:存储数据到Openstack Glance服务,本地文件系统为后备
- glance-swift:存储数据到OpenStack Glance服务,Swift做后备
- elliptics:存储数据到Elliptics key/value存储
构建安全的私有仓库
Nginx的安装与配置
1) 配置配置文件registry.conf:
upstream docker-registry {
server docker.wujun.club:5000;
}
server {
listen 443;
server_name docker.wujun.club;
ssl on;
ssl_certificate /etc/ssl/certs/docker-registry.crt;
ssl_certificate_key /etc/ssl/private/docker-registry.key;
client_max_body_size 0; #disable any limits to avoid http 413 for large image upload;
chunked_transfer_encoding on;
location / {
auth_basic "Restricted";
auth_basic_user_file docker-registry.htpasswd;
include docker-registry.conf;
}
location /_ping {
auth_basic off;
include docker-registry.conf;
}
location /v1/_ping {
auth_basic off;
include docker-registry.conf;
}
location /v2/_ping {
auth_basic off;
include docker-registry.conf;
}
location /v2/_catalog {
auth_basic off;
include docker-registry.conf;
}
}
2) 创建ssl支持配置问件docker-registry.conf
proxy_pass http://docker-registry;
proxy_set_header Host $host; #required for docker client's sake
proxy_set_header X-Real-IP $remote_addr; # apss on real client's IP
proxy_set_header Authorization ""; #see https://gitbu.com/dotcloud/docker-registry/issue/170
proxy_read_timeout 900;
3)通过htpasswd创建认证的用户名和密码
htpasswd -bc docker-registry.htpasswd docker docker
SSL证书
1) 创建自签名证书CA
CA包含公钥和私钥,私钥用于给其他证书签名,公钥用别人验证证书的有效性
echo 01 > ca.srl
openssl genrsa -des3 -out ca-key.pem 2048
openssl req -new -x509 -days 365 -key ca-key.pem -out ca.pem
2)为Nginx server创建服务端证书
openssl genrsa -des3 -out server-key.pem 2048
openssl req -subj '/CN=Terry' -new -key server-key.pem -out server.csr
openssl x509 -req -days 365 -in server.csr -CA ca.pem -CAkey ca-key.pem -out server-cert.pem
openssl rsa -in server-key.pem -out server-key.pem //删除server key中的passphrase
cp server-cert.pem /etc/ssl/certs/docker-registry.crt
cp server-key.pem /etc/ssl/private/docker-registry.key
3) 客户端配置证书
centos:
update-ca-trust
cp ca.pem /etc/pki/ca-trust/source/anchor/ca.crt
mac:
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ca.crt
4) 上传镜像
docker tag {image_name} dev.registry.com/dbyin/http
docker push dev.registry.com/dbyin/http