搭建docker本地私有仓库
安装docker之后,可以通过官方提供的registry镜像来搭建一套本地私有仓库环境。
大致流程:仓库创建–>镜像重命名–>修改docker启动参数–>上传镜像–>拉去镜像
- 使用registry镜像创建私有仓库
[root@localhost home]# docker run -d -v /home/yufeng/:/var/lib/registry -p 5000:5000 --restart=always --name myregistry registry:latest
Unable to find image 'registry:latest' locally
latest: Pulling from library/registry
4064ffdc82fe: Pull complete
c12c92d1c5a2: Pull complete
4fbc9b6835cc: Pull complete
765973b0f65f: Pull complete
3968771a7c3a: Pull complete
Digest: sha256:20bbbc0f6384cf7dc6e292ccbe75935b73c92ec776543c970904bc60feceb129
Status: Downloaded newer image for registry:latest
bd415e329edf5b4db0f00a9c861b190ef6e51e6f824c524af34897d4efbc6e4e
这将自动下载并启动一个registry容器,创建本地的私有仓库服务
-v /home/hzq/registry:/var/lib/registry
认情况下,会将仓库存放于容器内的/var/lib/registry目录下,指定本地目录挂载到容器。-p 5000:5000
端口映射--restart=always
在容器退出时总是重启容器,主要应用在生产环境--name myregistry
指定容器的名称
查看所有的镜像,可以发现该镜像已存在本地中
[root@localhost tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry latest b2b03e9146e1 3 weeks ago 33.3MB
shijian_tomcat latest 9895c456b9a8 3 months ago 326MB
<none> <none> edcee99f73e4 3 months ago 326MB
tomcat 8.0-jre8-slim 85d9cb1b3165 3 months ago 235MB
mysql 5.5 f010dab7b20d 3 months ago 205MB
mysql latest 8d65ec712c69 3 months ago 445MB
tomcat 8.5-jre8-slim 39949b1a0ffc 3 months ago 230MB
wks_tomcat latest 91ed283d2632 9 months ago 414MB
wks_nginx latest 29cf4adb5a52 9 months ago 25.8MB
wks_mysql latest 85b9f76efd29 9 months ago 387MB
nginx alpine 962f3267cbde 9 months ago 15.5MB
tomcat 7.0.76-jre8 dff9c6981d23 16 months ago 367MB
mysql 5.7.16 d9124e6c552f 20 months ago 383MB
shijian_mysql latest d9124e6c552f 20 months ago 383MB
查看所有正在运行的容器,可以看到该容器已在运行中
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd415e329edf registry:latest "/entrypoint.sh /e..." 4 seconds ago Up 3 seconds 0.0.0.0:5000->5000/tcp myregistry
ba56e6cfac54 shijian_tomcat "catalina.sh run" 3 months ago Up 5 hours 0.0.0.0:8080->8080/tcp shijian_tomcat_1
f33687352242 shijian_mysql "docker-entrypoint..." 3 months ago Up 5 hours 0.0.0.0:3306->3306/tcp shijian_mysql_1
2.管理私有仓库
- 通过docker tag重命名镜像,使之与registry匹配
[root@localhost yufeng]# docker tag registry:latest 192.168.207.129:5000/registry
[root@localhost yufeng]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.207.129:5000/registry latest b2b03e9146e1 3 weeks ago 33.3MB
registry latest b2b03e9146e1 3 weeks ago 33.3MB
shijian_tomcat latest 9895c456b9a8 3 months ago 326MB
<none> <none> edcee99f73e4 3 months ago 326MB
tomcat 8.0-jre8-slim 85d9cb1b3165 3 months ago 235MB
mysql 5.5 f010dab7b20d 3 months ago 205MB
mysql latest 8d65ec712c69 3 months ago 445MB
tomcat 8.5-jre8-slim 39949b1a0ffc 3 months ago 230MB
wks_tomcat latest 91ed283d2632 9 months ago 414MB
wks_nginx latest 29cf4adb5a52 9 months ago 25.8MB
wks_mysql latest 85b9f76efd29 9 months ago 387MB
nginx alpine 962f3267cbde 9 months ago 15.5MB
tomcat 7.0.76-jre8 dff9c6981d23 16 months ago 367MB
mysql 5.7.16 d9124e6c552f 20 months ago 383MB
shijian_mysql latest d9124e6c552f 20 months ago 383MB
- 上传镜像
[root@localhost docker]# docker push 192.168.207.129:5000/registry
The push refers to a repository [192.168.207.129:5000/registry]
Get https://192.168.207.129:5000/v1/_ping: http: server gave HTTP response to HTTPS client
无法上传镜像是因为新碟Docker版本对安全性要求较高,会要求仓库支持SSL/TLS证书。修改Docker daemon的启动参数,表示信任这个私有仓库,不进行安全证书检查。在”/etc/docker/“目录下,创建”daemon.json“文件。
[root@localhost docker]# touch daemon.json
在文件中写入:
{
"insecure-registries": [
"192.168.207.129:5000"
]
}
//多个私服写法,逗号分隔即可
{
"insecure-registries": [
"192.168.207.129:5000",
"hub.docker.jiankunking.io:5000"
]
}
重启docker(通过 restart命令重启无效)
[root@localhost docker]# docker restart myregistry
上传镜像成功:
[root@localhost docker]# docker push 192.168.207.129:5000/registry
The push refers to a repository [192.168.207.129:5000/registry]
00b6cd9831d7: Pushed
5030e231e2c6: Pushed
7a2efe6c629c: Pushed
f824ed3a5fe3: Pushed
4da3a15c1916: Pushed
latest: digest: sha256:a0c421c0332197472fa5335c28bbe6c7b434770b1a695af3605029ae4db9340e size: 1364
查看仓库镜像:
[root@localhost yufeng]# curl http://192.168.207.129:5000/v2/_catalog
{"repositories":["registry","shijian_tomcat"]}
从私有仓库拉去镜像:
[root@localhost yufeng]# docker pull 192.168.207.129:5000/shijian_tomcat