目录

搭建自己的docker镜像站

这篇文章介绍了如何搭建自己的docker镜像站

注意

注1、镜像仓库文档

注2、部署的服务器需要能拉取镜像

1 安装镜像仓库

1.1 配置仓库配置

任意位置新建文件夹放置镜像仓库

mkdir docker_registry && cd docker_registry

1.2 创建config.yml文件

我们创建一个config.yml文件

nano config.yml

文件中放入如下内容。

version: 0.1
log:
  fields:
    service: registry
    environment: development
storage:
    delete:
      enabled: true
    cache:
        blobdescriptor: inmemory
        blobdescriptorsize: 10000
    filesystem:
        rootdirectory: /var/lib/registry
    tag:
      concurrencylimit: 5
    maintenance:
      uploadpurging:
        enabled: true
        age: 48h
        interval: 24h
        dryrun: false
      readonly:
        enabled: true
http:
    #工作端口,如有冲突可自行调节
    addr: :5000


health:
  storagedriver:
    enabled: true
    interval: 10s
    threshold: 3
    

proxy:
  remoteurl: https://registry-1.docker.io
  #超时时间,超过缓存的镜像即失效
  ttl: 168h

1.3 创建docker-compose.yml文件

我们创建一个docker-compose.yml文件

nano docker-compose.yml

文件中放入如下内容。

services:
  ## docker hub
  dockerhub:
    container_name: docker-hub
    image: registry:3.0.0-rc.2
    restart: always
#    environment:
      #- OTEL_TRACES_EXPORTER=none
      #- http=http://host:port
      #- https=http://host:port
    volumes:
      - ./registry:/var/lib/registry
      - ./config.yml:/etc/distribution/config.yml
      #- ./htpasswd:/auth/htpasswd
    network_mode: host

1.4 启动容器

sudo docker-compose up -d

1.5 简单配置

如果不想进行反向代理,我们将镜像仓库改为ip:5000就可以进行拉取了。

2 反向代理

安装nginx,申请证书均省略。

2.1 nginx配置

nginx配置如下

server {
    #listen       80;
    listen       443 ssl;
    ## 填写绑定证书的域名
    server_name  hk11.606166.xyz;
    ## 证书文件名称(填写你证书存放的路径和名称)
    ssl_certificate /root/tls/hk11.606166.xyz.crt;
    ## 私钥文件名称(填写你证书存放的路径和名称)
    ssl_certificate_key /root/tls/hk11.606166.xyz.key;
    ssl_session_timeout 1d;
    http2 on;
    ssl_session_cache   shared:SSL:50m;
    ssl_session_tickets off;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_buffer_size 8k;
    chunked_transfer_encoding on;
    proxy_connect_timeout 600;
    proxy_send_timeout    600;
    proxy_read_timeout    600;
    send_timeout          600;

    location / {
        root   /usr/share/www/hk11.606166.xyz;   #结合实际自己设置网页根目录
        autoindex on;                   # 显示目录
        autoindex_exact_size off;       # 显示文件大小
        autoindex_localtime on;         # 显示文件时间
        charset utf-8;                  # 防止中文乱码
        #limit_rate 1m;                  # 限制下载速度为1MB
        sendfile on;                    # 开启零复制文件传输功能
        directio 5m;                    # 当文件大于5MB时以直接读取磁盘的方式读取文件
        index  index.html index.htm;
    }




    location /v2/ {
      # Do not allow connections from docker 1.5 and earlier
      # docker pre-1.6.0 did not properly set the user agent on ping, catch "Go *" user agents
      if ($http_user_agent ~ "^(docker\/1\.(3|4|5(?!\.[0-9]-dev))|Go ).*$" ) {
        return 404;
      }

      # To add basic authentication to v2 use auth_basic setting.
      #auth_basic "Registry realm";
      #auth_basic_user_file /etc/nginx/conf.d/nginx.htpasswd;

      ## If $docker_distribution_api_version is empty, the header is not added.
      ## See the map directive above where this variable is defined.
      #add_header 'Docker-Distribution-Api-Version' $docker_distribution_api_version always;

      proxy_pass                          http://localhost:5000;
      proxy_set_header  Host              $http_host;   # required for docker client's sake
      proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP
      proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
      proxy_set_header  X-Forwarded-Proto $scheme;
      proxy_read_timeout                  900;
    }

}

2.2 静态页

可创建上述nginx配置的根目录/usr/share/www/hk11.606166.xyz,并放入静态页文件

nano index.html

放入静态页代码内容如下,请结合实际修改

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Docker 镜像库</title>
    <style>
        /* 全局样式 */
        /* 侧边栏广告样式 */
        .sidebar-ad {
            position: fixed;
            right: 20px;
            top: 68%;
            transform: translateY(-50%);
            width: 200px;
            background-color: white;
            border: 2px solid #FF9800;
            padding: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
            text-align: center;
            border-radius: 10px;
            z-index: 1000;
        }
        .sidebar-ad img {
            max-width: 100%;
            border-radius: 5px;
        }
        .close-ad {
            position: absolute;
            top: 5px;
            right: 5px;
            background-color: red;
            color: white;
            border: none;
            cursor: pointer;
            font-size: 14px;
            padding: 2px 6px;
            border-radius: 50%;
        }

        body {
            font-family: 'Arial', sans-serif;
            line-height: 1.6;
            background-color: #f1f8ff; /* 背景颜色改为浅蓝色 */
            color: #333;
            margin: 0;
            padding: 0;
        }

        header {
            background-color: #4CAF50; /* 绿色背景 */
            color: white;
            padding: 20px 0;
            text-align: center;
        }

        header h1 {
            font-size: 2.5em;
            margin: 0;
        }

        nav {
            margin-top: 10px;
        }

        nav a {
            color: #fff;
            text-decoration: none;
            font-weight: bold;
            margin: 0 15px;
            font-size: 1.1em;
        }

        nav a:hover {
            text-decoration: underline;
            color: #ff9800; /* 橙色悬停 */
        }

        section {
            padding: 40px 20px;
            margin: 20px 0;
        }

        h2 {
            font-size: 2.2em;
            margin-bottom: 20px;
            color: #FF5722; /* 橙色标题 */
        }

        pre {
            background-color: #212121; /* 深灰色背景 */
            color: #f8f8f2; /* 白色文字 */
            padding: 10px 20px; /* 调整内边距,减小代码块的高度 */
            border-radius: 5px;
            margin-bottom: 20px;
            font-size: 1.1em;
            white-space: pre-wrap;  /* 支持换行 */
            word-wrap: break-word;  /* 支持长单词或长URL的换行 */
            overflow-x: auto;       /* 防止代码溢出 */
        }

        .container {
            width: 80%;
            margin: 0 auto;
        }

        footer {
            background-color: #4CAF50; /* 绿色背景 */
            color: white;
            text-align: center;
            padding: 10px 0;
            margin-top: 40px;
        }

        .code-block {
            background-color: #212121; /* 深灰色背景 */
            color: #f8f8f2; /* 白色文字 */
            padding: 10px 20px; /* 调整内边距,减小代码块的高度 */
            border-radius: 5px;
            overflow-x: auto;
            font-family: 'Courier New', Courier, monospace;
            margin-bottom: 30px;
            white-space: pre-wrap;  /* 支持换行 */
            word-wrap: break-word;  /* 支持长单词或长URL的换行 */
        }

        .code-block code {
            font-size: 1.1em;
        }

        h3 {
            color: #2196F3; /* 蓝色小标题 */
            margin-top: 20px;
        }

        ul {
            list-style-type: square;
            margin-left: 20px;
        }

        li {
            margin-bottom: 10px;
        }

        .button {
            background-color: #FF9800; /* 橙色按钮 */
            color: white;
            padding: 12px 24px;
            border-radius: 5px;
            text-decoration: none;
            font-weight: bold;
            font-size: 1.1em;
            margin-top: 20px;
            display: inline-block;
        }

        .links-section {
            margin-top: 40px;
        }

        .links-section h3 {
            color: #FF5722;
            margin-bottom: 15px;
        }

        .link-item {
            display: inline-block;
            margin-right: 20px;
            margin-bottom: 10px;
        }

        .link-item a {
            background-color: #4CAF50;
            color: white;
            padding: 12px 24px;
            border-radius: 5px;
            text-decoration: none;
            font-weight: bold;
            font-size: 1em;
            display: inline-block;
        }

        .link-item a:hover {
            background-color: #45a049; /* 按钮悬停时的效果 */
        }

        .button:hover {
            background-color: #FF5722; /* 橙色按钮悬停 */
        }
    </style>
