Friday, November 03, 2017

Oracle linux - building small Docker containers while using yum

Docker and more precise Docker containers are in general small. The rule for Docker containers is, keep them as small as possible. Building containers is a process where you have to look every step of the way on what you really need and what you can remove or clean. When building a container most people will depend on a mainstream base image, in our case we base most of our containers on the Oracle Linux 7 slim image which is the smallest Docker base image build upon Oracle Linux 7.

One of the rules when you build your own containers on top of the Oracle Linux 7 slim base image (rule applies for all others as well) is that you have to clean after you build. In one example we install a lot of things using yum which are needed as part of the build process. Rule is that you have to clean after you are done to ensure the resulting container is small and is not carrying unneeded things.

in our example we have totally in the beginning a run command, as shown below, that will install a number of packages during the build phase:

# Install dependencies
RUN yum -y install make gcc cc tar gzip

Right after this we initiate a number of compile and build steps. However, after those are done we can remove the installed dependencies again.

# Cleanup dependencies
RUN yum -y erase gcc cc tar gzip

Even though this is good housekeeping it is not enough to ensure your image is small. Yum will keep some files in cache which you also have to ensure are cleaned. If this is not done you will have a lot of unneeded things in your local yum cache which will make your docker image for a container much bigger than needed.

To clean the cache you will also have to ensure you have the following run command in your Dockerfile;

# clean the YUM cache.
RUN yum clean all

Making sure you remove the unneeded dependencies you installed at the end of your Dockerfile and also ensure that you can clean your yum cache will make your image shrink and will ensure you have small Docker images and Docker containers with Oracle Linux 7 in your environment. 

No comments: