title: How to build a large docker image with relatively small disk space consumption?
body: <p><strong>Background</strong></p>
  
  <p>I am trying to build a docker image from Dockerfile on a VM. VM is running Redhat 7.1 (kernel 3.10) and Docker is 1.10.2</p>
  
  <p>The Dockerfile has following content</p>
  
  <pre><code>FROM rhel
  MAINTAINER MyName&lt;me@email.com&gt;
  RUN #yum install wget and other tools (less than 500 MB)
  COPY entitlementfile /opt/entitlementfile
  RUN  wget -O /opt/installer.bin https://installer.com/installer.bin \ 
      &amp;&amp; chmod +x /opt/* \
      &amp;&amp; /opt/installer.bin --quiet \
      &amp;&amp; rm -f /opt/*.bin
  USER admin
  </code></pre>
  
  <p>My build VM has around <strong>16G</strong> space available</p>
  
  <pre><code>[root@xrh701 DockerImage]# df -h
  Filesystem                                        Size  Used Avail Use% Mounted on
  /dev/mapper/rhel-root                              18G  2.1G   16G  12% /
  ...
  </code></pre>
  
  <p>The installer is around <strong>3G</strong> and the installed package is around <strong>8G</strong>. This adds up to 11G maximum, slightly exceeds docker defaults base device size which is 10G.</p>
  
  <p>So I manually bring up docker daemon with a larger dm.size (<strong>15G</strong>) to work around this issue.</p>
  
  <pre><code>docker daemon --storage-opt dm.basesize=15G
  </code></pre>
  
  <p>Since docker is based on Union FS, the images are layered on top of each other.
  So my understanding is that </p>
  
  <p>(1) the maximum size that my image could get is 11G (Installer in one layer 3G, and the package layer added on top 8G)</p>
  
  <p>(2) and if I wget the installer, run it, then delete the installer in the same RUN command, the image should only be 8G ( since 3G installer is deleted)</p>
  
  <p>Either way, the bottom line is, 16G space should be more than enough.</p>
  
  <p><strong>Problem</strong></p>
  
  <p>But my current observation is that during my docker build process,
  it would always hang since it had consumed all the available spaces</p>
  
  <pre><code>[root@xrh701 DockerImage]# df -h
  Filesystem                                        Size  Used Avail Use% Mounted on
  /dev/mapper/rhel-root                              18G   18G   20K 100% /
  ...
  </code></pre>
  
  <p>I can see two images</p>
  
  <pre><code>$ docker images
  REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
  &lt;none&gt;              &lt;none&gt;              fa09e98656ba        About an hour ago   258.1 MB
  rhel                latest              32f8a1d5f019        9 days ago          203.2 MB
  </code></pre>
  
  <p>"docker inspect " shows that the &lt; none > image is the intermediate image after COPY the entitlement file.</p>
  
  <pre><code>docker ps -a
  CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
  232245023f90        fa09e98656ba        "/bin/sh -c 'wget -O "   About an hour ago   Exited (0) About an hour ago                       lonely_curie
  </code></pre>
  
  <p>This intermediate container has completed the RUN wget line, exited successfully, but did not commit to docker image due to lack of space</p>
  
  <p>What's worse, I cannot remove the container nor delete /var/lib/docker to restore the space back </p>
  
  <pre><code># docker rm -f lonely_curie
  Failed to remove container (lonely_curie): Error response from daemon: Driver devicemapper failed to remove root filesystem 232245023f90b42a4dbd19a78bf32836f9f8618d7dbcba54159c3df029b5b114: mount still active
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <ol>
  <li><p>Why has docker used up all the space? According to the calculation, the disk space (16G) should be more than enough for the target image (8G). [I own this VM so I can guarantee no one else or any other process is consuming hard disk space ]</p></li>
  <li><p>How do I force remove the container in  my current situation so I can restore the space back?</p></li>
  <li><p>How should I build this 8G image (or 11G image if you count the installer) on a 16G VM? If this is not possible, what is the minimum space to successfully build it? I am currently applying for a 32G VM from the lab.</p></li>
  </ol>