</head>
<body>

    <!-- 侧边栏广告 -->
    <div class="sidebar-ad" id="ad-box">
        <h3>合理使用</h3>
        <button class="close-ad" onclick="document.getElementById('ad-box').style.display='none'">&times;</button>
        <a href="https://nginx.org/" target="_blank">
            <img src="https://nginx.org/img/nginx_logo.png" alt="广告">
        </a>
        <p>本项目使用nginx进行反向代理,感谢nginx</p>
        <p>个人合理使用,竭泽而渔将使本站走向GG,会尽量保持能用,但不做任何保证。</p>
        <p>相聚是缘,感谢大家的喜欢。</p>



    </div>



    <!-- 页面头部 -->
    <header>
        <div class="container">
            <h1>Docker 镜像库介绍</h1>
            <nav>
                <a href="#config">配置示例</a>
                <a href="#faq">常见问题</a>
            </nav>
        </div>
    </header>

    <!-- 配置示例 -->
    <section id="config">
        <div class="container">
            <h2>Docker 镜像库配置示例</h2>
            <p>以下是 Docker 配置和镜像库设置,帮助你根据需要配置 Docker 环境。</p>

            <h3>1. 编辑 Docker 配置文件(`daemon.json`)</h3>
            <div class="code-block"><code>
sudo nano /etc/docker/daemon.json</code></div>
            <p>你可以在 Docker 的配置文件中设置镜像加速器。</p>

            <div class="code-block">
<code>{
    "registry-mirrors": [
      "https://hk88.606166.xyz"
    ]
}</code></div>

            <p>解释:</p>
            <ul>
                <li><strong>registry-mirrors</strong>:指定镜像加速器地址。</li>

            </ul>

            <h3>2. 重启 Docker 服务</h3>
            <p>以下是一个简单的 Docker 重启命令</p>
            <div class="code-block"><code>
sudo systemctl restart docker</code></div>


        </div>
    </section>

    <!-- 常见问题 -->
    <section id="faq">
        <div class="container">
            <h2>常见问题</h2>
            <h3>如何解决镜像拉取失败的问题?</h3>
            <p>如果镜像拉取失败,可能到此服务器连接较差,令行寻找其他镜像加速站。</p>
            <h3>群晖无法搜索的问题?</h3>
            <p>本站不支持群晖镜像搜索功能。</p>
            <h3>无法拉取的问题?</h3>
            <p>docker hub的限制,每6小时可拉取100次。</p>
            <h3>镜像拉取失败的问题?</h3>
            <p>可能闪存已经爆满,无法缓存新镜像,我亦无能为力。</p>



        </div>
    </section>

    <!-- 友情链接部分 -->
    <section class="links-section">
        <div class="container">
            <h3>友情链接</h3>
            <div class="link-item">
                <a href="https://www.docker.com" target="_blank">Docker 官方网站</a>
            </div>
            <div class="link-item">
                <a href="https://hub.docker.com" target="_blank">Docker Hub</a>
            </div>
            <div class="link-item">
                <a href="https://hk11.606166.xyz/sh/" target="_blank">Docker 官方安装脚本存档</a>
            </div>
            <div class="link-item">
                <a href="https://hk11.606166.xyz/docker/compose/" target="_blank">Docker Compose 存档</a>
            </div>
            <div class="link-item">
                <a href="https://docker.imgdb.de" target="_blank">imgdb.de 镜像加速</a>
            </div>


        </div>
    </section>

    <!-- 页脚 -->
    <footer>
        <p>仅供交流学习使用,侵权联系删除。</p>
        <p>&copy; 2025 Docker 镜像库  - All rights reserved.</p>
    </footer>

</body>
</html>

都配置好重启nginx即可

2.3 使用

只需将docker的镜像站改为自己搭建的镜像站即可,在静态页亦有教学

3 结语

这个镜像仓库工具的功能还有很多,如上传自己的镜像到自己搭建的仓库的功能,我将其关闭了。大家可以结合文档,自己调节需要的功能