1、制作Docker镜像
1.1、首先编辑dockerfile文件
注意:制作镜像过程最好新建一个单独的空目录,然后进入这个空目录完成镜像制作过程,这样可以防止其他不必要的文件被打包进镜像里面。
我这里新建了一个data目录
命令:mkdir /data
进入该目录:cd /data
编辑dockerfile文件,这里我去名为centos.dockerfile。
命令:vim centos.dockerfile
在centos.dockerfile文件里添加如下内容:
#设置基本的镜像,后续命令都以这个镜像为基础,指定的centos版本是7,注意版本前使用冒号,不能用-等,否则会报错找不到源数据。
FROM centos:7
#构建镜像的作者信息
MAINTAINER lht-2023-09
#重建rpm包数据库&安装依赖及所需要的工具&删除默认YUM源,使用YUM源为国内163 YUM源;
RUN rpm --rebuilddb;yum install make wget tar gzip passwd openssh-server gcc -y
#定义一个变量 变量的内容是下载163的基础源的地址
ENV REPOS_RUL http://mirrors.163.com/.help/CentOS7-Base-163.repo
#移除本地默认的yum源&下载163的基础源到指定位置
RUN rm -rf /etc/yum.repos.d/*;wget -P /etc/yum.repos.d/ $REPOS_RUL
#使用ssh-keygen生成ssh密钥,将yes命令的输出结果y,通过管道传递给ssh-keygen命令,这样ssh-keygen执行输出就不用再去输入“y”去确认了。
RUN yes|ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
RUN yes|ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
RUN yes|ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
#配置SSHD&修改root密码为Pa55Word
RUN echo 'Pa55Word' | passwd --stdin root
#启动SSHD服务进程,对外暴露22端口;
#只是声明端口,是做端口映射的依据
EXPOSE 22
CMD /usr/sbin/sshd -D
#ssh-keygen的主要参数如下:
# -b BITS 指定密钥长度。
# -e 读取私有或公共 OpenSSH 密钥文件并以 -m 选项指定的格式之一将密钥打印到标准输出。
# -C 添加注释。
# -f FILENAME 指定用来保存密钥的文件名。
# -i 以 -m 选项指定的格式读取未加密的私钥(或公钥)文件,并将 OpenSSH 兼容的私钥(或公钥)打印到 stdout。
# -l 显示公钥文件的指纹数据。
# -m KEY_FORMAT 为 -i(导入)或 -e(导出)转换选项指定密钥格式。支持的密钥格式为:“RFC4716”(RFC 4716/SSH2 公钥或私钥)、“PKCS8”(PEM PKCS8 公钥)或“PEM”(PEM 公钥)。 默认转换格式为“RFC4716”。
# -N 提供一个新密语。
# -P PASSPHRASE 提供(旧)密语。
# -q 静默模式。
# -t 指定要创建的密钥类型。可能的值为“dsa”、“ecdsa”、“ecdsa-sk”、“ed25519”、“ed25519-sk”或“rsa”。
# -y 从 OpenSSH 格式的私钥导出公钥。
1.2、使用docker build 来生成镜像
命令:docker build -t centos:ssh - < centos.dockerfile
2、搭建Harbor仓库
2.1、部署安装Docker-Compose快速编排工具
2.1.1、下载Docker-Compose
Docker-Compose本身就是一个程序(添加执行权限后可以执行),所以我们一般把他下载到/usr/local/bin/目录下
[root@localhost ~]# cd /usr/local/bin
[root@localhost bin]#
[root@localhost bin]# curl -L "https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@localhost bin]# ls
docker-compose docker-compose.bak
2.1.2、给Docker-Compose添加执行权限
[root@localhost bin]# chmod u+x docker-compose
[root@localhost bin]# ls
docker-compose docker-compose.bak
[root@localhost bin]# docker-compose --version
Docker Compose version v2.17.2
2.1.3、添加软链接(以后方便启动)
[root@localhost bin]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
[root@localhost bin]#
2.2、部署安装harbor
2.2.1、下载Habor并且解压
创建Habor下载目录并进入目录
[root@localhost bin]# cd /data/download/
[root@localhost download]# ls
[root@localhost download]# mkdir harbor
[root@localhost download]# cd harbor/
[root@localhost harbor]#
下载harbor
[root@localhost harbor]# wget -c https://github.com/goharbor/harbor/releases/download/v2.7.1/harbor-offline-installer-v2.7.1.tgz
--2023-09-14 23:15:58-- https://github.com/goharbor/harbor/releases/download/v2.7.1/harbor-offline-installer-v2.7.1.tgz
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/50613991/7a5d660d-3ac0-493e-ac50-ece279e39505?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-X4CSVEH53A%2F20230914%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230914T071601Z&X-Amz-Expires=300&X-Amz-Signature=01e0cf54ef76b0c371f46e0a666787922c6379a54c6c31514c4bc3cfHeaders=host&actor_id=0&key_id=0&repo_id=50613991&response-content-disposition=attachment%3B%20filename%3Dharbor-offline-installer-v2.7.1.tgz&response-content-type=applicatollowing]
--2023-09-14 23:15:59-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/50613991/7a5d660d-3ac0-493e-ac50-ece279e39505?X-Amz-Algorithm=AWS4-HMAtial=AKIAIWNJYAX4CSVEH53A%2F20230914%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230914T071601Z&X-Amz-Expires=300&X-Amz-Signature=01e0cf54ef76b0c371f46e0a666787922c6379a5473&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=50613991&response-content-disposition=attachment%3B%20filename%3Dharbor-offline-installer-v2.7.1.tgz&response-contenoctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.108.133, 185.199.111.133, 185.199.110.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 754307585 (719M) [application/octet-stream]
Saving to: ‘harbor-offline-installer-v2.7.1.tgz’
100%[=====================================================================================================================================>] 754,307,585 487KB/s in 67m 4
2023-09-15 00:23:43 (181 KB/s) - ‘harbor-offline-installer-v2.7.1.tgz’ saved [754307585/754307585]
解压下载的文件
[root@localhost harbor]# ls
harbor-offline-installer-v2.7.1.tgz
[root@localhost harbor]# tar -xzvf harbor-offline-installer-v2.7.1.tgz
harbor/harbor.v2.7.1.tar.gz
harbor/prepare
harbor/LICENSE
harbor/install.sh
harbor/common.sh
harbor/harbor.yml.tmpl
[root@localhost harbor]# ls
harbor harbor-offline-installer-v2.7.1.tgz
2.2.2、进入目录harbor并将文件名harbor.yml.tmpl修改成harbor.yml
[root@localhost harbor]# cd harbor
[root@localhost harbor]# ls
common.sh harbor.v2.7.1.tar.gz harbor.yml.tmpl install.sh LICENSE prepare
[root@localhost harbor]# mv harbor.yml.tmpl harbor.yml
[root@localhost harbor]# ls
common.sh harbor.v2.7.1.tar.gz harbor.yml install.sh LICENSE prepare
2.2.3、修改Habor配置文件harbor.yml
将hostname为本机IP地址,同时注释掉https部分(https默认是443端口,但是https我们没有证书,一定注释掉。要不然后面会提示不成功以及docker login登陆时会报错,所以我们只用80端口的http协议即可)
[root@localhost harbor]# vim harbor.yml
2.2.4、开始安装Habor
[root@localhost harbor]# ls
common.sh harbor.v2.7.1.tar.gz harbor.yml install.sh LICENSE prepare
[root@localhost harbor]# ./install.sh
2.2.5、在浏览器登陆Habor WEB平台
地址:宿主机的IP
默认用户名:admin,默认密码:Harbor12345可以在habor.cnf自己设置密码;
2.2.6、创建私有仓库用户并且绑定library仓库
创建用户名docker,并且设置密码
绑定library仓库
2.2.7、配置Insecure Registry
在Harbor HTTP Insecure Registry模式时,必须先配置Docker允许使用Insecure Registry。
编辑/etc/docker/daemon.json
加入以下内容:"insecure-registries":["192.168.88.191"]
[root@localhost harbor]# vim /etc/docker/daemon.json
注意:前一行后面要加上逗号后再回车才可以。
2.2.8、重启docker服务和docker-compose工具
重启docker服务
[root@localhost harbor]# systemctl restart docker
重启docker-compose工具
[root@localhost bin]# docker-compose restart
注意:重启docker-compose工具,需要进入harbor解压后的harbor目录,我这里是/data/download/harbor/harbor。
2.2.9、使用docker login命令测试登录创建的用户名和密码
[root@localhost harbor]# docker login 192.168.88.191
Username: docker
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
3、上传镜像到Harbor仓库
3.1、按照提示标记镜像
根据Harbor的提示标记镜像
[root@localhost harbor]# docker tag centos:ssh 192.168.88.191/library/centos-ssh:v1
[root@localhost harbor]#
3.2、通过docker push将镜像上传至Harbor仓库
[root@localhost harbor]# docker push 192.168.88.191/library/centos-ssh:v1
The push refers to repository [192.168.88.191/library/centos-ssh]
ab9048b44120: Pushed
a2a74060c379: Pushed
5f959363d8c4: Pushed
ef12a7cc7c2e: Pushed
e27f3f93d396: Pushed
20ea85c37c43: Pushed
174f56854903: Pushed
v1: digest: sha256:902aeb6152ddbdad2e44668977227fb56e2d7fc97551c2c3015704c4f58d36d8 size: 1778
[root@localhost harbor]#