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 ?
Asked
Active
Viewed 1.2k times
2
-
2Don't use `CentOS` use `alpine` instead. Start from something which is already small instead of trying to make something small – Tarun Lalwani Sep 27 '17 at 17:16
-
thanks for your answer but the package i need to install needs yum to be installed so alpine is not the solution for me ... – Joey Pablo Sep 28 '17 at 09:07
-
Then look at the answer I posted – Tarun Lalwani Sep 28 '17 at 09:11
-
I'm testing it :) – Joey Pablo Sep 28 '17 at 09:24
-
I have this error: Error response from daemon: minimal is not a valid change command – Joey Pablo Sep 28 '17 at 09:29
-
Chuck that `-c ....`, updated the answer. That is not needed – Tarun Lalwani Sep 28 '17 at 09:32
1 Answers
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