从头基于空镜像scratch创建一个新的Docker镜像

我们在使用Dockerfile构建docker镜像时,一种方式是使用官方预先配置好的容器镜像。优点是我们不用从头开始构建,节省了很多工作量,但付出的代价是需要下载很大的镜像包。

比如我机器上docker images返回的这些基于nginx的镜像,每个都超过了100MB,而一个简单的Ubuntu的容器超过了200MB,如果安装了相关的软件,尺寸会更大。

webp

如果我们的需求是在构建一个符合我们实际业务需求的Docker镜像的前提下,确保镜像尺寸尽可能的小,应该怎么做呢?

思路是使用空镜像scratch。

webp

新建一个文件夹,用wget下载rootfs.tar.xz压缩包。

wget -O rootfs.tar.xz https://github.com/debuerreotype/docker-debian-artifacts/raw/b024a792c752a5c6ccc422152ab0fd7197ae8860/jessie/rootfs.tar.xz

webp

这个将近30MB的压缩包是个什么东东?

解压之后看内容就知道了,包含了操作系统大部分常用命令。


webp

wget -O nginx.conf https://github.wdf.sap.corp/raw/slvi/docker-k8s-training/master/docker/res/nginx.conf

新建一个dockerfile文件,将下列内容粘贴进去:

FROM scratch# set the environment to honour SAP's proxy serversENV http_proxy http://sap.corp:8080
ENV https_proxy http://sap.corp:8080
ENV no_proxy .sap.corp# give yourself some creditLABEL maintainer="Jerry Wang"# add and unpack an archive that contains a Debian root filesystemADD rootfs.tar.xz /# use the apt-get package manager to install nginx and wgetRUN apt-get update && \
apt-get -y install nginx wget# use wget to download a custom website into the imageRUN wget --no-check-certificate -O /usr/share/nginx/html/cheers.jpg https://github.wdf.sap.corp/raw/slvi/docker-k8s-training/master/docker/res/cheers.jpg && \
wget --no-check-certificate -O /usr/share/nginx/html/index.html https://github.wdf.sap.corp/raw/slvi/docker-k8s-training/master/docker/res/cheers.html# copy the custom nginx configuration into the imageCOPY nginx.conf /etc/nginx/nginx.conf# link nginx log files to Docker log collection facilityRUN ln -sf /dev/stdout /var/log/nginx/access.log && \
ln -sf /dev/stderr /var/log/nginx/error.log# expose port 80 - the standard port for webserversEXPOSE 80# and make sure that nginx runs when a container is createdCMD ["nginx", "-g", "daemon off;"]

执行命令进行镜像的构建:

docker build -t nginx-from-scratch1.0 .

webp

产生的日志:


webp

最后看到镜像成功构建的消息。


webp

基于这个名为nginx-from-scratch的镜像启动一个新的nginx容器:


webp

localhost:1083, 看到首页,说明这个新构建的镜像工作正常。


webp


要获取更多Jerry的原创文章,请关注公众号"汪子熙":

webp


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24475491/viewspace-2221882/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24475491/viewspace-2221882/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值