侧边栏壁纸
博主头像
J&S Blog

顺着一路星光,去往有你的嘉处

  • 累计撰写 14 篇文章
  • 累计创建 5 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

私有镜像仓库搭建

Administrator
2026-05-24 / 0 评论 / 0 点赞 / 1 阅读 / 0 字

Docker官方私有镜像仓库

#创建新目录
[root@www ~]# mkdir /pem
​
#创建数据持久目录,并生成用户的加密信息
[root@www pem]# docker run --rm -v /pem/auth:/auth httpd:alpine htpasswd -Bbc /auth/htpasswd registry 123456
​
#拉取脚本
[root@www pem]# wget https://github.com/FiloSottile/mkcert/releases/download/v1.4.4/mkcert-v1.4.4-linux-amd64
[root@www pem]# chmod +x mkcert-v1.4.4-linux-amd64
[root@www pem]# mv mkcert-v1.4.4-linux-amd64 mkcert
​
#生成证书
[root@www pem]# ./mkcert www.zgs.com  #可以接域名或IP
[root@www pem]# mkdir certs && mv *.pem certs
[root@www pem]# ll certs/
total 4688
-rwxr-xr-x 1 root root 4788866 Apr 26  2022 mkcert
-rw------- 1 root root    1704 Nov 20 08:56 www.zgs.com-key.pem     #私钥
-rw-r--r-- 1 root root    1464 Nov 20 08:56 www.zgs.com.pem         #证书
​
#配置nginx.conf
[root@www pem]# vim nginx.conf 
​
events {}
http {
    server {
        listen 443 ssl;
        server_name registry.ytlinux.com;
​
        ssl_certificate     /etc/nginx/certs/www.zgs.com.pem;
        ssl_certificate_key /etc/nginx/certs/www.zgs.com-key.pem;
        client_max_body_size 5G; #控制上传镜像的大小
        auth_basic "Private Docker Registry";
        auth_basic_user_file /etc/nginx/auth/htpasswd;
​
        location /v2/ {
            proxy_pass http://registry:5000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}
​
#配置docker-compose.yaml
[root@www pem]# vim docker-compose.yaml 
​
version: '3'
services:
  registry:
    image: registry:2
    restart: always
    volumes:
      - ./data:/var/lib/registry
​
  nginx:
    image: nginx:alpine
    restart: always
    ports:
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./auth:/etc/nginx/auth:ro
      - ./certs:/etc/nginx/certs:ro
    depends_on:
      - registry
      
#启动
[root@www pem]# docker compose up -d
​
#访问网站测试
https://www.zgs.com/v2

Harbor 企业级的镜像仓库

Docker的镜像可以借助扫描器来完成镜像的漏洞扫描

#拉取harbor的安装包
[root@www ~]# mkdir /harbor
[root@www ~]# cd /harbor/
[root@www harbor]# wget https://github.com/goharbor/harbor/releases/download/v2.14.0/harbor-offline-installer-v2.14.0.tgz
​
#证书生成
看上面部署docker官方私有镜像仓库的示例
​
#修改文件配置
[root@www harbor]# vim harbor.yml
​
hostname: www.zgs.com
​
harbor_admin_password: Harbor12345  #设置admin的密码
​
https:
  port: 443
  certificate: /pem/certs/www.zgs.com.pem #证书
  private_key: /pem/certs/www.zgs.com-key.pem #私钥
​
#运行shell脚本
[root@www harbor]# ./install.sh #不安装漏洞扫描工具
[root@www harbor]# ./install.sh --with-trivy #安装漏洞扫描工具
和harbor集成后,可以实现上传镜像到仓库,自动扫描;对于高风险的镜像自动阻止分发和用户拉取
​
#######################
#这个漏洞扫描的工具也可以单独安装进行使用
[root@www mnt]# wget https://github.com/aquasecurity/trivy/releases/download/v0.67.2/trivy_0.67.2_Linux-64bit.rpm
​
[root@www mnt]# rpm -ivh trivy_0.67.2_Linux-64bit.rpm 
​
#扫描镜像
[root@www mnt]# trivy image httpd:latest
[root@www harbor]# vim harbor.yml   #默认不需要修改
trivy:
    
  enabled: true                     # 是否启用 Trivy 扫描功能,必须开启才能扫描
   
  ignore_unfixed: false             #是否忽略未修复的漏洞,false会显示所有漏洞
    
  skip_update: false                #是否跳过漏洞数据库的自动更新,false表示启动时更新
​
  skip_java_db_update: false        #是否跳过java依赖数据库的更新,false表示更新
​
  offline_scan: false               #是否离线扫描(不联网拉取漏洞库),false表示联网更新
​
  security_check: vuln              #安全检查类型,vuln表示漏洞扫描;config表示配置检查;all表示全部
​
  insecure: false                   #是否允许访问不受信任(自签名)https仓库,使用自签证书时设为true
​
  timeout: 5m0s                     #超时时间
​
  github_token: xxx                 #GitHub令牌,提高漏洞库拉取速率,留空则使用匿名访问

注意

自建的镜像仓库因为https的证书是自生成的,因此需要忽略docker的仓库风险提示,也就是启用不受信任的仓库,要配置docker的daemon.json,并重启docker服务

[root@www conf.d]# vim /etc/docker/daemon.json 
​
{
        "registry-mirrors": [
                "https://docker.1ms.run"
        ],
        "insecure-registries": [
                "www.zgs.com"
        ]
}


0
博主关闭了所有页面的评论