本想基于busybox + source code of registry 来构建自己的"registry" docker image, 但是苦于找不到“registry" source code,所以从官方的 registry image里面把registry相关的文件提取出来,结合busybox来实现一个山寨版的“registry" image.
以下是整个“山寨”的过程:
- 既然这里是折腾,就不用official 的busybox docker image了,而是用busybox这个工具来自己构建:
下载busybox 的二进制文件,这里使用1.28.1的版本的busybox,URL为:
https://busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64
[root@localhost ~]# wget https://busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64
--2019-09-21 22:57:09-- https://busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64
Resolving busybox.net (busybox.net)... 140.211.167.122
Connecting to busybox.net (busybox.net)|140.211.167.122|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1001112 (978K)
Saving to: ‘busybox-x86_64’
100%[======================================>] 1,001,112 660KB/s in 1.5s
2019-09-21 22:57:12 (660 KB/s) - ‘busybox-x86_64’ saved [1001112/1001112]
- 利用busybox 生成系统指令(本质上就是创建系统指令的软链接),命令如下:
[root@localhost docker_study]# ls
busybox
#首先创建相应的目录;
[root@localhost docker_study]# for i in `./busybox --list-full`;do mkdir -p `dirname $i`;done
#查看已经创建的目录
[root@localhost docker_study]# find . -type d
.
./usr
./usr/bin
./usr/sbin
./sbin
./bin
#创建系统命令的软链接;
[root@localhost docker_study]# for i in `./busybox --list-full`;do ln -s /root/docker_study/busybox $i;done
- 通过以上两步,关于busybox的工作就完成了;下面从official的registry image中,把registry相关文件弄出来,放到当前的/root/docker_study/对应目录下;
#通过docker pull registry 下载official registry, 这里忽略过程;
[root@localhost docker_study]# docker images | grep registry
docker.io/registry latest f32a97de94e1 6 months ago 25.8 MB
[root@localhost docker_study]#
#交互模式运行registry 容器,看看里面的文件结构;
[root@localhost docker_study]# docker run -it registry /bin/sh
/ # du -hsx /* | grep -Evi ^0
19.9M /bin
4.0K /entrypoint.sh
792.0K /etc
2.7M /lib
4.0K /root
224.0K /sbin
1.6M /usr
/ # which registry
/bin/registry
/ #
#用到的目录会有: /etc, /lib, /usr 以及 /bin/registry这个命令;
/ # exit
#创建目录,并mount到registry 容器里面
[root@localhost docker_study]# mkdir -p lib etc usr
[root@localhost docker_study]# ls
bin busybox etc lib linuxrc sbin usr
[root@localhost docker_study]# docker run -it -v /root/docker_study/etc:/mnt/etc -v /root/docker_study/lib:/mnt/lib -v /root/docker_study/usr:/mnt/usr -v /root/docker_study/bin:/mnt/bin registry /bin/sh
#copy 相应数据到对应的目录下,然后退出registry 容器;
/ # cd /mnt
/mnt # cp -ar /etc/* etc/
/mnt # cp -ar /lib/* lib/
/mnt # cp -ar /usr/lib usr/
/mnt # cp -ar /usr/local usr/
/mnt # cp -ar /usr/share usr/
/mnt # cp -a /bin/registry bin/
/mnt # du -hsx *
19.2M bin
808.0K etc
2.7M lib
1.1M usr
/mnt # exit
[root@localhost docker_study]# du -hsx *
20M bin
980K busybox
808K etc
2.8M lib
0 linuxrc
4.0K sbin
1.2M usr
- 至此,这个山寨版的registry运行条件应该已经具备了,模仿 official的registry, 我这里还需要一个ENTRYPOINT的运行脚本;查看原始的entrypoint.sh脚本内容如下:
#!/bin/sh
set -e
case "$1" in
*.yaml|*.yml) set -- registry serve "$@" ;;
serve|garbage-collect|help|-*) set -- registry "$@" ;;
esac
exec "$@"
在这里不做太多的修改,仅仅加上一个输出提示,以表明该image并不是official image. 修改后的my_entry.sh 内容如下:
[root@localhost docker_study]# cat my_entry.sh
#!/bin/sh
echo "This image not official image, it just created for study purpose only. Suggest to use the official image."
set -e
case "$1" in
*.yaml|*.yml) set -- registry serve "$@" ;;
serve|garbage-collect|help|-*) set -- registry "$@" ;;
esac
exec "$@"
[root@localhost docker_study]# ls
bin busybox etc lib linuxrc my_entry.sh sbin usr
[root@localhost docker_study]#
- 编写dockerfile。
#首先把/root/docker_study/目录下的所有内容打包, 这个目录下的内容对应image的根目录下的内容;
[root@localhost docker_study]#tar -czvf registry_Busybox-Based.tar.gz *
[root@localhost docker_study]# ls
bin busybox etc lib linuxrc my_entry.sh registry_Busybox-Based.tar.gz sbin usr
[root@localhost docker_study]#
#编写dockerfile, 内容如下:
[root@localhost docker_study]# cat dockerfile
FROM scratch
MAINTAINER PandaEye
ADD ./registry_Busybox-Based.tar.gz /
ENV PATH "/bin:/sbin:/usr/bin:/usr/sbin"
CMD ["/etc/docker/registry/config.yml"]
ENTRYPOINT ["/my_entry.sh"]
EXPOSE 5000
VOLUME ["/var/my_registry_of_docker/"]
[root@localhost docker_study]#
- 创建image之后发现无法进入交互模式,提示找不到文件,这是因为前面使用for语句创建软链接的时候,指定的绝对路径是在当前的工作目录,而在生成image 之后,没有文件: /root/docker_study/busybox 的存在导致;这里为了方便,就创建相应目录来解决这个问题:
[root@localhost docker_study]# mkdir -p root/docker_study/
[root@localhost docker_study]# ls
[root@localhost docker_study]# cp busybox root/docker_study/
#重新进行docker build.
[root@localhost docker_study]# ls
bin busybox etc lib linuxrc my_entry.sh root sbin usr
[root@localhost docker_study]# tar -czvf registry_Busybox-Based.tar.gz * #首先打包
[root@localhost docker_study]# ls #确保dockerfile文件存在;
bin busybox dockerfile etc lib linuxrc my_entry.sh registry_Busybox-Based.tar.gz root sbin usr
[root@localhost docker_study]#
[root@localhost docker_study]# docker build -t my_registry:latest .
[root@localhost docker_study]# docker history my_registry
IMAGE CREATED CREATED BY SIZE COMMENT
7e2e4bacdba2 13 minutes ago /bin/sh -c #(nop) VOLUME [[/var/my_regist... 0 B
ca1a6d91d22e 13 minutes ago /bin/sh -c #(nop) EXPOSE 5000/tcp 0 B
8d3b2436a2bf 13 minutes ago /bin/sh -c #(nop) ENTRYPOINT ["/my_entry.... 0 B
d1df6093e6e1 13 minutes ago /bin/sh -c #(nop) CMD ["/etc/docker/regis... 0 B
390cd88b4b7e 13 minutes ago /bin/sh -c #(nop) ENV PATH=/bin:/sbin:/us... 0 B
67e144ff97d2 13 minutes ago /bin/sh -c #(nop) ADD file:431579de83e57dc... 26.3 MB
fa295bddff92 13 minutes ago /bin/sh -c #(nop) MAINTAINER PandaEye 0 B
[root@localhost docker_study]#
- 运行my_registry 容器,verify 容器可以正常启动;
[root@localhost docker_study]# docker run -d my_registry
538c1e1ed07d777c1f9f7e48d319a765a863b308812aed4a7f7b63cb88ec5d70
[root@localhost docker_study]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
538c1e1ed07d my_registry "/my_entry.sh /etc..." 3 seconds ago Up 3 seconds 5000/tcp agitated_kare
36b1a8768da1 my_registry "/my_entry.sh /bin/sh" 15 minutes ago Exited (0) 15 minutes ago laughing_beaver
到此,我们这个“山寨”版的registry就成功“山寨”完成了。
本文原创,转载请著名出处;
网友评论