dockerregistry(docker registry部署)

本篇文章给大家谈谈dockerregistry,以及docker registry部署对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

什么是docker registry

就是镜像仓库,存放docker镜像的地方。橘含推送或者拉取docker镜像需要指定圆银笑镜像仓库搏唤,如果是私有的镜像仓库还需要输入账号和密码。

如何搭建及使用docker registry

registry是什么?

registry是Docker的镜像存储服务,docker hub上的registry镜像见Registry官方镜像,更多详细信息请哗铅参见源码。

搭建registry

在服务器上执行如下命令安装docker,这里选择腾讯云(Ubuntu Server 14.04.1 LTS 64位)镜像来创建服务器

curl -fsSL | sh

安装docker-compose Docker Compose是一个定义及运行多个Docker容器的工具。使用Docker Compose只需要在一个配置文件中定义多个Docker容器,然后使用一条命令将多个容器启动,Docker Compose会通过解析容器间的依赖关系,按先后顺序启动所定义的容器。详见Docker Compose

curl -L (uname -s)-$(uname -m) /usr/local/bin/docker-compose

chmod a+x /usr/local/bin/docker-compose

启动registry服务,此例中包含nginx和registry两个容器,涉及的包及配置文件请参见附件,解压后,直接执行如下命令即可创建服务。

docker-compose up -d

停止服务

docker-compose stop

重启服务

docker-compose restart

下线服务

docker-compose down

上传镜像

因为上面搭建的registry服务是http的,所以docker启动参数需要配置--insecure-registry localhost选项,修改/etc/default/docker文件

DOCKER_OPTS="--insecure-registry localhost"

重启docker

service docker restart

拉取上传镜像 docker pull;docker tag;docker push(tag默认为latest)

docker pull hello-world

docker tag hello-world localhost/library/hello-world

docker push localhost/library/hello-world

下载镜像

docker pull localhost/library/hello-world

删除镜像

docker rmi localhost/library/hello-world

获取镜像仓库列表

# curl

{"repositories":["library/hello-world"]}

未上传镜像前的输出如下:

# curl

{"repositories":[]}

获取镜像tag列表

# curl -X GET

{"name":"library/hello-world","tags":["latest"]}

获取镜像manifests信息段纯

# curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X GET

{

"schemaVersion": 2,

"mediaType"乱燃好: "application/vnd.docker.distribution.manifest.v2+json",

"config": {

"mediaType": "application/vnd.docker.container.image.v1+json",

"size": 1473,

"digest": "sha256:c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc"

},

"layers": [

{

"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",

"size": 974,

"digest": "sha256:c04b14da8d1441880ed3fe6106fb2cc6fa1c9661846ac0266b8a5ec8edf37b7c"

}

]

}

其中c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc即为执行docker images时看到的IMAGE ID。 layers表示了镜像的层次关系,可以通过layers中的digest来拉取blob,见下面获取镜像blob

获取镜像blob

在上面获取hello-world:latest镜像的manifests信息中可以看到其只有一个layer,以此为例来看如何获取镜像blob。从拉取的结果可以看到获取的blob与文件sha256是一致的。执行docker pull实际上就是首先获取到镜像的manifests信息后,再拉取blob的。

# curl -s -X GET -o hello-world.blob

# ls -l hello-world.blob

-rw-r--r-- 1 root root 974 Nov 23 09:56 hello-world.blob

# sha256sum hello-world.blob

c04b14da8d1441880ed3fe6106fb2cc6fa1c9661846ac0266b8a5ec8edf37b7c hello-world.blob

##删除镜像(soft delete)

首先通过curl -i 参数获取到镜像的Docker-Content-Digest,registry 2.3及以后的版本必须在header中指定Accept: application/vnd.docker.distribution.manifest.v2+json,否则默认返回的是schema1的digest,其与schema2的digest不同,使用不指定上述头信息返回的digest删除时会返回404。

# curl -i -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X GET

HTTP/1.1 200 OK

Server: nginx/1.11.5

Date: Wed, 23 Nov 2016 02:17:51 GMT

Content-Type: application/vnd.docker.distribution.manifest.v2+json

