什么是docker仓库 ?
Docker 仓库是用来包含镜像的位置,Docker提供一个注册服
务器(Register)来保存多个仓库,每个仓库又可以包含多个
具备不同tag的镜像。
Docker运行中使用的默认仓库是 Docker Hub 公共仓库。
Docker Hub
docker hub是docker公司维护的公共仓库,用户可以免费使用,也可以
购买私有仓库。
1.docker 搭建私有仓库registry:
236 docker
237 docker info
238 docker ps
239 docker search registry
240 docker pull registry
241 docker images
242 docker history registry 查看registry镜像具体的信息(做的事情,默认开启的端口)
243 docker run -d --name registry -p 5000:5000 -v /opt/registry:/var/lib/registry registry 冒号前为宿主机地址卷位置(不存在会创建),冒号后为容器中的地址卷
244 docker ps 查看正在运行的容器registry;
245 netstat -antlupe | grep 5000 查看端口映射是否开启;
246 docker tag busybox:latest localhost:5000/busybox:latest
247 ll /opt/registry
248 ls -l /opt/registry
249 docker images
250 docker push localhost:5000/busybox
251 ls -l /opt/registry
252 cd /opt/registry
253 tree .
254 yum install tree
255 tree .
256 du -sh *
可以使用 curl localhost:5000/v2/_catalog ;查看搭建的本地仓库的内容(会显示镜像名称)
其他主机使用registry推送镜像以及拉取镜像:
需要在server2主机上的/etc/docker目录下创建一个daemon.json文件
{
“insecure-registries” : [“172.25.3、254.111:5000”]
}
重启服务,用docke info 查看
2.对registry私有仓库进行加密认证:
自签名认证证书认证:
mkdir -p certs 创建证书目录
openssl req -newkey rsa:4096 -sha256 -keyout certs/reg.westos.org.key -x509 -days 365 -out certs/reg.westos.org.crt # 建立认证
认证信息依次填入:cn->shaanxi->xi'an->westos->linux->reg.westos.org->root@westos.org
ls 可以看见 westos.org.crt westos.org.key ;
将westos.org.crt文件复制到/etc/docker/certs.d/reg.westos.org/ca.crt, 每个Docker主机上。无需重启Docker
docker rm registry
docker run -d --name registry -p 443:443 -v /opt/registry:/var/lib/registry -v "$(pwd)"/certs:/certs -e REGISTRY_HTTP_ADDR=0.0.0.0:443 -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/reg.westos.org.crt -e REGISTRY_HTTP_TLS_KEY=/certs/reg.westos.org.key registry
curl -k https://172.25.254.111/v2/_catalog 可以看见仓库信息;
docker tag demo:v1 reg.westos.org/demo:v1
vim /etc/hosts 作server1 reg.westos.org 解析
mkdir -p /etc/docker/certs.d/reg.westos.org/
cp certs/reg.westos.org.crt /etc/docker/certs.d/reg.westos.org/ca.crt
ll /etc/docker/certs.d/reg.westos.org/ca.crt
docker push reg.westos.org/demo:v1 成功显示:
docker push reg.westos.org/demo:v1
The push refers to repository [reg.westos.org/demo]
abf1aec6a57e: Pushed
在server2中将mkdir -p /etc/docker/certs.d/reg.westos.org/
cp server1中的ca.crt 到server2的reg.westos.org/下
vim hosts
server1 中 : curl -k https://172.25.254.111/v2_catatlog;
添加用户认证:
server1:
cd
mkdir auth
yum install httpd-tools
htpasswd -B -c auth/htpasswd lb
New password:
Re-type new password:
Adding password for user lb
[root@server1 ~]# htpasswd -B auth/htpasswd zy
New password:
Re-type new password:
Adding password for user zy
[root@server1 ~]# cat auth/htpasswd
lb:$2y$05$YKI.bGNHCZY2yYfo4RgFj.uZPZE5c7rlnHyQAGkkC23spH7gimZBK
zy:$2y$05$XliqNmOzjUpu55dbSCWBX.GzMDlHKWske6T9JKIjKjvXYYfUWyfj6
[root@server1 ~]# docker rm -f registry
registry
[root@server1 ~]# docker run -d --name registry -p 443:443 -v /opt/registry:/var/lib/registry -v "$(pwd)"/certs:
/certs -e REGISTRY_HTTP_ADDR=0.0.0.0:443 -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/reg.westos.org.crt -e
REGISTRY_HTTP_TLS_KEY=/certs/reg.westos.org.key -v "$(pwd)"/auth:/auth -e "REGISTRY_AUTH=htpasswd" -e
"REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry
1029c7e951fa3589c4acc5ca82910789549274fdec5ac442cf6a46c7a0fbca5d
[root@server1 ~]# docker tag busybox:latest reg.westos.org/busybox:latest
[root@server1 ~]# docker push reg.westos.org/busybox:latest
The push refers to repository [reg.westos.org/busybox]
8a788232037e: Preparing
no basic auth credentials
[root@server1 ~]# docker login reg.westos.org
Username: lb
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
在server2中 docker login reg.westos.org
docker pull reg.westos.org/demo:v1
3.Docker Harbor 的部署和搭建:
我们在日常Docker容器使用和管理过程中,渐渐发现部署企业私有仓库往往是很有必要的, 它可以帮助你管理企业的一些敏感镜像, 同时由于Docker Hub的下载速度和GFW的原因, 往往需要将一些无法直接下载的镜像导入本地私有仓库. 而Harbor就是部署企业私有仓库的一个不二之选。
Harbor的每个组件都是以Docker容器的形式构建的,可以使用Docker Compose来进行部署。如果环境中使用了kubernetes,Harbor也提供了kubernetes的配置文件。
3-1:常见的Harbor命令:
cd harbor/#执行必须切到目录下
docker-compose start#开启
docker-compose stop#关闭
docker-compose down#关闭并删除
./prepare#清理缓存
./install.sh#安装
安装参数
--with-notary#内容信任
--with-clair#漏洞扫描
--with-trivy#
--with-chartmuseum#Helm Charts
3-2:企业级Harbor部署:
tar zxf harbor-offline-installer-v1.10.1.tgz
mv docker-compose-Linux-x86_64-1.27.0 /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
vim /etc/hosts
172.25.254.54 lbin reg.westos.org
228 mkdir -p certs
229 openssl req -newkey rsa:4096 -sha256 -keyout certs/reg.westos.org.key -x509 -days 365 -out certs/reg.westos.org.crt
230 ls
231 cd certs/
232 ls
233 mkdir /etc/docker/certs.d/reg.westos.org -p
234 ls
235 cp reg.westos.org.crt /etc/docker/certs.d/reg.westos.org/ca.crt
cd harbor
vim harbor.yml
hostname: reg.westos.org #和地址解析名一样
# The path of cert and key files for nginx
certificate: /root/certs/reg.westos.org #存放路径
private_key: /root/certs/reg.westos.org
harbor_admin_password: westos
./install.sh
docker-compose ps
访问网页:172.25.254.111
用户:admin
秘密:westos
4.docker 网络:
docker 的网络 :
server2中:
docker network ls ;
ip addr show
yum install bridge-utils.x86_64 ;
docker load -i bustbox.tar ;
docker run -it --name yuyu busybox ;
docker run -it --name yuyu2 busybox ; (虚拟ip,想要访问,需要-p 80:80 做端口映射)
docker inspect yuyu ;
ip addr show ;
[root@server2 ~]# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.0242fdaaec72 no veth7ea9028
vethcf48acc
brctl show ;
docker run -it --name yuyu3 --network=host busybox ; 让容器进程和宿主机共用ip(可以直接访问到);
docker run -it --name demo --network=none busybox ; 禁用容器的网络;
docker network create mynet1 ; 创建一个新的网段;可以在创建网络的时候指定;