docker registry搭建私有dockerhub把本地镜像推送到私有库
docker search 查找镜像我们要用的regisotry
[root@WIND ~]#docker search registry
[root@WIND ~]# docker pull registry
拉好registry之后,创建一个docker registry仓库,这个仓库就是我的私有库,后面我们要存放私有镜像的
[root@WIND ~]# docker run -d -p 5000:5000 -v /root/my_docker_registry:/tm/registry --privileged=true registry
c2665043750a32a4f0b4ad4d3438d6faf95a178cf881bf307480fc74d0d7225f
[root@WIND ~]#
私有仓库容器启动之后,查看一下
[root@WIND ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2665043750a registry "/entrypoint.sh /etc…" 4 seconds ago Up 3 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp nifty_wright
41cfd91ae7d7 b647c0530a2d "/bin/bash" 4 hours ago Up 4 hours gifted_haibt
7acb7ba6797b b647c0530a2d "/bin/bash" 23 hours ago Up 4 hours awesome_wozniak
[root@WIND ~]#
确认一下端口是不是我们设置的
[root@WIND ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 16013/docker-proxy
tcp6 0 0 :::5000 :::* LISTEN 16019/docker-proxy
登陆我们的ubuntu容器服务,安装ifconfig命令,默认是没有这个命令的
等会我们要把这个安装好ifconfig命令的ubuntu,打包成一个新的镜像,然后推送到我们的私有镜像仓库
从私有镜像仓库拉下来运行
[root@WIND ~]# docker exec -it 41cfd91ae7d7 bash
root@41cfd91ae7d7:/#
root@41cfd91ae7d7:/# ifconfig
bash: ifconfig: command not found
root@41cfd91ae7d7:/#
明显提示command not found
apt-get install 命令安装(如果是centos 使用yum安装)
root@41cfd91ae7d7:/# apt-get -y install net-tools
Reading package lists... Done
Building dependency tree
root@41cfd91ae7d7:/#
root@41cfd91ae7d7:/# 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 2860 bytes 7352987 (7.3 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1661 bytes 125173 (125.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig 命令安装好之后,提交带ifconfig命令的镜像,使之成为一个新的镜像,这个新的镜像带了ifconfig命令
[root@WIND ~]# docker commit -m 'add ifconfig command' -a 'WIND' 41cfd91ae7d7 ubuntu_ifconfig:1.1.1
sha256:533caa6208fa3745cc322681dabaf1e8e80a50192d3f26210f12ce31610f5fa8
[root@WIND ~]#
这里看我们的id, 是7d7结尾的,就是刚才我们登录安装ifconfig命令那个
查看一下我们的新镜像,名字是ubuntu_ifconfig, 版本是1.1.1
[root@WIND ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu_ifconfig 1.1.1 533caa6208fa 6 seconds ago 191MB
unbuntu_with_vim WIND c56e4c03ee3a 24 hours ago 179MB
docker run 命令,用我们提交的镜像,运行一个容器服务
[root@WIND ~]# docker run -it -d ubuntu_ifconfig:1.1.1
18c09ab98b5e5dfcd52cbe787a001e209cd26a8638eec8d31f27618e97bdf6e3
[root@WIND ~]#
-d 是在后台运行
查看我们跑起来的容器服务
[root@WIND ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18c09ab98b5e ubuntu_ifconfig:1.1.1 "/bin/bash" 3 seconds ago Up 2 seconds determined_kowalevski
c2665043750a registry "/entrypoint.sh /etc…" 29 minutes ago Up 29 minutes 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp nifty_wright
41cfd91ae7d7 b647c0530a2d "/bin/bash" 4 hours ago Up 4 hours gifted_haibt
7acb7ba6797b b647c0530a2d "/bin/bash" 24 hours ago Up 4 hours awesome_wozniak
登录到容器内部,查看是否有ifconfig命令
[root@WIND ~]# docker exec -it 18c09ab98b5e bash
root@18c09ab98b5e:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.5 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:05 txqueuelen 0 (Ethernet)
RX packets 13 bytes 1108 (1.1 KB)
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
在容器里面能看到IP,说明新的镜像已经有这个命令了
用命令看一下我们的docker registry ,啥都没(curl 返回repositories后面是空的)
[root@WIND ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18c09ab98b5e ubuntu_ifconfig:1.1.1 "/bin/bash" 3 minutes ago Up 3 minutes determined_kowalevski
c2665043750a registry "/entrypoint.sh /etc…" 32 minutes ago Up 32 minutes 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp nifty_wright
[root@WIND ~]# curl -XGET http://localhost:5000/v2/_catalog
{"repositories":[]}
localhost是我们当前操作的主机,也可以换成实际的主机IP,到时候把仓库IP地址放出去,这个仓库的镜像就可以正常被别人拉取和使用了
我们这里是自己测试,用localhost。
给刚才安装好ifconfig命令的镜像打个标签
[root@WIND ~]#
[root@WIND ~]# docker tag ubuntu_ifconfig:1.1.1 localhost:5000/ubuntu_ifconfig:1.1.1
[root@WIND ~]#
查看一下打好标签的镜像,可以看到IMAGE ID是一样的,但是REPOSITORY那一列不一样
[root@WIND ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu_ifconfig 1.1.1 533caa6208fa 13 minutes ago 191MB
localhost:5000/ubuntu_ifconfig 1.1.1 533caa6208fa 13 minutes ago 191MB
imp_ubuntu_with_vim u009_latest b647c0530a2d 24 hours ago 178MB
docker默认不允许http推送,我们先修改一下配置,取消这个限制,让他允许http推送
[root@WIND ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://xxxhu.mirror.aliyuncs.com"],
"insecure- registry": ["http://localhost:5000"]
}
[root@WIND ~]#
重启一下docker守护进程,使刚才的配置生效(据说新版的docker不用重启配置也能生效)
[root@WIND ~]# systemctl start docker
[root@WIND ~]#
[root@WIND ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-02-28 22:23:32 CST; 10s ago
Docs: https://docs.docker.com
Main PID: 17215 (dockerd)
Tasks: 8
Memory: 36.6M
CGroup: /system.slice/docker.service
└─17215 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
启动我们刚才的容器服务
[root@WIND ~]# docker start c2665043750a
c2665043750a
[root@WIND ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2665043750a registry "/entrypoint.sh /etc…" 45 minutes ago Up 4 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp nifty_wright
[root@WIND ~]#
还是5000的端口,没有问题
docker push命令,把镜像推送到我们的私服版本库
[root@WIND ~]# docker push localhost:5000/ubuntu_ifconfig:1.1.1
The push refers to repository [localhost:5000/ubuntu_ifconfig]
c14675006a68: Pushed
aeee91d46fdd: Pushed
1.1.1: digest: sha256:10b4dde74f5dc6853941866eed6e4a800d874a72d024dcea7a04c91def17bdb2 size: 740
[root@WIND ~]#
验证私服库的镜像
[root@WIND ~]# curl -XGET http://10.13.53.194:5000/v2/_catalog
{"repositories":["ubuntu_ifconfig"]}
[root@WIND ~]#
前面repositories是空的,现在有了内容了
删除我们本地的镜像,等会从我们的私有库重新拉下来使用
[root@WIND ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu_ifconfig 1.1.1 533caa6208fa 24 minutes ago 191MB
localhost:5000/ubuntu_ifconfig 1.1.1 533caa6208fa 24 minutes ago 191MB
[root@WIND ~]# docker rmi ubuntu_ifconfig:1.1.1
Untagged: ubuntu_ifconfig:1.1.1
[root@WIND ~]#
[root@WIND ~]# docker rmi -f localhost:5000/ubuntu_ifconfig:1.1.1
Untagged: localhost:5000/ubuntu_ifconfig:1.1.1
Untagged: localhost:5000/ubuntu_ifconfig@sha256:10b4dde74f5dc6853941866eed6e4a800d874a72d024dcea7a04c91def17bdb2
Deleted: sha256:533caa6208fa3745cc322681dabaf1e8e80a50192d3f26210f12ce31610f5fa8
docker pull从私有仓库拉取镜像
[root@WIND ~]# docker pull localhost:5000/ubuntu_ifconfig:1.1.1
1.1.1: Pulling from ubuntu_ifconfig
cbfb82870d5c: Already exists
1ec171b79f30: Already exists
Digest: sha256:10b4dde74f5dc6853941866eed6e4a800d874a72d024dcea7a04c91def17bdb2
Status: Downloaded newer image for localhost:5000/ubuntu_ifconfig:1.1.1
localhost:5000/ubuntu_ifconfig:1.1.1
[root@WIND ~]#
dokcer pull要带版本号,如果不记得了,用下面这个命令查一下
格式:
curl -XGET http://仓库地址/v2/镜像名称/tags/list
[root@WIND ~]# curl -XGET http://localhost:5000/v2/ubuntu_ifconfig/tags/list
{"name":"ubuntu_ifconfig","tags":["1.1.1"]}
[root@WIND ~]#
查看我们拉取的镜像,前面已经用docker rmi 删掉本地镜像的了,现在又有localhost:5000/ubuntu_ifconfig这个了,版本是1.1.1
[root@WIND ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost:5000/ubuntu_ifconfig 1.1.1 533caa6208fa 30 minutes ago 191MB
imp_ubuntu_with_vim u009_latest b647c0530a2d 24 hours ago 178MB
docker run 启动一个镜像服务,验证拉下来的镜像能否使用
[root@WIND ~]# docker run -it localhost:5000/ubuntu_ifconfig:1.1.1 /bin/bash
root@c18fda6b16bb:/#
root@c18fda6b16bb:/# 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 710 (710.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
实验结束。