Sometimes when building Docker images it's necessary to use apt-get
. For example, an image for running Magento might start something like this
FROM php:5.4-apache
RUN apt-get -qqy update \
&& apt-get -qqy install git \
libmcrypt-dev \
libpng12-dev \
libxml2-dev \
libxslt-dev \
&& docker-php-ext-install bcmath \
gd \
mcrypt \
mysql \
soap \
xsl \
zip
But now I have all the junk brought in by those apt-get
commands. Worse, I'm not even sure what I can afford to delete, because presumably the php libs are dynamically linked.
I'm thinking along lines such as
- Is there a way to statically link the php libs in
docker-php-ext-install
so I can nuke all theapt-get
stuff? - How can I remove the data left by
apt-get update
?
but those are really just X-Y questions. My actual question is just
How can I build smaller Docker images without entirely trading away the ease-of-use and maintainability of using apt-get
in the Dockerfile?
Where you looking for This approach? https://www.ianlewis.org/en/creating-smaller-docker-images
Specifically you uninstall unneeded packages in the same layer
You can get rid of some stuff some stuff by running
rm -rf /var/lib/apt/lists/*
e.g.Remember that you will need to run this in the same line as the
apt-get update
for it to be effective.