los.adrian
los.adrian

Reputation: 538

Why the virtual size still the same after delete a file from container with rm?

I would like to know more about Docker. I'm so beginner, very new with this and linux. My English is not perfect therefore I copied the Terminal output. I had an image with 516MB VIRTUAL SIZE. I started the bash in a container and i use wget to download some 26MB data into this. After the downloading I create a image from the container's changes with commit. After that the VIRTUAL SIZE was 542.5MB. That is correct. After the commit I started again the bash, and I deleted this file. When i committed the changes the image VIRTUAL SIZE was 542.8MB.

This size does not decreased, instead increased. Could you tell me the reason?

Local images:

[fedora_user@fedora-vm ~]$ docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
tester/mytestfedora   latest              c0ca83567bdd        4 seconds ago       516 MB
fedora                 latest              834629358fe2        3 months ago        241.3 MB

Using the smallest one and downloading a file:

[fedora_user@fedora-vm ~]$ docker run -i -t tester/mytestfedora /bin/bash
bash-4.3# su dockeres
[dockeres@05ef6e284e32 /]$ cd /home/dockeres/downloads/    
[dockeres@05ef6e284e32 downloads]$ wget https://dl.dropboxusercontent.com/u/827503/0_TEMP/Riverbed.zip
--2015-03-31 19:24:47--  https://dl.dropboxusercontent.com/u/827503/0_TEMP/Riverbed.zip
Resolving dl.dropboxusercontent.com (dl.dropboxusercontent.com)... 54.243.97.104, 54.243.80.193, 50.16.185.28, ...
Connecting to dl.dropboxusercontent.com (dl.dropboxusercontent.com)|54.243.97.104|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26177252 (25M) [application/zip]
Saving to: 'Riverbed.zip'
Riverbed.zip                      100%[==============================================================>]  24.96M  3.66MB/s   in 14s    
2015-03-31 19:25:03 (1.78 MB/s) - 'Riverbed.zip' saved [26177252/26177252]
[dockeres@05ef6e284e32 downloads]$ exit
bash-4.3# exit

List the active ones and create a new image from a container's changes:

[fedora_user@fedora-vm ~]$ docker ps -all
CONTAINER ID        IMAGE                         COMMAND             CREATED             STATUS                     PORTS               NAMES
05ef6e284e32        tester/mytestfedora:latest   "/bin/bash"         2 minutes ago       Exited (0) 6 seconds ago                       sick_einstein       
[fedora_user@fedora-vm ~]$ docker commit 05ef6e284e32 tester/mytestfedora
f122b12e94a32d477f2f2f18c5a5190a9ad5d349109933da65a0cfeff448c822
[fedora_user@fedora-vm ~]$ docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
tester/mytestfedora   latest              f122b12e94a3        5 seconds ago       542.5 MB
fedora                 latest              834629358fe2        3 months ago        241.3 MB

Access it again and remove the downloaded file:

[fedora_user@fedora-vm ~]$ docker run -i -t tester/mytestfedora /bin/bash
[dockeres@fb5ba36692f0 /]$ cd /home/dockeres/downloads/             
[dockeres@fb5ba36692f0 downloads]$ rm -f Riverbed.zip              
[dockeres@fb5ba36692f0 downloads]$ ls
[dockeres@fb5ba36692f0 downloads]$ exit
bash-4.3# exit

List the active ones again and create a new image from a container's changes:

[fedora_user@fedora-vm ~]$ docker ps -all
CONTAINER ID        IMAGE                         COMMAND             CREATED             STATUS                     PORTS               NAMES
fb5ba36692f0        tester/mytestfedora:latest   "/bin/bash"         51 seconds ago      Exited (0) 5 seconds ago                       goofy_yalow         
[fedora_user@fedora-vm ~]$ docker commit fb5ba36692f0 tester/mytestfedora
f744e248576d7fa434768a1e1d25625a9654020fe77e12306f304ff5d5ad3e3b
[fedora_user@fedora-vm ~]$ docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
tester/mytestfedora   latest              f744e248576d        3 seconds ago       542.8 MB
fedora                 latest              834629358fe2        3 months ago        241.3 MB

Thank you in advance for any help you can provide.

Ed

Upvotes: 9

Views: 4447

Answers (3)

mo kanglong
mo kanglong

Reputation: 11

Sorry, my English is not good, I used machine translation

If you need to remove a large amount of space in the container, then you can consider the following methods.

You can use docker export and docker import to get rid of the history layer of old images.

Suppose you now have a container B running in old image

A. and you have deleted some files in container
B. Now you need to export this container B to tar and import back a new image
C. docker export -o your.tar containerID
docker import your.tar new-image

You will notice that both the tar file and the new image C are much smaller than the old image.

Upvotes: 1

ISanych
ISanych

Reputation: 22680

Answer on comment, as it too big for comment.

Single command

FROM ubuntu

RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections \
 && apt-get -qq update \
 && apt-get -qq -y install wget unzip \
 && mkdir /usr/etc \
 && cd /usr/etc \
 && wget -nv -O /tmp/jboss.zip http://download.jboss.org/jbossas/7.1/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final.zip \
 && unzip /tmp/jboss.zip \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Smaller file:

$ docker build -t test1 .
$ docker images | grep test1
test1 356 MB

Separate commands:

FROM ubuntu

RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections && apt-get -qq update && apt-get -qq -y install wget unzip
RUN wget -nv -O /tmp/jboss.zip http://download.jboss.org/jbossas/7.1/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final.zip
RUN mkdir /usr/etc
RUN cd /usr/etc && unzip /tmp/jboss.zip
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Bigger image:

$ docker build -t test2 .
$ docker images | grep test2
test2 510 MB

Upvotes: 8

seanmcl
seanmcl

Reputation: 9946

Docker uses a union file system for its layers. Each RUN command creates a new layer, as do commits from running containers. An image consists of a particular layer and all its ancestor layers. This is the virtual size. Thus, the virtual size is monotonically increasing in the layers. Your wget created a new layer. Your rm created a new layer, even though the union of files (say, du -hs /) has a smaller size.

Upvotes: 11

Related Questions