解决 docker push 报错 405 Method Not Allowed

实践中发现 0.8.1 版本无法提交镜像(实际是说明文档的配置文件错误,见 部署Docker Registry测试环境), push 镜像的时候服务器端会返回 Error: Status 405 trying to push repository405 Method Not Allowed

警告:Docker-Registry 早期版本存在安全问题,建议尝试 docker-registry 推荐的docker虚拟容器运行最新版本,本文仅作为参考手段。

客户端提交测试:

docker commit -m "first commit" centos5 docker-repo.alipay.net/centos:centos5
docker push docker-repo.alipay.net/centos:centos5

提交映像文件的时候会出现 405 错误,提示方法不支持 - 以下是在服务器端输入的日志:

[info] POST /images/docker-repo.alipay.net/centos/centos5/push?tag=
[02396454] +job push(docker-repo.alipay.net/centos/centos5)
10.210.185.18 - - [20/Sep/2014:23:32:18] "GET /v1/_ping HTTP/1.1" 200 4 "-" "Go 1.1 package http"
2014-09-20 23:32:18,866 INFO: 10.210.185.18 - - [20/Sep/2014:23:32:18] "GET /v1/_ping HTTP/1.1" 200 4 "-" "Go 1.1 package http"
Error: Status 405 trying to push repository centos/centos5: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>

[02396454] -job push(docker-repo.alipay.net/centos/centos5) = ERR (1)
10.210.185.18 - - [20/Sep/2014:23:32:18] "PUT /v1/repositories/centos/centos5/ HTTP/1.1" 405 178 "-" "docker/1.2.0 go/go1.3.1 git-commit/fa7b24f kernel/3.10.0-123.6.3.el7.x86_64 os/linux arch/amd64"
2014-09-20 23:32:18,872 INFO: 10.210.185.18 - - [20/Sep/2014:23:32:18] "PUT /v1/repositories/centos/centos5/ HTTP/1.1" 405 178 "-" "docker/1.2.0 go/go1.3.1 git-commit/fa7b24f kernel/3.10.0-123.6.3.el7.x86_64 os/linux arch/amd64"

参考 405 status code

+1: still happening in 0.7.3, docker 0.12.0.

Solved by downgrading to 0.7.0.

我检查了一下当前安装的版本,确实是最新release 0.8.1

pip list | grep dock

输出显示

docker-registry (0.8.1)
docker-registry-core (2.0.1)

如何把版本降低到 0.7.0

参考 Downgrade the pip installed package

如果要安装特定版本,可以使用类似

pip install docker-registry==0.7.0

来完成。但是我尝试了,已经安装了 0.8.1 之后,使用命令

pip uninstall docker-registry
pip install docker-registry==0.7.0

但是安装完成之后,发现还是 0.8.1 ,仔细看了一下安装输出信息有

Requested docker-registry==0.7.0, but installing version 0.8.1

解决的方法参考 Installing specific package versions with Pip

也就是先卸载,然后通过 pip 直接下载指定url的 tar.gz 文件包进行安装

pip uninstall docker-registry
pip install -Iv https://pypi.python.org/packages/source/d/docker-registry/docker-registry-0.7.0.tar.gz
pip uninstall docker-registry-core
pip install -Iv https://pypi.python.org/packages/source/d/docker-registry-core/docker-registry-core-1.0.6.tar.gz
pip uninstall bugsnag
pip install -Iv https://pypi.python.org/packages/source/b/bugsnag/bugsnag-2.0.1.tar.gz
pip uninstall gunicorn
pip install -Iv https://pypi.python.org/packages/source/g/gunicorn/gunicorn-18.0.tar.gz
pip uninstall boto
pip install -Iv https://pypi.python.org/packages/source/b/boto/boto-2.27.0.tar.gz

根据你安装环境的软件包组合,可能会有不同的python模块需要重新安装,方法类似。

以上降级工作完成后,再次启动 docker-registry ,再次测试提交映像成功!