Remove sudo #206 AND Remove 「本地安装」「批量上传脚本」#208
parent
61be103f7f
commit
833aa02d92
|
@ -1,12 +1,13 @@
|
|||
## Docker Hub
|
||||
目前 Docker 官方维护了一个公共仓库 [Docker Hub](https://hub.docker.com/),其中已经包括了超过 15,000 的镜像。大部分需求,都可以通过在 Docker Hub 中直接下载镜像来实现。
|
||||
|
||||
### 注册
|
||||
你可以在 https://hub.docker.com 免费注册一个 Docker 账号。
|
||||
### 登录
|
||||
可以通过执行 `docker login` 命令来输入用户名、密码和邮箱来完成注册和登录。
|
||||
注册成功后,本地用户目录的 `.dockercfg` 中将保存用户的认证信息。
|
||||
可以通过执行 `docker login` 命令交互式的输入用户名及密码来完成在命令行界面的登录。
|
||||
登录成功后,本地用户目录的 `.dockercfg` 中将保存用户的认证信息。
|
||||
|
||||
### 基本操作
|
||||
用户无需登录即可通过 `docker search` 命令来查找官方仓库中的镜像,并利用 `docker pull` 命令来将它下载到本地。
|
||||
你可以通过 `docker search` 命令来查找官方仓库中的镜像,并利用 `docker pull` 命令来将它下载到本地。
|
||||
|
||||
例如以 centos 为关键词进行搜索:
|
||||
```
|
||||
|
|
|
@ -9,12 +9,12 @@
|
|||
#### 容器运行
|
||||
在安装了 Docker 后,可以通过获取官方 registry 镜像来运行。
|
||||
```
|
||||
$ sudo docker run -d -p 5000:5000 registry
|
||||
$ docker run -d -p 5000:5000 registry
|
||||
```
|
||||
这将使用官方的 registry 镜像来启动本地的私有仓库。
|
||||
用户可以通过指定参数来配置私有仓库位置,例如配置镜像存储到 Amazon S3 服务。
|
||||
```
|
||||
$ sudo docker run \
|
||||
$ docker run \
|
||||
-e SETTINGS_FLAVOR=s3 \
|
||||
-e AWS_BUCKET=acme-docker \
|
||||
-e STORAGE_PATH=/registry \
|
||||
|
@ -26,45 +26,27 @@ $ sudo docker run \
|
|||
````
|
||||
此外,还可以指定本地路径(如 `/home/user/registry-conf` )下的配置文件。
|
||||
```
|
||||
$ sudo docker run -d -p 5000:5000 -v /home/user/registry-conf:/registry-conf -e DOCKER_REGISTRY_CONFIG=/registry-conf/config.yml registry
|
||||
$ 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` 目录。
|
||||
```
|
||||
$ sudo docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry
|
||||
$ docker run -d \
|
||||
-p 5000:5000 \
|
||||
-v /opt/data/registry:/var/lib/registry \
|
||||
registry
|
||||
```
|
||||
|
||||
#### 本地安装
|
||||
对于 Ubuntu 或 CentOS 等发行版,可以直接通过源安装。
|
||||
* Ubuntu
|
||||
```
|
||||
$ sudo apt-get install -y build-essential python-dev libevent-dev python-pip liblzma-dev
|
||||
$ sudo pip install docker-registry
|
||||
```
|
||||
* CentOS
|
||||
```
|
||||
$ sudo yum install -y python-devel libevent-devel python-pip gcc xz-devel
|
||||
$ sudo python-pip install docker-registry
|
||||
```
|
||||
|
||||
也可以从 [docker-registry](https://github.com/docker/docker-registry) 项目下载源码进行安装。
|
||||
```
|
||||
$ sudo apt-get install build-essential python-dev libevent-dev python-pip libssl-dev liblzma-dev libffi-dev
|
||||
$ git clone https://github.com/docker/distribution
|
||||
$ cd distribution
|
||||
$ sudo docker build .
|
||||
```
|
||||
启动运行registry的容器
|
||||
```
|
||||
$ sudo docker run -d -p 5000:5000 --restart=always --name registry ${IMAGE_ID}
|
||||
```
|
||||
|
||||
###在私有仓库上传、下载、搜索镜像
|
||||
### 在私有仓库上传、下载、搜索镜像
|
||||
创建好私有仓库之后,就可以使用 `docker tag` 来标记一个镜像,然后推送它到仓库,别的机器上就可以下载下来了。例如私有仓库地址为 `192.168.7.26:5000`。
|
||||
|
||||
先在本机查看已有的镜像。
|
||||
```
|
||||
$ sudo docker images
|
||||
$ 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
|
||||
|
@ -72,7 +54,7 @@ ubuntu 14.04 ba5877dc9bec 6 week
|
|||
|
||||
使用`docker tag` 将 `ba58` 这个镜像标记为 `192.168.7.26:5000/test`(格式为 `docker tag IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]`)。
|
||||
```
|
||||
$ sudo docker tag ba58 192.168.7.26:5000/test
|
||||
$ 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
|
||||
|
@ -81,7 +63,7 @@ ubuntu latest ba5877dc9bec 6 week
|
|||
```
|
||||
使用 `docker push` 上传标记的镜像。
|
||||
```
|
||||
$ sudo docker push 192.168.7.26:5000/test
|
||||
$ 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)
|
||||
|
@ -102,7 +84,7 @@ $ curl http://192.168.7.26:5000/v1/search
|
|||
|
||||
现在可以到另外一台机器去下载这个镜像。
|
||||
```
|
||||
$ sudo docker pull 192.168.7.26:5000/test
|
||||
$ docker pull 192.168.7.26:5000/test
|
||||
Pulling repository 192.168.7.26:5000/test
|
||||
ba5877dc9bec: Download complete
|
||||
511136ea3c5a: Download complete
|
||||
|
@ -110,38 +92,23 @@ ba5877dc9bec: Download complete
|
|||
25f11f5fb0cb: Download complete
|
||||
ebc34468f71d: Download complete
|
||||
2318d26665ef: Download complete
|
||||
$ sudo docker images
|
||||
$ docker images
|
||||
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
|
||||
192.168.7.26:5000/test latest ba5877dc9bec 6 weeks ago 192.7 MB
|
||||
```
|
||||
|
||||
可以使用 [这个脚本](https://github.com/yeasy/docker_practice/raw/master/_local/push_images.sh) 批量上传本地的镜像到注册服务器中,默认是本地注册服务器 `127.0.0.1:5000`。例如:
|
||||
```
|
||||
$ wget https://github.com/yeasy/docker_practice/raw/master/_local/push_images.sh; sudo chmod a+x push_images.sh
|
||||
$ ./push_images.sh ubuntu:latest centos:centos7
|
||||
The registry server is 127.0.0.1
|
||||
Uploading ubuntu:latest...
|
||||
The push refers to a repository [127.0.0.1:5000/ubuntu] (len: 1)
|
||||
Sending image list
|
||||
Pushing repository 127.0.0.1:5000/ubuntu (1 tags)
|
||||
Image 511136ea3c5a already pushed, skipping
|
||||
Image bfb8b5a2ad34 already pushed, skipping
|
||||
Image c1f3bdbd8355 already pushed, skipping
|
||||
Image 897578f527ae already pushed, skipping
|
||||
Image 9387bcc9826e already pushed, skipping
|
||||
Image 809ed259f845 already pushed, skipping
|
||||
Image 96864a7d2df3 already pushed, skipping
|
||||
Pushing tag for rev [96864a7d2df3] on {http://127.0.0.1:5000/v1/repositories/ubuntu/tags/latest}
|
||||
Untagged: 127.0.0.1:5000/ubuntu:latest
|
||||
Done
|
||||
Uploading centos:centos7...
|
||||
The push refers to a repository [127.0.0.1:5000/centos] (len: 1)
|
||||
Sending image list
|
||||
Pushing repository 127.0.0.1:5000/centos (1 tags)
|
||||
Image 511136ea3c5a already pushed, skipping
|
||||
34e94e67e63a: Image successfully pushed
|
||||
70214e5d0a90: Image successfully pushed
|
||||
Pushing tag for rev [70214e5d0a90] on {http://127.0.0.1:5000/v1/repositories/centos/tags/centos7}
|
||||
Untagged: 127.0.0.1:5000/centos:centos7
|
||||
Done
|
||||
可以使用 `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` 即可将上面的两个镜像上传到私有仓库中。
|
||||
|
|
Loading…
Reference in New Issue