本文介绍本人在 Centos 7.1 上的搭建过程 private docker registry 的全过程,参考自这篇
官网文档 ,英语好的可以直接看官网文档,里面的内容更详细,涉及更多原理性的东西,而本文侧重于动手实践。
本文不介绍 docker 的基础概念,也不介绍为什么要搭建 private docker registry(这方面的内容网上有太多文档)。并且,虽然可以在好几种不同的环境中搭建,但本文只介绍在 Centos 7.1 上的搭建过程,其它版本的系统搭建过程,也请查阅上面给出过的
官网文档 。
先安装 docker 1.6 或更高版本 0. 前提条件 需要 centos 7 64位系统,且内核版本最少为 3.10。可通过下面指令检查你的内核版本:
3.10.0-229.14.1.el7.x86_64
1.安装 安装 docker 有三种方式:
通过源码安装。又可分为两种方式,完全通过源码安装和通过 docker 自身完成安装。前一种方式不仅非常复杂,而且网上介绍的文章很少,这里不介绍。后一种需要你的主机已经安装了 docker,这个要求有点先有鸡还是先有蛋的意味,这里也不介绍。 通过 yum 安装。这种方式最简单,正是本文要介绍的,它会自动帮你下载安装各种依赖文件。 通过 curl 来安装。这种方式其实是运行一个安装脚本,在这个脚本里面也是通过 yum 来安装的。
通过 yum 安装的步骤如下:
1.1,使用 root 或其它有 sudo 权限的账号登录进系统。以下过程假设你使用 root 登录,如果是其它账号,请在命令最前面加上 sudo。
1.3,添加 yum 仓库源
tee /etc/yum.repos.d/docker.repo <<- 'EOF '
> baseurl=https://yum.dockerproject.org/repo/main/centos/7/
> gpgkey=https://yum.dockerproject.org/gpg
1.4,安装 docker 包
$ yum install docker-engine
1.5,启动 docker 守护进程
1.6,到这一步安装 docker 就算完成了,我们可以运行一个 hello-world 容器来验证一下安装是否正确
$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from hello-world
a8219747be10: Pull complete
91c95931e552: Already exists
hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1.7.1cf5daeb82aab55838d
Status: Downloaded newer image for hello-world:latest
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (Assuming it was not already locally available.)
3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
For more examples and ideas, visit:
http://docs.docker.com/userguide/
出现上述这样的输出,就说明安装正确了。
2.创建一个 docker 用户组 docker 守护进程绑定到一个 unix socket 而不是 tcp 端口。默认情况下,这个 unix socket 的所有者是 root,其他用户需要通过 sudo 的方式访问。因为 docker 守护进程总是以 root 用户运行。
为避免使用 docker 命令的时候必须使用 sudo,我们可以创建一个 docker 用户组,然后将你的用户添加到这个组里面。当 docker 守护进程启动时,它会让 docker 用户组对该 unix socket 有可读可写的权限。
当然如果你只会使用 root 账号登录使用 docker 命令,也可略过这个步骤,对后续进程不会有影响。
创建用户组并添加用户的步骤如下:
2.1,使用 sudo 权限登录进系统
2.2,创建 docker 用户组
2.3,将你的用户添加到 docker 组
$ sudo usermod -aG docker your_username
2.4,退出登录然后再登录进来
这确保你的账号有正确的权限
2.5,不使用 sudo 来执行 docker 命令,验证是否正确
3. 让 docker 守护进程开机自启动
为了确保当你重启系统的时候,docker 可以自动启动,执行如下指令:
搭建 Docker Registry 以 localhost 运行 要安装并启动一个 docker registry 非常简单,只需要执行下面这条指令:
$ docker run -d -p 5000:5000 --restart=always --name registry registry:2
现在就能够使用这个 registry 了,下面测试一下。
从 docker 官方镜像仓库获取一个镜像,并使用 tag 指令让它指向到你的仓库(这里我们就以上面下载的 hello-world 镜像为例):
$ docker tag hello-world localhost:5000/hello-world:latest
然后将它推送到你的仓库:
$ docker push localhost:5000/hello-world
然后从本地删除这个 hello-world 镜像:
$ docker rmi hello-world localhost: 5000 /hello-world
可以通过命令 docker images 看到 hello-world 镜像已经不存在了
然后再从仓库中把它拉取出来:
关闭
站长推荐 /6