docker搭建私仓

2023-12-27 13:30:24

Docker Registry是官方提供的工具,用于构建私有镜像仓库。

3.1 环境搭建

Docker Registry也是Docker Hub提供的一个镜像,可以直接拉取运行。

1. 拉取镜像

docker pull registry

2. 启动Docker Registry

docker run -d -p 5000:5000 -v /data/myregistry/:/tmp/registry --privileged=true registry

3. 验证(查看私服中的所有镜像),Registry会返回json格式的所有镜像目录

curl http://IP:端口/v2/_catalog        #IP和端口为docker主机IP

输出:

{"repositories":[]}

3.2 向Registry私仓中上传镜像

1. 从Hub上下载ubuntu镜像到本地并成功运行

docker pull ubuntu 
docker run -it -d ubuntu 

原始的ubuntu镜像是不带着ifconfig命令的,在外网连通的情况下,安装ifconfig命令并测试通过

连接容器:

docker exec -it 容器id /bin/bash

安装net-tools:

apt-get update  # 更新
apt-get install -y net-tools

完成验证:

root@550e40f3d370:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 6461  bytes 20857541 (20.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4455  bytes 242481 (242.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

退出容器:exit

安装完成后,commit我们自己的新镜像:

docker commit -m="ifconfig cmd add" -a="josh" 容器id joshubuntu:1.2

查看本地镜像仓库中joshubuntu是否存在:

docker images

输出:

REPOSITORY            TAG       IMAGE ID       CREATED                  SIZE
ubuntu                latest    b6548eacb063   Less than a second ago   77.8MB
registry              latest    909c3ff012b7   Less than a second ago   25.4MB
joshubuntu            1.2       244f7de5628a   13 seconds ago           112MB

2. 启动我们的新镜像并与原来的对比

docker stop 容器id    # 停掉原来的ubuntu 

docker run -it 镜像id /bin/bash    # 启动一个新的测试ifconfig

进入容器,使用ifconfig命令查看情况:

docker exec -it 容器id /bin/bash
root@30df0fa3c1f0:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3. 将新镜像修改符合docker镜像规范

按照公式:docker tag 镜像:Tag Host:Port/Repository:Tag

使用命令 docker tag 将joshubuntu:1.2 这个镜像修改为192.168.31.233:5000/zzyyubuntu:1.2

docker tag joshubuntu:1.2 192.168.31.223:5000/joshubuntu:1.2

检验:

[root@localhost data]# docker images
REPOSITORY                        TAG       IMAGE ID       CREATED                  SIZE
ubuntu                            latest    b6548eacb063   Less than a second ago   77.8MB
registry                          latest    909c3ff012b7   Less than a second ago   25.4MB
192.168.224.128:5000/joshubuntu   1.0       9ff699250f65   9 minutes ago            112MB
joshubuntu                        1.0       9ff699250f65   9 minutes ago            112MB

配置docker允许接收http请求

修改/etc/docker/daemon.json,添加insecure-registries允许http:

tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://edfnnsgx.mirror.aliyuncs.com"],
  "insecure-registries": ["192.168.204.128:5000"]
}
EOF

然后重启docker:(新版本的docker会立即生效)

systemctl daemon-reload
systemctl restart docker

3.3 推送到私仓

  1. 添加一个对应私仓地址的tag
[root@localhost data]# docker start 9f5451a1f921
9f5451a1f921

推送到私服库:

[root@localhost data]# docker push 192.168.204.128:5000/joshubuntu:1.2
The push refers to repository [192.168.204.128:5000/joshubuntu]
e5bbc7abeac3: Pushed 
8ceb9643fb36: Pushed 
1.2: digest: sha256:53a79c7fab3f4b60b27e8192956f860ee16434932f0210f4eac6b8788ee1c66b size: 741

再次验证私服库:

curl http://IP:5000/v2/_catalog

输出:

{"repositories":["joshubuntu"]}

3.4 pull到本地并运行

  1. 复制一份到主机(可忽略此步骤)

将192.168.31.223:5000/joshubuntu复制一份到主机:

docker run -it 90c4fb740827 /bin/bash	
docker export d44bf231d4e2 >bak.tar	#将虚拟机打包到Linux

docker rmi -f 90c4fb740827		#删掉原有的包

[root@localhost data]# docker images
REPOSITORY                        TAG       IMAGE ID       CREATED                  SIZE
192.168.204.128:5000/joshubuntu   1.2       244f7de5628a   11 minutes ago           112MB
  1. pull到本地
curl -XGET http://ip:端口/v2/_catalog

docker pull 192.168.204.128:5000/joshubuntu:1.2

文章来源:https://blog.csdn.net/qq_39689711/article/details/135241499
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。