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 ?
-
1Don'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
2 Answers
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
- 2,801
- 2
- 24
- 37
- 133,941
- 8
- 173
- 238
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?
- 528
- 2
- 12
- 24
-
2The 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