docker HUB提供了registry的image, 可以非常方便的搭建私有regisrty.
当然你也可以选择从docker-registry的源码来搭建. 见 :
https://github.com/docker/docker-registry
本文主要说一下使用docker registry image搭建私有registry.
1. 首先要部署docker
参考 :
2. 启动docker 服务端, 最好使用-g 设置好root路径.
参考 :
3. 然后从docker hub 下载registry镜像到本地.
4. 使用 registry image 启动container. 启动时最好指定卷(用于存储镜像), 端口映射.
[root@db-172-16-3-221 ~]# docker pull registry
[root@db-172-16-3-221 ~]# docker images registry
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry 0.8.1 3e7767ddd728 9 days ago 427.9 MB
registry 0.7.3 c723c9b95ac0 9 days ago 424.1 MB
registry latest 8e9a29f977a7 9 days ago 427.9 MB
registry 0.6.9 bee70978874c 9 days ago 461.9 MB
registry 0.8.0 cd3581c06bdc 11 weeks ago 442.5 MB
registry 0.6.8 346f0c4d40c3 4 months ago 447.8 MB
registry 0.7.2 8b65881eed3d 4 months ago 516 MB
registry 0.7.0 a6a394071526 4 months ago 516 MB
registry 0.7.1 cc32522f16d6 4 months ago 516 MB
registry 0.6.7 cd8d7a6d86d1 5 months ago 452.5 MB
registry 0.6.6 e260f5a77e52 6 months ago 443 MB
registry 0.6.5 e7bac0a3804b 7 months ago 433 MB
registry 0.6.3 1f7bbd131cd8 9 months ago 490 MB
registry 0.6.2 0b520d776e7d 9 months ago 466.6 MB
registry 0.6.1 9f98cb899f46 9 months ago 460.1 MB
registry 0.6.0 873f518b98ef 9 months ago 466.5 MB
registry 0.5.9 e8e5377f8307 9 months ago 466.3 MB
registry 0.6.4 b04ace768d59 9 months ago 490 MB
4. 使用 registry image 启动container. 启动时最好指定卷(用于存储镜像), 端口映射.
创建目录, 用于存储镜像
创建配置文件, 从 https://github.com/docker/docker-registry/blob/master/config/config_sample.yml 下载
5. 启动container, 指定网络, 指定端口映射, 指定卷, 环境变量(配置文件,存储路径),
首先要打上tag
或存放到用户下(方便辨识是谁上传的, 或者归类) :
6.2 测试从私有registry pull image到本地
6.3 使用私有registry下载的image run container :
6.4 search , 使用方法如下 :
[参考]
[root@db-172-16-3-221 ~]# mkdir /data01/registry_sto
[root@db-172-16-3-221 ~]# mkdir /data01/registry_conf
创建配置文件, 从 https://github.com/docker/docker-registry/blob/master/config/config_sample.yml 下载
[root@db-172-16-3-221 ~]# cd /data01/registry_conf
[root@db-172-16-3-221 registry_conf]# vi config_sample.yml
# 须修改几处, 本文使用本地存储, local配置. (为了使search可用, 必须设置search backend)
issue: '"Digoal`s docker-registry server"'
local: &local
<<: *common
storage: local
storage_path: _env:STORAGE_PATH:/registry_sto
search_backend: _env:SEARCH_BACKEND:sqlalchemy
sqlalchemy_index_database: _env:SQLALCHEMY_INDEX_DATABASE:sqlite:////tmp/docker-registry.db
5. 启动container, 指定网络, 指定端口映射, 指定卷, 环境变量(配置文件,存储路径),
先使用前台启动, 调试正常后, 加上-d 后台启动.
docker run -d --net="host" -p 5000:5000 -v /data01/registry_conf:/registory_conf -v /data01/registry_sto:/registry_sto -e DOCKER_REGISTRY_CONFIG=/registory_conf/config_sample.yml -e STORAGE_PATH=/registry_sto -e SETTINGS_FLAVOR=local --name=registry registry:0.8.1
[root@db-172-16-3-221 registry_conf]# docker run --net="host" -p 5000:5000 -v /data01/registry_conf:/registory_conf -v /data01/registry_sto:/registry_sto -e DOCKER_REGISTRY_CONFIG=/registory_conf/config_sample.yml -e STORAGE_PATH=/registry_sto -e SETTINGS_FLAVOR=local --name=registry registry:0.8.1
2014-11-03 02:26:50,648 WARNING: Cache storage disabled!
2014-11-03 02:26:50,648 WARNING: LRU cache disabled!
2014-11-03 02:26:50 [1] [INFO] Starting gunicorn 18.0
2014-11-03 02:26:50 [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
2014-11-03 02:26:50 [1] [INFO] Using worker: gevent
2014-11-03 02:26:50 [15] [INFO] Booting worker with pid: 15
2014-11-03 02:26:50 [16] [INFO] Booting worker with pid: 16
2014-11-03 02:26:50 [17] [INFO] Booting worker with pid: 17
2014-11-03 02:26:51 [22] [INFO] Booting worker with pid: 22
2014-11-03 02:26:51,022 WARNING: Cache storage disabled!
2014-11-03 02:26:51,023 WARNING: LRU cache disabled!
2014-11-03 02:26:51,030 WARNING: Cache storage disabled!
2014-11-03 02:26:51,031 WARNING: LRU cache disabled!
2014-11-03 02:26:51,105 WARNING: Cache storage disabled!
2014-11-03 02:26:51,106 WARNING: LRU cache disabled!
2014-11-03 02:26:51,193 WARNING: Cache storage disabled!
2014-11-03 02:26:51,193 WARNING: LRU cache disabled!
[root@db-172-16-3-221 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c64e3cb2fd8 registry:0.8.1 /bin/sh -c 'exec doc 22 seconds ago Up 21 seconds registry
[root@db-172-16-3-221 ~]# docker inspect registry
[{
"Args": [
"-c",
"exec docker-registry"
],
"Config": {
"AttachStderr": true,
"AttachStdin": false,
"AttachStdout": true,
"Cmd": [
"/bin/sh",
"-c",
"exec docker-registry"
],
"CpuShares": 0,
"Cpuset": "",
"Domainname": "",
"Entrypoint": null,
"Env": [
"DOCKER_REGISTRY_CONFIG=/registory_conf/config_sample.yml",
"STORAGE_PATH=/registry_sto",
"SETTINGS_FLAVOR=local",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"ExposedPorts": {
"5000/tcp": {}
},
"Hostname": "db-172-16-3-221",
"Image": "registry:0.8.1",
"Memory": 0,
"MemorySwap": 0,
"NetworkDisabled": false,
"OnBuild": null,
"OpenStdin": false,
"PortSpecs": null,
"StdinOnce": false,
"Tty": false,
"User": "",
"Volumes": null,
"WorkingDir": ""
},
"Created": "2014-11-03T02:26:49.936106407Z",
"Driver": "devicemapper",
"ExecDriver": "native-0.2",
"HostConfig": {
"Binds": [
"/data01/registry_conf:/registory_conf",
"/data01/registry_sto:/registry_sto"
],
"ContainerIDFile": "",
"Dns": null,
"DnsSearch": null,
"Links": null,
"LxcConf": [],
"NetworkMode": "host",
"PortBindings": {
"5000/tcp": [
{
"HostIp": "",
"HostPort": "5000"
}
]
},
"Privileged": false,
"PublishAllPorts": false,
"VolumesFrom": null
},
"HostnamePath": "/data01/docker/containers/5c64e3cb2fd8c3160c953a4708b31aeb9900eefc40787e3e565c099e977f1e1f/hostname",
"HostsPath": "/data01/docker/containers/5c64e3cb2fd8c3160c953a4708b31aeb9900eefc40787e3e565c099e977f1e1f/hosts",
"Id": "5c64e3cb2fd8c3160c953a4708b31aeb9900eefc40787e3e565c099e977f1e1f",
"Image": "3e7767ddd728c1480e9bcfd5fdbe453b57f948deca3f55c9ef0459f9d24720a1",
"MountLabel": "",
"Name": "/registry",
"NetworkSettings": {
"Bridge": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"PortMapping": null,
"Ports": null
},
"Path": "/bin/sh",
"ProcessLabel": "",
"ResolvConfPath": "/etc/resolv.conf",
"State": {
"ExitCode": 0,
"FinishedAt": "0001-01-01T00:00:00Z",
"Paused": false,
"Pid": 29578,
"Running": true,
"StartedAt": "2014-11-03T02:26:50.167293486Z"
},
"Volumes": {
"/registory_conf": "/data01/registry_conf",
"/registry_sto": "/data01/registry_sto"
},
"VolumesRW": {
"/registory_conf": true,
"/registry_sto": true
}
}
]
好了, 如果防火墙允许的话, 可以连接到这个私有的HUB了.
IE
6. 测试 :
6.1 首先测试push image到这个registry.
当然要先制作image, 制作image可以通过现有的image或者通过dockerfile来制作, 略.
假设当前已有的image如下, 我想把centos:centos6 push到刚刚搭建的私有docker registry.
[root@db-172-16-3-221 ~]# docker images centos
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos centos5 504a65221a38 4 weeks ago 467.1 MB
centos centos6 68edf809afe7 4 weeks ago 212.7 MB
centos centos7 87e5b6b3ccc1 4 weeks ago 224 MB
centos latest 87e5b6b3ccc1 4 weeks ago 224 MB
首先要打上tag
docker tag [-f|--force[=false]] IMAGE [REGISTRYHOST/][USERNAME/]NAME[:TAG]
如下,
[root@db-172-16-3-221 ~]# docker tag 68edf809afe7 172.16.3.221:5000/centos:centos6
[root@db-172-16-3-221 ~]# docker push 172.16.3.221:5000/centos:centos6
The push refers to a repository [172.16.3.221:5000/centos] (len: 1)
Sending image list
Pushing repository 172.16.3.221:5000/centos (1 tags)
511136ea3c5a: Image successfully pushed
5b12ef8fd570: Image successfully pushed
68edf809afe7: Image successfully pushed
Pushing tag for rev [68edf809afe7] on {http://172.16.3.221:5000/v1/repositories/centos/tags/centos6}
[root@db-172-16-3-221 data01]# cd registry_sto/
[root@db-172-16-3-221 registry_sto]# ll
total 4
drwxr-xr-x 5 root root 4096 Nov 3 10:41 images
drwxr-xr-x 3 root root 20 Nov 3 10:41 repositories
[root@db-172-16-3-221 registry_sto]# du -sh *
73M images
12K repositories
或存放到用户下(方便辨识是谁上传的, 或者归类) :
[root@150 ~]# docker tag 504a65221a38 172.16.3.221:5000/digoal/centos:centos5
[root@150 ~]# docker push 172.16.3.221:5000/digoal/centos:centos5
The push refers to a repository [172.16.3.221:5000/digoal/centos] (len: 1)
Sending image list
Pushing repository 172.16.3.221:5000/digoal/centos (1 tags)
Image 511136ea3c5a already pushed, skipping
Image 5b12ef8fd570 already pushed, skipping
504a65221a38: Image successfully pushed
Pushing tag for rev [504a65221a38] on {http://172.16.3.221:5000/v1/repositories/digoal/centos/tags/centos5}
6.2 测试从私有registry pull image到本地
[root@150 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
digoal/pg 9.3.5 b8a933b7f4d7 10 days ago 646.5 MB
centos centos5 504a65221a38 4 weeks ago 467.1 MB
centos centos7 87e5b6b3ccc1 4 weeks ago 224 MB
centos latest 87e5b6b3ccc1 4 weeks ago 224 MB
[root@150 ~]# docker pull 172.16.3.221:5000/centos:centos6
Pulling repository 172.16.3.221:5000/centos
68edf809afe7: Download complete
511136ea3c5a: Download complete
5b12ef8fd570: Download complete
注意repository列, 从私有registry下载的image很容易辨识.
[root@150 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
digoal/pg 9.3.5 b8a933b7f4d7 10 days ago 646.5 MB
centos centos5 504a65221a38 4 weeks ago 467.1 MB
172.16.3.221:5000/centos centos6 68edf809afe7 4 weeks ago 212.7 MB
centos centos7 87e5b6b3ccc1 4 weeks ago 224 MB
centos latest 87e5b6b3ccc1 4 weeks ago 224 MB
6.3 使用私有registry下载的image run container :
[root@150 ~]# docker run -t -i --rm 172.16.3.221:5000/centos:centos6 /bin/bash
bash-4.1# w
02:47:41 up 25 days, 22:48, 0 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
bash-4.1# uname -r
2.6.32-431.23.3.el6.x86_64
bash-4.1# cat /etc/redhat-release
CentOS release 6.5 (Final)
6.4 search , 使用方法如下 :
[root@150 ~]# curl -X GET http://172.16.3.221:5000/v1/search?q=centos
{"num_results": 2, "query": "centos", "results": [{"description": null, "name": "library/centos"}, {"description": null, "name": "digoal/centos"}]}
[root@150 ~]# curl -X GET http://172.16.3.221:5000/v1/search?q=digoal
{"num_results": 1, "query": "digoal", "results": [{"description": null, "name": "digoal/centos"}]}
结合openstack等云平台使用, 将镜像存储到云平台镜像管理存储, 可参考 :
6. man docker-tag