1

I'm trying to reduce the size of my docker image which is using Centos 7.2 The issue is that it's 257MB which is too high... I have followed the best practices to write Dockerfile in order to reduce the size... Is there a way to modify the image after the build and rebuild that image to see the size reduced ?

Joey Pablo
  • 297
  • 4
  • 14

2 Answers2

3

First of all if you want to reduce an OS size, don't start with big one like CentOS, you can start with alpine which is small

Now if you are still keen on using CentOS, do the following:

docker run -d --name centos_minimal centos:7.2.1511 tail -f /dev/null

This will start a command in the background. You can then get into the container using

docker exec -it centos_minimal bash

Now start removing packages that you don't need using yum remove or yum purge. Once you are done you can commit the image

docker commit centos_minimal centos_minimal:7.2.1511_trial1

Experimental Squash Image

Another option is to use an experimental feature of the build command. In this you can have a dockerfile like below

FROM centos:7
RUN yum -y purge package1 package2 package2

Then build this file using

docker build --squash -t centos_minimal:squash .

For this you need to add "experimental": true to your /etc/docker/daemon.json and then restart the docker server

Josh Correia
  • 2,801
  • 2
  • 24
  • 37
Tarun Lalwani
  • 133,941
  • 8
  • 173
  • 238
-2

It is possible, but not at all elegant. Just like you can add software to the base image, you could also remove:

FROM centos:7

RUN yum -y update && yum clean all

RUN yum -y install new_software

RUN yum -y remove obsolete_software

Ask yourself: does your OS have to be CentOS? Then I would recommend you use the default installation and make sure your have enough disk space and memory.

If it does not need to be CentOS, you should rather start with a more minimalistic image. See the discussion here: Which Docker base image should be used to install Apps in a container without any additional OS?

Ingo Steinke
  • 528
  • 2
  • 12
  • 24
  • 2
    The answer is correct but not in the context. The question is about "rebuild that image to see the size reduced?" What you are doing is just adding another layer which removes the packages but the lower layers still have those packages and hence the size won't have any difference, rather it would be increased a bit by your layer as well because it would modify the package database also – Tarun Lalwani Sep 27 '17 at 17:19