10,docker基础之---Harbor+阿里云+本地镜像仓库

时间:2022-07-13 00:45:14

什么是镜像仓库?

存放着很多镜像的仓库。

为什么要使用镜像仓库?

例如我们之前使用的pull下载mysql镜像的时候下载的镜像就是存放在他人的镜像仓库之上,公司内部部署镜像仓库即可多台电脑从此库中拉取镜像进行使用。

镜像仓库的分类(大致分两类)

公共镜像仓库:【官方​​https://hub.docker.com​​】【非官方:基于其他组织开发的免费镜像供大家学习和使用】

私有镜像仓库: 公司自己搭建的,用于存放公司内部的镜像,自己管理,不提供给外部使用。

阿里云镜像仓库的搭建使用

阿里云镜像仓库申请地址:

https://cr.console.aliyun.com/cn-hangzhou/instance/repositories

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库


上述则是创建完成操作指南表示分几个步骤下面我对以下内容进行实战操作
[root@docker ~]# docker login --username=aliyun5965972577 registry.cn-hangzhou.aliyuncs.com
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 #显示登录成功
#先查看一下本地镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos redis b7aa4955980a 3 days ago 599MB
mycentos nginx 1cdfb823cd10 3 days ago 522MB
mycentos jdk 27177c9bb814 3 days ago 1.04GB
wordpress latest c3c92cc3dcb1 14 months ago 616MB
mysql 5.7 c20987f18b13 14 months ago 448MB
centos 7 eeb6ee3f44bd 17 months ago 204MB
[root@docker ~]#如我想把上述的mysql:5.7上传到阿里云镜像
[root@docker ~]# docker tag c20987f18b13 registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql:5.7
#再次进行查看倒数第二行已经上传成功
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos redis b7aa4955980a 3 days ago 599MB
mycentos nginx 1cdfb823cd10 3 days ago 522MB
mycentos jdk 27177c9bb814 3 days ago 1.04GB
wordpress latest c3c92cc3dcb1 14 months ago 616MB
mysql 5.7 c20987f18b13 14 months ago 448MB
registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql 5.7 c20987f18b13 14 months ago 448MB
centos 7 eeb6ee3f44bd 17 months ago 204MB
[root@docker ~]# docker push registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql:5.7
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql]
e889c671872c: Pushed
789f3aa31b3f: Pushed
35ba198e64f5: Pushed
9b64bb048d04: Pushed
aad27784b762: Pushed
0d17fee8db40: Pushed
d7a777f6c3a4: Pushed
a0c2a050fee2: Pushed
0798f2528e83: Pushed
fba7b131c5c3: Pushed
ad6b69b54919: Pushed
5.7: digest: sha256:398f124948bb3d5789c0ac7c004d02e6d9a3ae95aa9804d7a3b33a344ff3c9cd size: 2621
[root@docker ~]#

上传成功咱们进入到阿里云仓库去看一下:

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

下面新开一台虚拟机进行测试(宿主机为192.168.88新开一台为192.168.88.188)

#188ip先登录阿里云
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 17 months ago 204MB
[root@localhost ~]# docker login --username=aliyun5965972577 registry.cn-hangzhou.aliyuncs.com
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
[root@localhost ~]# docker pull registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql:5.7
5.7: Pulling from lyxceshi/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:398f124948bb3d5789c0ac7c004d02e6d9a3ae95aa9804d7a3b33a344ff3c9cd
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql:5.7
registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql:5.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql 5.7 c20987f18b13 14 months ago 448MB
centos 7 eeb6ee3f44bd 17 months ago 204MB
[root@localhost ~]#

修改名称:

[root@localhost ~]# docker tag  registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql:5.7 mysql:5.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 14 months ago 448MB
registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql 5.7 c20987f18b13 14 months ago 448MB
centos 7 eeb6ee3f44bd 17 months ago 204MB
[root@localhost ~]#

启动mysql容器:

[root@localhost ~]# docker run -itd mysql:5.7 /bin/bash
0cc36629e1bd8449fb9a202d25a4f65378062edb6a6c5b492a745334c7022cf5
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0cc36629e1bd mysql:5.7 "docker-entrypoint.s…" 19 seconds ago Up 18 seconds 3306/tcp, 33060/tcp mystifying_banach
[root@localhost ~]#

harbor镜像仓库搭建

简介:构建自己的镜像仓库

前提:安装之前确保前置条件满足,需要安装docker,docker-compose,openssl以及python2.7以上

harbor离线版安装下载地址:

https://github.com/goharbor/harbor/releases

安装依赖环境包:

[root@docker ~]# yum install openssl -y

上传harbor安装包到/usr/local

[root@docker ~]# cd /usr/local/
[root@docker local]# ll
总用量 620080
drwxr-xr-x. 2 root root 28 217 19:51 bin
drwxr-xr-x. 2 root root 6 411 2018 etc
drwxr-xr-x. 2 root root 6 411 2018 games
-rw-r--r-- 1 root root 634959522 219 16:10 harbor-offline-installer-v1.9.3.tgz
drwxr-xr-x. 2 root root 6 411 2018 include
drwxr-xr-x. 2 root root 6 411 2018 lib
drwxr-xr-x. 2 root root 6 411 2018 lib64
drwxr-xr-x. 2 root root 6 411 2018 libexec
drwxr-xr-x. 2 root root 6 411 2018 sbin
drwxr-xr-x. 5 root root 49 1121 17:57 share
drwxr-xr-x. 2 root root 6 411 2018 src
[root@docker local]#

进行解压:

[root@docker local]# tar -xf harbor-offline-installer-v1.9.3.tgz

进入解压完成的harbor目录修改配置文件harbor.yml:

[root@docker local]# cd harbor
[root@docker harbor]# ll
总用量 623364
-rw-r--r-- 1 root root 638288840 1114 2019 harbor.v1.9.3.tar.gz
-rw-r--r-- 1 root root 5805 1114 2019 harbor.yml
-rwxr-xr-x 1 root root 5088 1114 2019 install.sh
-rw-r--r-- 1 root root 11347 1114 2019 LICENSE
-rwxr-xr-x 1 root root 1748 1114 2019 prepare
[root@docker harbor]#

修改harbor.yml文件内的主机名和密码:

[root@docker harbor]# vim harbor.yml
#修改第四行为IP
原内容:
hostname: reg.mydomain.com
修改后:
hostname: r192.168.88.88

#修改第27行密码
原内容密码为默认arbor12345可以自行进行修改
harbor_admin_password: Harbor12345

执行prepare脚本:

[root@docker harbor]# sh prepare

ll查看一下是否生成一个docker-compose.yml文件

[root@docker harbor]# ll
总用量 623372
drwxr-xr-x 3 root root 20 219 16:22 common
-rw-r--r-- 1 root root 5290 219 16:22 docker-compose.yml
-rw-r--r-- 1 root root 638288840 1114 2019 harbor.v1.9.3.tar.gz
-rw-r--r-- 1 root root 5802 219 16:18 harbor.yml
-rwxr-xr-x 1 root root 5088 1114 2019 install.sh
-rw-r--r-- 1 root root 11347 1114 2019 LICENSE
-rwxr-xr-x 1 root root 1748 1114 2019 prepare
[root@docker harbor]#

安装install.sh:

[root@docker harbor]# sh install.sh

查看:

[root@docker harbor]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------------
harbor-core /harbor/harbor_core Up (healthy)
harbor-db /docker-entrypoint.sh Up (healthy) 5432/tcp
harbor-jobservice /harbor/harbor_jobservice ... Up (healthy)
harbor-log /bin/sh -c /usr/local/bin/ ... Up (healthy) 127.0.0.1:1514->10514/tcp
harbor-portal nginx -g daemon off; Up (healthy) 8080/tcp
nginx nginx -g daemon off; Up (healthy) 0.0.0.0:80->8080/tcp,:::80->8080/tcp
redis redis-server /etc/redis.conf Up (healthy) 6379/tcp
registry /entrypoint.sh /etc/regist ... Up (healthy) 5000/tcp
registryctl /harbor/start.sh Up (healthy)
[root@docker harbor]#

