目录

用免费 cloudflare 服务搭建 docker 镜像服务代理。Proxy Docker by Free Cloudflare

用免费 cloudflare 服务搭建 docker 镜像服务代理。

最近,docker主流镜像站都关了, 自己搭建一个加速器。

必备条件

  • cloudflare 账号
  • github 账号

fork github 代码

首先 fork https://github.com/ciiiii/cloudflare-docker-proxy 仓库到自己账号下。

然后修改 src/index.js,修改内容如下: 其中 mydomain.com 就是你的域名

1
2
3
4
5
6
7
8
9
const routes = {
  "docker.mydomain.com": "https://registry-1.docker.io",
  "quay.mydomain.com": "https://quay.io",
  "gcr.mydomain.com": "https://gcr.io",
  "k8s-gcr.mydomain.com": "https://k8s.gcr.io",
  "k8s.mydomain.com": "https://registry.k8s.io",
  "ghcr.mydomain.com": "https://ghcr.io",
  "cloudsmith.mydomain.com": "https://docker.cloudsmith.io",
};

再修改 wrangler.toml,同样是替换域名

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20

[env.production]
name = "cloudflare-docker-proxy"
routes = [
  { pattern = "docker.mydomain.com", custom_domain = true },
  { pattern = "quay.mydomain.com", custom_domain = true },
  { pattern = "gcr.mydomain.com", custom_domain = true },
  { pattern = "k8s-gcr.mydomain.com", custom_domain = true },
  { pattern = "k8s.mydomain.com", custom_domain = true },
  { pattern = "ghcr.mydomain.com", custom_domain = true },
  { pattern = "cloudsmith.mydomain.com", custom_domain = true },
]

[env.production.vars]
MODE = "production"
TARGET_UPSTREAM = ""

[env.staging]
name = "cloudflare-docker-proxy-staging"
route = { pattern = "docker-staging.mydomain.com", custom_domain = true }

最后修改 README.md 中的 Deploy to Cloudflare Workers 图标对应的 Github 仓库地址为你 fork 后的仓库地址,比如 https://deploy.workers.cloudflare.com/?url=https://github.com/xxxx(你的 github 账号)/cloudflare-docker-proxy

这3个修改都完成后,提交代码。

部署到 cloudflare

然后在 Github 你自己该代码仓库的 readme 界面, 点击 Deploy to Cloudflare Workers 图标进行部署,会自动跳转到 cloudflare,按步骤操作即可,最终会在 Github 仓库中创建一个 Github Action 来将该仓库部署到 Cloudflare Workers。

可以到 Github 查看 Action 执行进度

查看部署结果

执行完成后,切换到 Cloudflare Dashboard(https://dash.cloudflare.coms/) ,不出意外的话就可以看到刚创建的 Worker 了。

切换到 Setting,添加自定义域名。如果自己没有域名,也可以使用 cloudflare 提供的免费域名。

使用说明

为了加速 Docker 镜像拉取,你可以使用以下命令设置 registry mirror:

1
2
3
4
5
sudo tee /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": ["https://docker.yourdomain.com"]
}
EOF

配置完后需要重启 Docker 服务

1
sudo systemctl restart docker

使用该代理从不同的镜像仓库拉取镜像,请参考以下命令:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# docker pull nginx:latest
docker pull docker.yourdomain.com/library/nginx:latest  # 拉取 Docker 官方镜像

# docker pull quay.io/coreos/etcd:latest
docker pull quay.yourdomain.com/coreos/etcd:latest  # 拉取 Quay 镜像

# docker pull gcr.io/google-containers/busybox:latest
docker pull gcr.yourdomain.com/google-containers/busybox:latest  # 拉取 GCR 镜像

# docker pull k8s.gcr.io/pause:latest
docker pull k8s-gcr.yourdomain.com/pause:latest  # 拉取 k8s.gcr.io 镜像

# docker pull registry.k8s.io/pause:latest
docker pull k8s.yourdomain.com/pause:latest  # 拉取 registry.k8s.io 镜像

# docker pull ghcr.io/github/super-linter:latest
docker pull ghcr.yourdomain.com/github/super-linter:latest  # 拉取 GitHub 容器镜像

# docker pull docker.cloudsmith.io/public/repo/image:latest
docker pull cloudsmith.yourdomain.com/public/repo/image:latest  # 拉取 Cloudsmith 镜像

为了避免Worker 用量耗尽,你可以手动 pull 镜像然后 re-tag 之后 push 至本地镜像仓库。