Update content #227 #180

This commit is contained in:
khs1994
2017-11-22 20:28:10 +08:00
parent 1a75b7d669
commit eb1573a1fe
16 changed files with 119 additions and 144 deletions

View File

@@ -1,7 +1,7 @@
## Docker Hub
目前 Docker 官方维护了一个公共仓库 [Docker Hub](https://hub.docker.com/),其中已经包括了超过 15,000 的镜像。大部分需求,都可以通过在 Docker Hub 中直接下载镜像来实现。
### 注册
你可以在 https://hub.docker.com 免费注册一个 Docker 账号。
你可以在 https://cloud.docker.com 免费注册一个 Docker 账号。
### 登录
可以通过执行 `docker login` 命令交互式的输入用户名及密码来完成在命令行界面的登录。
登录成功后,本地用户目录的 `.dockercfg` 中将保存用户的认证信息。

View File

@@ -1,114 +0,0 @@
## 私有仓库
有时候使用 Docker Hub 这样的公共仓库可能不方便,用户可以创建一个本地仓库供私人使用。
本节介绍如何使用本地仓库。
`docker-registry` 是官方提供的工具,可以用于构建私有的镜像仓库。
### 安装运行 docker-registry
#### 容器运行
在安装了 Docker 后,可以通过获取官方 registry 镜像来运行。
```bash
$ docker run -d -p 5000:5000 registry
```
这将使用官方的 registry 镜像来启动本地的私有仓库。
用户可以通过指定参数来配置私有仓库位置,例如配置镜像存储到 Amazon S3 服务。
```bash
$ docker run \
-e SETTINGS_FLAVOR=s3 \
-e AWS_BUCKET=acme-docker \
-e STORAGE_PATH=/registry \
-e AWS_KEY=AKIAHSHB43HS3J92MXZ \
-e AWS_SECRET=xdDowwlK7TJajV1Y7EoOZrmuPEJlHYcNP2k4j49T \
-e SEARCH_BACKEND=sqlalchemy \
-p 5000:5000 \
registry
````
此外,还可以指定本地路径(如 `/home/user/registry-conf` )下的配置文件。
```bash
$ docker run -d \
-p 5000:5000 \
-v /home/user/registry-conf:/registry-conf \
-e DOCKER_REGISTRY_CONFIG=/registry-conf/config.yml \
registry
```
默认情况下,仓库会被创建在容器的 `/var/lib/registry `v1 中是`/tmp/registry`)下。可以通过 `-v` 参数来将镜像文件存放在本地的指定路径。
例如下面的例子将上传的镜像放到 `/opt/data/registry` 目录。
```bash
$ docker run -d \
-p 5000:5000 \
-v /opt/data/registry:/var/lib/registry \
registry
```
### 在私有仓库上传、下载、搜索镜像
创建好私有仓库之后,就可以使用 `docker tag` 来标记一个镜像,然后推送它到仓库,别的机器上就可以下载下来了。例如私有仓库地址为 `192.168.7.26:5000`
先在本机查看已有的镜像。
```bash
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu latest ba5877dc9bec 6 weeks ago 192.7 MB
ubuntu 14.04 ba5877dc9bec 6 weeks ago 192.7 MB
```
使用`docker tag``ba58` 这个镜像标记为 `192.168.7.26:5000/test`(格式为 `docker tag IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]`)。
```bash
$ docker tag ba58 192.168.7.26:5000/test
root ~ # docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 ba5877dc9bec 6 weeks ago 192.7 MB
ubuntu latest ba5877dc9bec 6 weeks ago 192.7 MB
192.168.7.26:5000/test latest ba5877dc9bec 6 weeks ago 192.7 MB
```
使用 `docker push` 上传标记的镜像。
```bash
$ docker push 192.168.7.26:5000/test
The push refers to a repository [192.168.7.26:5000/test] (len: 1)
Sending image list
Pushing repository 192.168.7.26:5000/test (1 tags)
Image 511136ea3c5a already pushed, skipping
Image 9bad880da3d2 already pushed, skipping
Image 25f11f5fb0cb already pushed, skipping
Image ebc34468f71d already pushed, skipping
Image 2318d26665ef already pushed, skipping
Image ba5877dc9bec already pushed, skipping
Pushing tag for rev [ba5877dc9bec] on {http://192.168.7.26:5000/v1/repositories/test/tags/latest}
```
用 curl 查看仓库中的镜像。
```bash
$ curl http://192.168.7.26:5000/v1/search
{"num_results": 7, "query": "", "results": [{"description": "", "name": "library/miaxis_j2ee"}, {"description": "", "name": "library/tomcat"}, {"description": "", "name": "library/ubuntu"}, {"description": "", "name": "library/ubuntu_office"}, {"description": "", "name": "library/desktop_ubu"}, {"description": "", "name": "dockerfile/ubuntu"}, {"description": "", "name": "library/test"}]}
```
这里可以看到 `{"description": "", "name": "library/test"}`,表明镜像已经被成功上传了。
现在可以到另外一台机器去下载这个镜像。
```bash
$ docker pull 192.168.7.26:5000/test
Pulling repository 192.168.7.26:5000/test
ba5877dc9bec: Download complete
511136ea3c5a: Download complete
9bad880da3d2: Download complete
25f11f5fb0cb: Download complete
ebc34468f71d: Download complete
2318d26665ef: Download complete
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
192.168.7.26:5000/test latest ba5877dc9bec 6 weeks ago 192.7 MB
```
可以使用 `docker-compose push` 批量上传本地的镜像到私有 Docker 仓库,这里以 `127.0.0.1:5000` 为例:
编写 `docker-compose.yml` 文件
```yaml
version: "3.4"
services:
ubuntu:
image: 127.0.0.1:5000/ubuntu:latest
centos:
image: 127.0.0.1:5000/centos:centos7
```
在该文件路径下执行 `docker-compose push` 即可将上面的两个镜像上传到私有仓库中。

90
repository/registry.md Normal file
View File

@@ -0,0 +1,90 @@
## 私有仓库
有时候使用 Docker Hub 这样的公共仓库可能不方便,用户可以创建一个本地仓库供私人使用。
本节介绍如何使用本地仓库。
`docker-registry` 是官方提供的工具,可以用于构建私有的镜像仓库。本文内容基于 `docker-registry` v2。
### 安装运行 docker-registry
#### 容器运行
你可以通过获取官方 `registry` 镜像来运行。
```bash
$ docker run -d -p 5000:5000 --restart=always --name registry registry
```
这将使用官方的 registry 镜像来启动本地的私有仓库。默认情况下,仓库会被创建在容器的 `/var/lib/registry ` 下。可以通过 `-v` 参数来将镜像文件存放在本地的指定路径。例如下面的例子将上传的镜像放到本地的 `/opt/data/registry` 目录。
```bash
$ docker run -d \
-p 5000:5000 \
-v /opt/data/registry:/var/lib/registry \
registry
```
### 在私有仓库上传、搜索、下载镜像
创建好私有仓库之后,就可以使用 `docker tag` 来标记一个镜像,然后推送它到仓库。例如私有仓库地址为 `127.0.0.1:5000`
先在本机查看已有的镜像。
```bash
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu latest ba5877dc9bec 6 weeks ago 192.7 MB
```
使用 `docker tag``ubuntu:latest` 这个镜像标记为 `127.0.0.1:5000/ubuntu:latest`(格式为 `docker tag IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]`)。
```bash
$ docker tag ubuntu:latest 127.0.0.1:5000/ubuntu:latest
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu latest ba5877dc9bec 6 weeks ago 192.7 MB
127.0.0.1:5000/ubuntu:latest latest ba5877dc9bec 6 weeks ago 192.7 MB
```
使用 `docker push` 上传标记的镜像。
```bash
$ docker push 127.0.0.1:5000/ubuntu:latest
The push refers to repository [127.0.0.1:5000/ubuntu]
373a30c24545: Pushed
a9148f5200b0: Pushed
cdd3de0940ab: Pushed
fc56279bbb33: Pushed
b38367233d37: Pushed
2aebd096e0e2: Pushed
latest: digest: sha256:fe4277621f10b5026266932ddf760f5a756d2facd505a94d2da12f4f52f71f5a size: 1568
```
`curl` 查看仓库中的镜像。
```bash
$ curl 127.0.0.1:5000/v2/_catalog
{"repositories":["ubuntu"]}
```
这里可以看到 `{"repositories":["ubuntu"]}`,表明镜像已经被成功上传了。
先删除已有镜像,再尝试去下载这个镜像。
```bash
$ docker rmi 127.0.0.1:5000/ubuntu:latest
$ docker pull 127.0.0.1:5000/ubuntu:latest
Pulling repository 127.0.0.1:5000/ubuntu:latest
ba5877dc9bec: Download complete
511136ea3c5a: Download complete
9bad880da3d2: Download complete
25f11f5fb0cb: Download complete
ebc34468f71d: Download complete
2318d26665ef: Download complete
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
127.0.0.1:5000/ubuntu:latest latest ba5877dc9bec 6 weeks ago 192.7 MB
```