输入本机地址进入浏览器访问验证:

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

harbor镜像仓库使用

使用另一台centos当作客户端进行访问:

[root@localhost ~]# docker login --username=admin 192.168.88.88
Password:
Error response from daemon: Get "https://192.168.88.88/v2/": dial tcp 192.168.88.88:443: connect: no route to host
[root@localhost ~]#发现登录失败这是因为harbor是https协议需要进行修改客户端

修改配置文件添加私有仓库地址:

[root@localhost ~]# vi /etc/docker/daemon.json
追加,"insecure-registries":["http://192.168.88.88"]
#原内容
[root@localhost ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://jc5mcdvz.mirror.aliyuncs.com"]
}
#修改后内容
[root@localhost ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://jc5mcdvz.mirror.aliyuncs.com"],"insecure-registries":["http://192.168.88.88"]
}
[root@localhost ~]# "insecure-registries":["http://192.168.0.151"]

进行重启docker服务:

[root@localhost ~]# systemctl restart docker

再次进行登录:

[root@localhost ~]# docker login --username=admin 192.168.88.88
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
[root@localhost ~]#

在网页新创建一下项目:

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

进行推送:(客户端测试上传mysql)

[root@localhost ~]# docker tag mysql:5.7 192.168.88.88/lyxceshi/mysql:5.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.88.88/lyxceshi/mysql 5.7 c20987f18b13 14 months ago 448MB
mysql 5.7 c20987f18b13 14 months ago 448MB
registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql 5.7 c20987f18b13 14 months ago 448MB
centos 7 eeb6ee3f44bd 17 months ago 204MB
[root@localhost ~]#

进行推送:

[root@localhost ~]# docker push 192.168.88.88/lyxceshi/mysql:5.7

10,docker基础之---Harbor+阿里云+本地镜像仓库

10,docker基础之---Harbor+阿里云+本地镜像仓库

服务器进行下载测试:

[root@docker harbor]# vi /etc/docker/daemon.json
[root@docker harbor]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://jc5mcdvz.mirror.aliyuncs.com"],"insecure-registries":["http://192.168.88.88"]
}
[root@docker harbor]#

进行重启docker服务和harbor服务:

[root@docker harbor]# systemctl restart docker
[root@docker harbor]# docker-compose up -d

进入容器:

[root@docker harbor]# docker login --username=admin 192.168.88.88
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
[root@docker harbor]#

进行下载:

[root@docker harbor]# docker pull 192.168.88.88/lyxceshi/mysql:5.7


10,docker基础之---Harbor+阿里云+本地镜像仓库

还可以设置公开如下:

10,docker基础之---Harbor+阿里云+本地镜像仓库

刚才咱们已经登录进入镜像该如何退出呢如下:

[root@docker harbor]# docker logout
Removing login credentials for https://index.docker.io/v1/
[root@docker harbor]#

再次进入登录:

[root@docker harbor]# docker login --username=admin 192.168.88.88
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
#发现会把密码加密放进/root/.docker/config.json.我们可以查看一下也可以给这个文件删除
[root@docker harbor]# cat /root/.docker/config.json
{
"auths": {
"192.168.88.88": {
"auth": "YWRtaW46SGFyYm9yMTIzNDU="
}
}
}[root@docker harbor]#进行删除1
[root@docker harbor]# rm -rf /root/.docker/config.json

本地镜像仓库创建使用

#查看镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos redis b7aa4955980a 3 days ago 599MB
mycentos nginx 1cdfb823cd10 3 days ago 522MB
mycentos jdk 27177c9bb814 4 days ago 1.04GB
mysql 5.7 c20987f18b13 14 months ago 448MB
centos 7 eeb6ee3f44bd 17 months ago 204MB
#进行保存
[root@docker ~]# docker save c20987f18b13 -o /home/mysql.tar

#详解
docker save 进行保存
c20987f18b13 mysql的ID也可以直接输入mysql:5.7
-o 表示输出
/home/mysql.tar 表示输出的目录

