前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >私有docker registry镜像的制作-busybox based

私有docker registry镜像的制作-busybox based

作者头像
qsjs
发布2020-06-09 09:08:12
7030
发布2020-06-09 09:08:12
举报

我们通过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

代码语言:javascript
复制
[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]
  1. 利用busybox 生成系统指令(本质上就是创建系统指令的软链接),命令如下:
代码语言:javascript
复制
[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
  1. 通过以上两步,关于busybox的工作就完成了;下面我们需要拿到registry这个服务的相关文件,并部署到/root/docker_study的相应目录下,这里借助official的registry image,找到相应的文件,并放到当前的/root/docker_study/对应目录下; 我们通过git hub上official的registry对应的dockerfile( 地址为: https://github.com/docker/distribution-library-image/blob/0b6ea3ba50b65563600a717f07db4cfa6f18f957/amd64/Dockerfile ),可以看到该dockerfile的内容如下:
代码语言:javascript
复制
# 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 两个文件到对应的目录下;操作如下:

代码语言:javascript
复制
[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]#
  1. 至此,registry运行条件应该已经具备了,这里我们还需要指定ENTRYPOINT指令的值;查看official的registry, 是通过一个叫做entrypoint.sh脚本来实现的,其内容如下:
代码语言:javascript
复制
#!/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 内容如下:

代码语言:javascript
复制
[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]# 
  1. 创建registry image。
代码语言:javascript
复制
#首先把/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]# 
  1. 创建image之后发现无法进入交互模式,提示找不到文件,这是因为前面使用for语句创建软链接的时候,指定的绝对路径是在当前的工作目录,而在生成image 之后,文件 /root/docker_study/busybox 不存在导致;这里为了方便,就创建相应目录来解决这个问题:
代码语言:javascript
复制
[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]# 
  1. 运行my_registry 容器,verify 容器可以正常启动;
代码语言:javascript
复制
[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就成功完成了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器镜像服务
容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档