Content-Length: 524

Connection: keep-alive

Docker-Content-Digest: sha256:a18ed77532f6d6781500db650194e0f9396ba5f05f8b50d4046b294ae5f83aa4

Docker-Distribution-Api-Version: registry/2.0

Etag: "sha256:a18ed77532f6d6781500db650194e0f9396ba5f05f8b50d4046b294ae5f83aa4"

{

"schemaVersion": 2,

"mediaType": "application/vnd.docker.distribution.manifest.v2+json",

"config": {

"mediaType": "application/vnd.docker.container.image.v1+json",

"size": 1473,

"digest": "sha256:c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc"

},

"layers": [

{

"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",

"size": 974,

"digest": "sha256:c04b14da8d1441880ed3fe6106fb2cc6fa1c9661846ac0266b8a5ec8edf37b7c"

}

]

}

根据上一步返回的Docker-Content-Digest删除,返回202表示删除成功

# curl -k -v -s -X DELETE

* Hostname was NOT found in DNS cache

* Trying 127.0.0.1...

* Connected to localhost (127.0.0.1) port 80 (#0)

DELETE /v2/library/hello-world/manifests/sha256:a18ed77532f6d6781500db650194e0f9396ba5f05f8b50d4046b294ae5f83aa4 HTTP/1.1

User-Agent: curl/7.35.0

Host: localhost

Accept: */*

**HTTP/1.1 202 Accepted**

* Server nginx/1.11.5 is not blacklisted

Server: nginx/1.11.5

Date: Wed, 23 Nov 2016 02:29:59 GMT

Content-Type: text/plain; charset=utf-8

Content-Length: 0

Connection: keep-alive

Docker-Distribution-Api-Version: registry/2.0

* Connection #0 to host localhost left intact

确认结果

# curl -X GET

{"name":"library/hello-world","tags":null}

删除镜像(hard delete)

在上一步中,只是删除了镜像的manifests信息,解引用的blob还在占用磁盘空间,执行如下命令可以查看可以删除的blob

docker exec -it myregistry_registry_1 /bin/registry garbage-collect --dry-run /etc/registry/config.yml

要删除blob,释放磁盘空间,需要执行下面的命令。需要特别注意的是在执行下面的命令时registry必须是只读模式(只读模式可在registry配置文件中设置),否则可能会导致数据不一致。

docker exec -it myregistry_registry_1 /bin/registry garbage-collect /etc/registry/

[img]

如何部署 Docker Registry 服务

官方镜像下的简单示例本节中,将创建一个Container来孝友运行Docker的官方Registry镜像。你将推送(Push)一个镜像到这个Registry服务器,然后再从该Registry中拉取(Pull)同一个镜像。这是个很好的弊卖练习,有助于理解客户端与本地Registry的基本交互。1、安装Docker。2、从Docker公共Registry中运行hello-world镜像。$dockerrunhello-worldrun命令自动从Docker的官方镜像库中将hello-world镜像pull下来。3、在localhost上启动Registry服务。$dockerrun-p5000:5000registry:2.0这将在DOCKER_HOST上启动一个Registry服务,并在5000端口监听。4、列出镜像。$dockerimagesREPOSITORYTAGIMAGEIDCREATEDVIRTUALSIZEregistry2.0bbf0b6ffe9233daysago545.1MBgolang1.4121a93c904635daysago514.9MBhello-worldlateste45a5af57b003monthsago910B这个列表应当包括一个由先前运行而得来的hello-world镜像。5、为本地repoistory重新标记hello-world镜像。$dockertaghello-world:latestlocalhost:5000/hello-mine:latest此命令使用[REGISTRYHOST/]NAME[:TAG]格式为hello-world:latest重新打标。REGISTRYHOST在此例中是localhost。在MacOSX环境中,得把localhost换成$(boot2dockerip):5000。6、列出新镜像。租慎逗$dockerimagesREPOSITORYTAGIMAGEIDCREATEDVIRTUALSIZEregistry2.0bbf0b6ffe9233daysago545.1MBgolang1.4121a93c904635daysago514.9MBhello-worldlateste45a5af57b003monthsago910Blocalhost:5000/hello-minelatestef5a5gf57b013monthsago910B可以看到,新镜像已经出现在列表中。7、推送新镜像到本地Registry中。$dockerpushlocalhost:5000/hello-mine:latestThepushreferstoarepository[localhost:5000/hello-mine](len:1)e45a5af57b00:Imagealreadyexists31cbccb51277:Imagesuccessfullypushed511136ea3c5a:ImagealreadyexistsDigest:sha256:a1b13bc01783882434593119198938b9b9ef2bd32a0a246f16ac99b01383ef7a8、使用curl命令及DockerRegistry服务APIv2列出Registry中的镜像:$curl-v-XGET*HostnamewasNOTfoundinDNScache*Trying127.0.0.1*Connectedtolocalhost(127.0.0.1)port5000(#0)GET/v2/hello-mine/tags/listHTTP/1.1User-Agent:curl/7.35.0Host:localhost:5000Accept:*/*

docker registry使用指南

docker registry 的作用就是存储我们的镜像。通常情况下我们可以使用 docker hub 来存储,不过如果是在公司内部使用,不想将镜像公开,可以手动搭建一个本地registry,如 docker registry 或 harbor 。本文简单介绍一下 docker registry 的搭建使用及常用配置。

搭建registry最基础的命令为:

docker run -d -v /data/registry:/var/lib/registry -p 5000:5000 registry:2

registry定义的对外服务端口为 5000 ,我们也可以通过环境变量 REGISTRY_HTTP_ADDR 来修改服务端口。

如果要使用其他存储,如 Amazon S3 bucket , Google Cloud Platform 或其他docker支持的 存储 ,也可以通过环境变量单独配置(推荐用yaml的形式来配置)。

注:私有仓库,推送镜像时,要在 /etc/docker/daemon.json 或 C:\ProgramData\docker\config\daemon.json 文件中添加以下配置,并重启docker。

创建服务端证书的第三步,可能会报错 unable to open '/etc/pki/CA/index.txt' ,需要手动创建该文件 touch /etc/pki/CA/index.txt , 并创建一个序列文件来标记CA证书 echo '1000' /etc/pki/CA/serial 。

证书生成也可以参考:

为了提高regsitry的安全性,可以开启访问控制,用户需要登陆后才可以使用registry。

首先,创建一个密码文件,里面包含一条用户名密码(stark/catherine)。

windows系统下需要修改编码格式:

docker run --rm --entrypoint htpasswd httpd:2 -Bbn testuser testpassword | Set-Content -Encoding ASCII auth/htpasswd

然后启动容器,带上用户认证。

配置一个域名解析: echo 127.0.0.1 myregistry.com /etc/hosts ,然后使用docker登录 docker login myregistry.com:5000 ,用户名/密码就是前面配置的stark/catherine。

注:使用身份认证,建议开启TLS,否则登录信息明文传输(header中),一样不安全。

x509报错解决:x509报错通常就是自签证书没有加入到docker client所局升在host的信任证书中,手动加入即可。对于linux用户,只需要拷贝根证书文件到 /etc/docker/certs.d/myregistrydomain.com:5000/ca.crt 中即可。

最后,也可以在浏览器中访问 或是 查看仓库信息。

以上只是最简单的用户认证,只能使用我们预先定义好的用户来访问。

我们也可以在regsitry之前使用一个代枯没理,来实现没腊纳更高级的身份认证;或者将registry集成到我们自己的身份认证和访问控制系统中,由我们的鉴权服务来签发token给用户,然后用户使用签发的token访问我们的registry。harbor就提供了这样一整套服务,如果有此方面的需求,可以考虑使用harbor。

不知到为啥,容器起来了,但访问不了

完整的配置选项 参考 。

配置文件和环境变量的对应关系。配置文件中的内容为:

上述配置文件如果通过环境变量来配置,则对应的环境变量为:

REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/var/lib/registry

格式为 REGISTRY_variable ,其中 variable 为配置的变量,通过 _ 连接yaml文件中的各层变量得到。

token 认证流程简图:

关于dockerregistry和docker registry部署的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

标签列表