docker搭建基于局域网的私有仓库(带用户权限)

有时候使用Docker Hub这样的公共仓库可能不方便,现在许多公司也都搭建属于自己内部用的仓库。
    该文章将会讲述如何搭建带用户权限的私有仓库,使用私有仓库的优点:
1、节省网络带宽,每个镜像不用每个人都去官网下载,只需要从私有仓库中下载即可。2、提供镜像资源利用,针对公司内部使用的镜像,推送到本地的私有仓库,供公司内部的人员使用。
环境准备:
    两台装有docker的Centos7虚拟机(尽量保证docker的版本都一致)
     虚拟机一:192.168.0.124 用作私有仓库
     虚拟机二:192.168.0.122 普通用户机
搭建:

    1、私有仓库是基于registry镜像简单制作的。首先下载registry镜像。

[root@localhost ~]#docker pull registry
[root@localhost ~]# docker images | grep registry
registry                     latest              708bc6af7e5e        3 weeks ago         25.8MB

   2、默认情况下,会将仓库放在容器内的/tmp/registry目录下,这样有个问题,如果容器被删除,则存放在容器中的镜像也会丢失。所以一般情况下建议指定本地一个目录挂载到容器内的/tmp/registry下(新版本是/var/lib/registry)

[root@localhost ~]# docker run -d -p 5000:5000 -v /docker/registry/:/var/lib/registry/ --restart=always registry
ed679207b41c21bf91bad30a7fe44e2529b5cbbf10ca89002609a690b4bc99c4
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
ed679207b41c        registry            "/entrypoint.sh /etc…"   35 seconds ago      Up 34 seconds       0.0.0.0:5000->5000/tcp   infallible_banach

   3、在向私有镜像push 镜像时,docker从1.3之后,与docker registry交互默认使用的是https,此处只是http服务,因此需要在修改docker的配置文件,修改有两种办法。一种是修改新版docker下的daemon.json文件。如下:

[root@localhost registry]# cat /etc/docker/daemon.json
{
   "registry-mirrors": ["https://*********.mirror.aliyuncs.com"],
    "insecure-registries": [ "192.168.0.124:5000"]
}

另一种是:

vim /usr/lib/systemd/system/docker.service
添加:
:ADD_REGISTRY='--insecure-registry 192.168.0.124:5000'

笔者更倾向于第一种办法。第二种属于老办法。不知道新版是否还适用。
   4、重启docker。
   5、向私有仓库push 镜像。push 前先修改镜像的tag,也就是打标签。

[root@localhost ~]docker tag busybox 192.168.0.124:5000/busybox
[root@localhost ~]# docker images | grep 192.168.0.124:5000*
192.168.0.124:5000/centos    latest              470671670cac        4 weeks ago         237MB
192.168.0.124:5000/busybox   latest              6d5fcfe5ff17        7 weeks ago         1.22MB

上面的busybox也是一个操作系统,特别小,适合docker的练习。
    6、准备工作都进行完毕,正式push镜像。

[root@localhost registry]# docker push 192.168.0.124:5000/busybox
The push refers to repository [192.168.0.124:5000/busybox]
195be5f8be1d: Pushed
latest: digest: sha256:edafc0a0fb057813850d1ba44014914ca02d671ae247107ca70c94db686e7de6 size: 527

    7、查看私有仓库的所有镜像:

[root@localhost registry]# curl 192.168.0.124:5000/v2/_catalog
{"repositories":["busybox","centos"]}

    8、客户端下载该镜像前,首先修改docker配置文件,添加仓库地址,添加的方法跟上面192.168.0.124的方法相同。仓库地址指向192.168.0.124即可
    9、从私有仓库中下载已有的镜像:

[root@localhost ~]# docker pull 192.168.0.124:5000/busybox
Using default tag: latest
latest: Pulling from busybox
bdbbaa22dec6: Pull complete
Digest: sha256:edafc0a0fb057813850d1ba44014914ca02d671ae247107ca70c94db686e7de6
Status: Downloaded newer image for 192.168.0.124:5000/busybox:latest
192.168.0.124:5000/busybox:latest
[root@localhost ~]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
192.168.0.124:5000/centos    latest              470671670cac        4 weeks ago         237MB
192.168.0.124:5000/busybox   latest              6d5fcfe5ff17        7 weeks ago         1.22MB

    10、嫌镜像名太长,可以tag呀!!
    11、上述客户端完全不用任何认证就可以pull镜像。当然我不想所有人都访问时,可以配置带有用户权限的registry,只有当用户登录用户名与密码后才能使用。registry的用户名和密码由htpasswd来生成。

[root@localhost docker]# pwd
/docker
[root@localhost docker]# mkdir ./auth
[root@localhost docker]# docker rm -f `docker ps -a -q`  #之前的仓库先停掉
64d9bbbe43c5
[root@localhost docker]# docker run --entrypoint htpasswd registry -Bbn tom 123456 > ./auth/htpasswd
[root@localhost docker]# cat ./auth/htpasswd
tom:$2y$05$C/gcw1VYz2BofRy9yltBHOrYiGyaJY603DEVmzjMBHnfflRY0/Utm

    12、重新启动容器

[root@localhost docker]# docker run -d -p 5000:5000 --restart=always -v /docker/auth/:/auth/ -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd -v /docker/registry/:/var/lib/registry/ registry
a79e8636e09419e74033361f8b2cf31521ff48b775c7392b3ce35a0948dba923
[root@localhost docker]# docker exec -it a79e sh
/ # cat auth/htpasswd
tom:$2y$05$C/gcw1VYz2BofRy9yltBHOrYiGyaJY603DEVmzjMBHnfflRY0/Utm

    13、客户端删除之前下载的镜像,重新测试

[root@localhost ~]# docker rmi -f `docker iamges -a -q`
[root@localhost ~]# docker pull 192.168.0.124:5000/busybox
Using default tag: latest
Error response from daemon: Get http://192.168.0.124:5000/v2/busybox/manifests/latest: no basic auth credentials

    14、pull不了????登录一下就好了

[root@localhost ~]# docker login 192.168.0.124:5000
Username: tom
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

    15、可以看到登录认证通过后的文件保存在/root/.docker/config.json下

[root@localhost ~]# cat /root/.docker/config.json
{
    "auths": {
        "192.168.0.124:5000": {
            "auth": "dG9tOjEyMzQ1Ng=="
        }
    },
    "HttpHeaders": {
        "User-Agent": "Docker-Client/19.03.6 (linux)"
    }

    16、再次下载

[root@localhost ~]# docker pull 192.168.0.124:5000/busybox
Using default tag: latest
latest: Pulling from busybox
bdbbaa22dec6: Pull complete
Digest: sha256:edafc0a0fb057813850d1ba44014914ca02d671ae247107ca70c94db686e7de6
Status: Downloaded newer image for 192.168.0.124:5000/busybox:latest
192.168.0.124:5000/busybox:latest

    17、当然,仓库端要想push镜像也需要登录通过。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值