Docker Hub作为Docker默认官方公共镜像,如果想要自己搭建私有镜像残酷,官方也提供Registry镜像,使得我们搭建私有仓库变得非常简单。
所谓私有仓库,也就是在本地(局域网)搭建的一个类似公共仓库的东西,搭建好之后,我们可以将镜像提交到私有仓库中。这样我们既能使用 Docker 来运行我们的项目镜像,也避免了商业项目暴露出去的风险。
载入镜像
docker load -i registry2.tar
查看镜像历史,找到挂载目录,可以在创建容器时,指定新的存储路径
指定存储路径
-v /opt/registry:/var/lib/registry
docker run -d --name resistry -v /opt/registry:/var/lib/registry -p 5000:5000 registry
指定端口映射
-p 5000:5000
查看端口
更改镜像格式为私有仓库上传格式,以供私有仓库识别
上传至私有仓库
docker push localhost:5000/busybox
真实路径产看存储,如果有证明已经上传至私有仓库
也可通过本地访问查看
curl localhost:5000/v2/_catalog
docker pull localhost:5000/busybox
docker images
当删除同id不同名的两个镜像时,无法通过id删除,只能通过命名删除
创建目录
mkdir certs
cd certs/
生成非对称加密密钥
openssl req -newkey rsa:4096 -nodes -sha256 -keyout certs/westos.org.key -x509 -days 365 -out certs/westos.org.crt
[root@server1 ~]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout certs/westos.org.key -x509 -days 365 -out certs/westos.org.crt
Generating a 4096 bit RSA private key
........++
......................................................++
writing new private key to 'certs/westos.org.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:shanxi
Locality Name (eg, city) [Default City]:xian
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:reg.westos.org
Email Address []:[email protected]
reg.westos.org为仓库命名格式
Common Name (eg, your name or your server's hostname) []:reg.westos.org
说明:两台主机建立密钥认证,需要两台主机各持一份,一份用于server端仓库容器建立时指定密钥认证文件,一份位于client端/etc/docker/certs.d,用于访问时与server端进行验证。同理,当一台主机进行自访问时也需要在/etc/docker/certs.d目录下放入密钥认证文件。
拷贝证书到docker主机/etc/docker/certs.d目录
cd /etc/docker/
mkdir certs.d
cd certs.d/
mkdir reg.westos.org
cd reg.westos.org/
cp ~/certs/certs.d/westos.org.crt ca.crt
创建容器,指定密钥文件,一定要在家目录下执行
docker run -d --name registry -v /opt/registry:/var/lib/registry -p 443:443 -v "$(pwd)"/certs:/certs -e REGISTRY_HTTP_ADDR=0.0.0.0:443 -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/westos.org.crt -e REGISTRY_HTTP_TLS_KEY=/certs/westos.org.key registry
修改镜像名为上传格式
docker tag nginx:latest reg.westos.org/nginx:latest
上传镜像
docker ps
docker push reg.westos.org/nginx:latest
server3使用私有仓库
将server1内证书传递至server3/etc/docker/certs.d目录下
使用仓库
安装支持软件
yum install -y httpd-tools
生成用户认证文件,该目录下内容再容器建立后依然可继续添加用户
cd ~
mkdir auth
htpasswd -Bc auth/htpasswd admin #第一次添加用Bc
cat auth/htpasswd
htpasswd -B auth/htpasswd westos #追加添加用B
cat auth/htpasswd
创建容器,添加用户认证及密钥模块
docker run -d -p 443:443 \
--restart=always --name registry \
-v "$(pwd)"/auth:/auth \
-e "REGISTRY_AUTH=htpasswd" \
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
-v "$(pwd)"/certs:/certs \
-e REGISTRY_HTTP_ADDR=0.0.0.0:443 \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/westos.org.crt \
-e REGISTRY_HTTP_TLS_KEY=/certs/westos.org.key \
registry