2

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
  • 307
  • 1
  • 4
  • 14

1 Answers1

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
  • 3,807
  • 3
  • 33
  • 50
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265