总体也可以上述替换成
[root@docker ~]# docker save mysql:5.7 > /home/mysql.tar

进行查看:

[root@docker ~]# cd /home/
[root@docker home]# ll
总用量 444804
-rw-r--r-- 1 root root 179 216 09:28 dockerfile
-rw------- 1 root root 453726720 219 17:30 mysql.tar
drwxrwxr-x 6 root root 309 327 2018 redis-4.0.9
-rw-r--r-- 1 root root 1737022 216 09:27 redis-4.0.9.tar.gz
-rw-r--r-- 1 root root 219 216 09:28 redis_install.sh
[root@docker home]#

载入镜像:

#本地传输到宿主机
[root@docker home]# sz mysql.tar

下载好之后打开另一台虚拟机进行载入镜像:

[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 14 months ago 448MB
registry.cn-hangzhou.aliyuncs.com/lyxceshi/mysql 5.7 c20987f18b13 14 months ago 448MB
centos 7 eeb6ee3f44bd 17 months ago 204MB
[root@localhost ~]#把上述的mysql镜像先进行删除处理
[root@localhost ~]# docker rmi -f c20987f18b13
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 17 months ago 204MB

进行载入:

[root@localhost ~]# ll   上传成功
total 431368
-rw-------. 1 root root 1260 Nov 21 05:08 anaconda-ks.cfg
-rw-r--r-- 1 root root 441714176 Feb 19 04:41 mysql.tar
[root@localhost ~]#

对mysql.tar进行载入:

[root@localhost ~]# docker load -i mysql.tar
#其中的-i也可以替换成小于号<
#进行查看
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 17 months ago 204MB
<none> <none> cd3ed0dfff7e 3 years ago 437MB
[root@localhost ~]#发现载入成功但是没有名字我们来对他进行修改
[root@localhost ~]# docker tag cd3ed0dfff7e mysql:5.7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 17 months ago 204MB
mysql 5.7 cd3ed0dfff7e 3 years ago 437MB
[root@localhost ~]#发现已经ok了(ID也是一样的原封不动复制过来)

保存容器:

#启动一个mysql容器
[root@docker home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos redis b7aa4955980a 3 days ago 599MB
mycentos nginx 1cdfb823cd10 3 days ago 522MB
mycentos jdk 27177c9bb814 4 days ago 1.04GB
mysql 5.7 c20987f18b13 14 months ago 448MB
centos 7 eeb6ee3f44bd 17 months ago 204MB
[root@docker home]# docker run -itd c20987f18b13 /bin/bash
111400433a3b707eaca9fc7c27b3d16219566d710224db1b2dbc7762497e1e24
[root@docker home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
111400433a3b c20987f18b13 "docker-entrypoint.s…" 15 seconds ago Up 14 seconds 3306/tcp, 33060/tcp cool_tharp
[root@docker home]#

进行载入:

[root@docker home]# docker export 111400433a3b -o /home/mysql-export.tar
[root@docker home]# ll
总用量 881604
-rw-r--r-- 1 root root 179 216 09:28 dockerfile
-rw------- 1 root root 447279616 219 17:53 mysql-export.tar
-rw------- 1 root root 453726720 219 17:30 mysql.tar
drwxrwxr-x 6 root root 309 327 2018 redis-4.0.9
-rw-r--r-- 1 root root 1737022 216 09:27 redis-4.0.9.tar.gz
-rw-r--r-- 1 root root 219 216 09:28 redis_install.sh
[root@docker home]#进行下载到宿主机
[root@docker home]# sz mysql-export.tar

另一台服务器进行载入此容器:

#进行上传:
[root@localhost ~]# ll
total 481596
-rw-------. 1 root root 1260 Nov 21 05:08 anaconda-ks.cfg
-rw-r--r-- 1 root root 51433090 Feb 19 04:56 mysql-export.tar
-rw-r--r-- 1 root root 441714176 Feb 19 04:41 mysql.tar
[root@localhost ~]#进行载入
[root@localhost ~]# docker import mysql-export.tar
#ok之后查看镜像发现ID与之前ID不一样也就是说这样的方法则不是完完全全复制而是可以自动生成