我有一些基于ubuntu12.04图像的docker镜像。我想将其中一些迁移到基于ubuntu14.04。但是ubuntu14.04映像几乎比ubuntu12.04映像大120 MB。
任何人都知道为什么以及如何减少ubuntu14.04图像
正在运行docker images
myregistry/ubuntu 14.04 xxx xxx 225.4 MB
myregistry/ubuntu 12.04 xxx xxx 103.9 MB
答案 0 :(得分:3)
我看到的第一个问题是你没有告诉我们你是如何得到你所拥有的图像的。它们来自非root注册表(“myregistry”),您从复制/粘贴练习中省略了哈希。
当我下载这些图片时,这就是我所看到的:
WineSoaked$ docker pull ubuntu:12.04
Pulling repository ubuntu
b45a6cbea6d1: Download complete
511136ea3c5a: Download complete
0b825e3a3d82: Download complete
3327dfd9dc7d: Download complete
4e284c9e0a9e: Download complete
ae9570f7a92a: Download complete
acc75c8f0e1d: Download complete
WineSoaked$ docker pull ubuntu:14.04
Pulling repository ubuntu
6b4e8a7373fe: Download complete
511136ea3c5a: Download complete
b18d0a2076a1: Download complete
67b66f26d423: Download complete
25c4824a5268: Download complete
8b1c48305638: Download complete
c900195dcbf3: Download complete
WineSoaked$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 6b4e8a7373fe 6 days ago 194.9 MB
ubuntu 12.04 b45a6cbea6d1 6 days ago 112.3 MB
你会注意到每个拉动中的第二个哈希是相同的。这是构建它们的基本图像。其余的与用于构建它们的dockerfiles有关。我们去看看,好吗?
以下是12.04的Dockerfile:
FROM scratch
ADD precise-core-amd64.tar.gz /
# a few minor docker-specific tweaks
# see https://github.com/dotcloud/docker/blob/master/contrib/mkimage/debootstrap
RUN echo '#!/bin/sh' > /usr/sbin/policy-rc.d \
&& echo 'exit 101' >> /usr/sbin/policy-rc.d \
&& chmod +x /usr/sbin/policy-rc.d \
\
&& dpkg-divert --local --rename --add /sbin/initctl \
&& cp -a /usr/sbin/policy-rc.d /sbin/initctl \
&& sed -i 's/^exit.*/exit 0/' /sbin/initctl \
\
&& echo 'force-unsafe-io' > /etc/dpkg/dpkg.cfg.d/docker-apt-speedup \
\
&& echo 'DPkg::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' > /etc/apt/apt.conf.d/docker-clean \
&& echo 'APT::Update::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' >> /etc/apt/apt.conf.d/docker-clean \
&& echo 'Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache "";' >> /etc/apt/apt.conf.d/docker-clean \
\
&& echo 'Acquire::Languages "none";' > /etc/apt/apt.conf.d/docker-no-languages \
\
&& echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > /etc/apt/apt.conf.d/docker-gzip-indexes
# delete all the apt list files since they're big and get stale quickly
RUN rm -rf /var/lib/apt/lists/*
# this forces "apt-get update" in dependent images, which is also good
# enable the universe
RUN sed -i 's/^#\s*\(deb.*universe\)$/\1/g' /etc/apt/sources.list
# upgrade packages for now, since the tarballs aren't updated frequently enough
RUN apt-get update && apt-get dist-upgrade -y && rm -rf /var/lib/apt/lists/*
# overwrite this with 'CMD []' in a dependent Dockerfile
CMD ["/bin/bash"]
Dockerfile中的每个命令都对应于一个哈希值,这些哈希值是组成AUFS文件系统的中间图像。即:
FROM scratch
ADD precise-core-amd64.tar.gz
... RUN
RUN rm -rf /var/lib/apt/lists/*
RUN sed -i 's/^#\s*\(deb.*universe\)$/\1/g' /etc/apt/sources.list
RUN apt-get update && apt-get dist-upgrade -y && rm -rf /var/lib/apt/lists/*
同行
并且b45a6cbea6d1对应于生成的图像。您可以根据Dockerfile:
类似地分解14.04图像FROM scratch
ADD trusty-core-amd64.tar.gz /
# a few minor docker-specific tweaks
# see https://github.com/dotcloud/docker/blob/master/contrib/mkimage/debootstrap
RUN echo '#!/bin/sh' > /usr/sbin/policy-rc.d \
&& echo 'exit 101' >> /usr/sbin/policy-rc.d \
&& chmod +x /usr/sbin/policy-rc.d \
\
&& dpkg-divert --local --rename --add /sbin/initctl \
&& cp -a /usr/sbin/policy-rc.d /sbin/initctl \
&& sed -i 's/^exit.*/exit 0/' /sbin/initctl \
\
&& echo 'force-unsafe-io' > /etc/dpkg/dpkg.cfg.d/docker-apt-speedup \
\
&& echo 'DPkg::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' > /etc/apt/apt.conf.d/docker-clean \
&& echo 'APT::Update::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' >> /etc/apt/apt.conf.d/docker-clean \
&& echo 'Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache "";' >> /etc/apt/apt.conf.d/docker-clean \
\
&& echo 'Acquire::Languages "none";' > /etc/apt/apt.conf.d/docker-no-languages \
\
&& echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > /etc/apt/apt.conf.d/docker-gzip-indexes
# delete all the apt list files since they're big and get stale quickly
RUN rm -rf /var/lib/apt/lists/*
# this forces "apt-get update" in dependent images, which is also good
# enable the universe
RUN sed -i 's/^#\s*\(deb.*universe\)$/\1/g' /etc/apt/sources.list
# upgrade packages for now, since the tarballs aren't updated frequently enough
RUN apt-get update && apt-get dist-upgrade -y && rm -rf /var/lib/apt/lists/*
# overwrite this with 'CMD []' in a dependent Dockerfile
CMD ["/bin/bash"]
所以,看看Dockerfile,我认为14.04更大的主要原因是它们开始的基础tarball更大。 12.04的tarball约为39MB,而14.04 tarball约为66MB。你可以下载并用枪杀它们来获得它们的真实尺寸,但我认为这可以帮助你开始理解它。
可以使用docker history命令确认:
$ docker history ubuntu:12.04
IMAGE CREATED CREATED BY SIZE
b45a6cbea6d1 6 days ago /bin/sh -c #(nop) CMD [/bin/bash] 0 B
acc75c8f0e1d 6 days ago /bin/sh -c apt-get update && apt-get dist-upg 8.5 MB
ae9570f7a92a 6 days ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/ 1.911 kB
4e284c9e0a9e 6 days ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0 B
3327dfd9dc7d 6 days ago /bin/sh -c echo '#!/bin/sh' > /usr/sbin/polic 156.2 kB
0b825e3a3d82 6 days ago /bin/sh -c #(nop) ADD file:f66a301611d5f2b8e4 103.7 MB
511136ea3c5a 16 months ago 0 B
$ docker history ubuntu:14.04
IMAGE CREATED CREATED BY SIZE
6b4e8a7373fe 6 days ago /bin/sh -c #(nop) CMD [/bin/bash] 0 B
c900195dcbf3 6 days ago /bin/sh -c apt-get update && apt-get dist-upg 2.178 MB
8b1c48305638 6 days ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/ 1.895 kB
25c4824a5268 6 days ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0 B
67b66f26d423 6 days ago /bin/sh -c echo '#!/bin/sh' > /usr/sbin/polic 194.5 kB
b18d0a2076a1 6 days ago /bin/sh -c #(nop) ADD file:57743b1a50a2440168 192.6 MB
511136ea3c5a 16 months ago 0 B