Docker image size is becoming huge after DOCKER COMMIT

I’m started using a docker with basic docker image Linux - x86-64 ( latest ). I have pulled this image with the command

docker pull ubuntu

I run this docker using the command

docker run -it ea4c82dcd15a /bin/bash

where “ea4c82dcd15a” is the IMAGE ID for the image.
This allows me to run the image successfully and work with it. But after doing some work in the container and storing some of my small work data into the container, committing the docker makes the docker image size too huge as it sometimes crosses the size in GBs.
I use below command to commit the docker

docker commit -p -a <author> -m <message> <container_id> <label>:<tag>

But as I told you this is creating a docker image including my work data but with a large size of the image.
Can anyone please clear if I’m doing anything wrong in the whole process or there is any other reason for this is happening?
Please help guys!!
Thanks in Advance…

Its hard to do without knowing what you’re doing in the container.

Maybe look into dockerfiles and create a “recipe” for your image, instead of doing commands IN a container for then to create a image of that.

I have tried running one simple flask service in the container of which workspace size is 8MB. How can this service increase the size of image?

but what did you install to run that flask service?
did you use apt-get update? on my debian, my apt cache is 400mb in size.

Yes I did.
I used some commands like:

apt-get update
apt-get upgrade
apt install python
pip install flask

Is that is the reason for getting the increased size of the docker image?
And if yes, then what should I do to control this. Because however, I need to use the above commands as those are must to run such workspaces in the docker container. I also have created one virtual environment in the docker.
And if yes, the installation of packages is making the big size image, then please help me out to do it properly without getting these type of issues.
Thanks for your reply and Support.

try and run a “apt-get clean” and see how much that helps

No, Nothing happened… :slightly_frowning_face:
It still very high sized… :cold_sweat:

I ran all the commands you mentioned, except for installing flask, as pip install was causing issues.

What i ran:

apt-get update
apt-get upgrade
apt install python
sudo apt-get install python-pip python-dev build-essential
sudo pip install --upgrade pip
sudo pip install --upgrade virtualenv

After committing, the image is around 500mb

Thanks for the response.
Means using apt for installing/updating packages causes the issue?
My docker image became 4GB in size, I’m not even storing much data into it. Should I stop using docker for the work then? or do we have any other solution to solve this?
Because without apt I cannot work in the docker. Can we have any other way to install such packages that will not cause the issue?

If i run

FROM python:2
RUN pip install flask

its 900mb.

I have no idea on how you can get it to 4gb :slight_smile:

I kept the docker container continuously running for many days

is it then maybe log files ?

Where and how to get that log file then?

Its pretty hard for me to tell you, since i dont know much about flask/your application.
But it was only a guess, maybe try and “cd /” and run “du -h --max-depth=1” and find out what it is that uses space

In General it is better to just define what you want to accomplish within a dockerfile.

When a container starts, all the differences to the image it’s running from will be part of the container runtime and will be included when you do a docker commit.

The increased size can have multiple reasons and is not limited to your flask application.

You can easily check the difference and probably the reason for the inflated image size with the docker diff {container_name}, which shows you exactly the differences.

HI, I also have this issue… I spin up some postgres image, and add some schema to it then save the images using docker commit command.

And surprised with how the images turn to 23GB… when I tried to inspect inside the image using du -chd 1 command… it seems the total only consist of 4.2GB.

root@17597b554824:/# du -chd 1 | sort -h
0       ./proc
0       ./sys
4.0K    ./boot
4.0K    ./docker-entrypoint-initdb.d
4.0K    ./home
4.0K    ./lib64
4.0K    ./media
4.0K    ./mnt
4.0K    ./opt
4.0K    ./srv
8.0K    ./tmp
16K     ./dev
20K     ./run
24K     ./root
936K    ./etc
4.1M    ./sbin
4.8M    ./bin
8.4M    ./var
13M     ./lib
285M    ./usr
3.8G    ./data
4.2G    .
4.2G    total
root@17597b554824:/#

and here’s the image

ggalihpp@LAPTOP-AJEE6E58:~$ docker images
REPOSITORY                                   TAG          IMAGE ID       CREATED          SIZE
sysrest-db                                   ggalihpp     70df3adbdeef   32 minutes ago   23.5GB```

The image size is the sum of the size of the layers. When you change files which are on one of the read-only image layers then you actually duplicate the file, save it on the writable container filesystem and change that. It depends on what storage driver you use but this is what usually happens. This is one possible reason I can imagine why the image size is much larger than the container’s filesystem but it could be something else. We cannot say without knowing what you exactly did and based on what image.

An other possible reason is you run a build process inside the container and saved large amount of data under /tmp. If something deletes files from /tmp when the container starts you won’t see that. You could also have /tmp on tmpfs so your container starts with an empty /tmp in memory hiding the data that was there in the image.