Dockerfile相关(推送镜像?私有仓库?)(九)

上面我们讲到了 Dockerfile 的基本写法以及构建镜像的时候一些注意事项,那么镜像构建完成后,如何把我们的镜像给到别人使用呢?第一种方法就是利用 Docker 官方提供的公共的 Docker Hub 仓库,我们可以将镜像推送上去,然后别人就可以直接拉取镜像。

一、推送到官方镜像仓库

1.1 注册

首先需要 https://cloud.docker.com 免费注册一个 Docker 账号。

1.2 登录

然后通过执行docker login命令交互式的输入用户名及密码来完成在命令行界面登录 Docker Hub。

$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: zhangsan
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

1.3 注销

你可以通过docker logout退出登录。

1.4 推送镜像

用户在登录后可以通过docker push命令来将自己的镜像推送到 Docker Hub。以下命令中的 username 请替换为你的 Docker 账号用户名。

$ docker tag ubuntu:17.10 username/ubuntu:17.10
$ docker image ls

REPOSITORY                                               TAG                    IMAGE ID            CREATED             SIZE
ubuntu                                                   17.10                  275d79972a86        6 days ago          94.6MB
username/ubuntu                                          17.10                  275d79972a86        6 days ago          94.6MB
$ docker push username/ubuntu:17.10

二、私有仓库

有时候我们可能希望我们的镜像只在局域网范围内使用,不希望推送到 Docker Hub 这样的公共仓库,那么这个时候我们可以创建一个本地仓库供私人使用。

docker-registry 就是是官方提供的一个私有仓库工具,可以用于存储私有的镜像仓库。同样的我们可以通过获取官方 registry 镜像来直接运行:

$ docker run -d -p 5000:5000 --name registry registry:2

上面的命令会使用官方的 registry 镜像来启动私有仓库容器。默认情况下,仓库会被创建在容器的/var/lib/registry目录下。你可以通过 -v 参数来将镜像文件存放在本地的指定路径。例如下面的例子将上传的镜像放到本地的 /opt/data/registry 目录:


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

这样我们就运行了一个数据持久化的私有镜像仓库。

创建好私有仓库之后,然后可以使用docker tag来标记一个镜像,然后推送它到仓库。比如私有仓库地址为127.0.0.1:5000。先在本机查看已有的镜像。

$ 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 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上传标记的镜像:

$ 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: Pushedfc56279bbb33: Pushed
b38367233d37: Pushed
2aebd096e0e2: Pushed
latest: digest: sha256:fe4277621f10b5026266932ddf760f5a756d2facd505a94d2da12f4f52f71f5a size: 1568

此外,我们还可以使用 registry 仓库提供的 API 来查看仓库中的镜像:

$ curl 127.0.0.1:5000/v2/_catalog
{"repositories":["ubuntu"]}

这里可以看到 {"repositories":["ubuntu"]},表明镜像已经被成功上传了。

先删除已有镜像,再尝试从私有仓库中下载这个镜像。

$ docker image rm 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 image ls
REPOSITORY                         TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
127.0.0.1:5000/ubuntu:latest       latest              ba5877dc9bec        6 weeks ago         192.7 MB

到这里我们就完成了把镜像上传到了私有仓库中的完整过程。

注意事项

如果你不想使用 127.0.0.1:5000 作为仓库地址,比如想让本网段的其他主机也能把镜像推送到私有仓库。你就得把例如 192.168.199.100:5000 这样的内网地址作为私有仓库地址,这时你会发现无法成功推送镜像。

这是因为 Docker 默认不允许非 HTTPS 方式推送镜像。我们可以通过 Docker 的配置选项来取消这个限制,我们这里是 CentOS 7 系统,同样还是编辑文件/etc/docker/daemon.json,添加如下内容:

{
  "registry-mirror": [
    "https://registry.docker-cn.com"
  ],
  "insecure-registries": [
    "192.168.199.100:5000"
  ]
}

其中的insecure-registries就是我们添加的内容,然后重启 Docker 之后就可以在局域网内使用我们的私有镜像仓库了。

默认的私有镜像仓库可以满足我们的很多需求,但是往往在企业中使用的话还有很安全性方面的功能的缺失,比如权限管理之类的,这对于企业来说是非常重要的,不过 registry 对于权限管理这一块做了一个对外暴露的接口,只要我们实现他暴露的安全接口就可以来实现权限管理相关的接口,其中注明的开源镜像管理软件 Harbor 就是这类应用的佼佼者,我们还在后面的课程中和大家学习 Harbor 的一个使用。

下一篇将介绍Kubernetes基本概念
(转发请注明出处:http://www.cnblogs.com/zhangyongli2011/ 如发现有错,请留言,谢谢)

posted @ 2022-12-27 10:39  非法小恋  阅读(93)  评论(0编辑  收藏  举报