我们通过hub.docker网站可以发现official的registry基于alphine base image, 其实我们也可以基于busybox image来创建registry, 本文带你一步步基于busybox 来构建自己的"registry" docker image,下面跟我来实现这个基于busybox的“registry" image. 以下是制作过程:
下载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]
[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
# Build a minimal distribution container
FROM alpine:3.8
RUN set -ex \
&& apk add --no-cache ca-certificates apache2-utils
COPY ./registry /bin/registry
COPY ./config-example.yml /etc/docker/registry/config.yml
VOLUME ["/var/lib/registry"]
EXPOSE 5000
COPY docker-entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["/etc/docker/registry/config.yml"]
从上面的dockerfile文件中,我们发现official的registry的dockerfile包含如下指令: A. 利用alphine:3.8 image作为baseimage, B. copyregistry 文件到/bin/目录下 C. copy config.yml 文件到/etc/docker/registry/目录下; D. 指定默认的volume: /var/lib/registry, E. 默认expose的 端口5000. 所以我们需要copy 两个文件到对应的目录下;操作如下:
[root@localhost docker_study]# docker run -it -v /root/docker_study/etc:/mnt/ etc -v /root/docker_study/bin:/mnt/bin registry /bin/sh
/ # mkdir -p /mnt/etc/docker/registry/
/ # cp -avr /etc/docker/registry/config.yml /mnt/etc/docker/registry/ #copy registry的配置文件
'/etc/docker/registry/config.yml' -> '/mnt/etc/docker/registry/config.yml'
/ # cp -avr /bin/registry /mnt/bin/ #copy registry的二进制文件
'/bin/registry' -> '/mnt/bin/registry'
/ # exit
[root@localhost docker_study]# ls
bin busybox etc linuxrc sbin usr
[root@localhost docker_study]#
#!/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 linuxrc my_entry.sh sbin usr
[root@localhost docker_study]#
#首先把/root/docker_study/目录下的所有内容打包, 这个目录下的内容对应image的根目录下的内容;
[root@localhost docker_study]#tar -czvf registry_Busybox-Based.tar.gz *
[root@localhost docker_study]# ls
bin busybox etc 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/lib/registry"]
[root@localhost docker_study]#
[root@localhost docker_study]# mkdir -p root/docker_study/
[root@localhost docker_study]# cp busybox root/docker_study/
#重新进行docker build.
[root@localhost docker_study]# ls
bin linuxrc registry_Busybox-Based.tar.gz sbin
busybox etc my_entry.sh root usr
[root@localhost docker_study]#
[root@localhost docker_study]# tar -czvf registry_Busybox-Based.tar.gz * #首先打包
#确保dockerfile是存在的,然后执行下面的docker build 命令;
[root@localhost docker_study]# docker build -t my_registry:latest .
[root@localhost docker_study]# docker history my_registry
IMAGE CREATED CREATED BY SIZE COMMENT
9ed6394b8c47 12 seconds ago /bin/sh -c #(nop) VOLUME [/var/lib/registry] 0 B
344ba05e52d6 12 seconds ago /bin/sh -c #(nop) EXPOSE 5000/tcp 0 B
3fa5fc341acd 12 seconds ago /bin/sh -c #(nop) ENTRYPOINT ["/my_entry.... 0 B
46cdfc9940c7 13 seconds ago /bin/sh -c #(nop) CMD ["/etc/docker/regis... 0 B
9c2c05e9dcb0 13 seconds ago /bin/sh -c #(nop) ENV PATH=/bin:/sbin:/us... 0 B
bb252bad399d 13 seconds ago /bin/sh -c #(nop) ADD file:3016f327a405ee7... 22.1 MB
b28c46c30840 2 weeks ago /bin/sh -c #(nop) MAINTAINER PandaEye 0 B
[root@localhost docker_study]#
[root@localhost docker_study]# docker run -d my_registry
8445804a09e1635e9a1e18abcd9d0b793c035a931d8add52ce2c92000492ee4d
[root@localhost docker_study]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8445804a09e1 my_registry "/my_entry.sh /etc..." 42 seconds ago Up 41 seconds 5000/tcp kind_visvesvaraya
[root@localhost docker_study]#
到此,我们这个基于busybox的registry就成功完成了。