Where are Docker images stored on the host machine?
Tags: docker, linux
Published: 2021-06-29 15:19:05

<p dir="ltr">When we create Docker objects such as <a href="https://techtutorialsite.com/what-are-docker-images/" rel="noopener noreferrer" target="_blank">images</a>, <a href="https://techtutorialsite.com/how-to-create-docker-container/" rel="noopener noreferrer" target="_blank">containers</a>, volumes, etc., all these objects are stored inside a directory in our local machine. By default, all the Docker objects are stored in the following directory.</p>

<pre>/var/lib/docker/storage-driver</pre>
                
<p dir="ltr">Moreover, the contents stored inside the /var/lib/docker path depend on the storage driver that you are using as well. Let&rsquo;s first understand the docker storage drivers in short.</p>
<h2 dir="ltr">Docker Storage Drivers</h2>
<p dir="ltr">In an ideal case, very little amount of data gets written onto the writable layer of a Docker container. And we use volumes in Docker to write such data. But there are some workloads that require us to be able to use the container&rsquo;s writable layer to write data and this is exactly where storage drivers in Docker come in.</p>
<p dir="ltr">Docker uses a pluggable architecture to support various different storage drivers. It totally depends upon these storage drivers that how the Docker images and containers are managed and stored in our Docker host.</p>
<p dir="ltr">The different types of storage drivers supported by Docker are - overlay2, aufs, devicemapper, btrfs, zfs, vfs. The current default Docker Storage driver is overlay2.</p>
<p dir="ltr">To check the default storage driver, you can use the following command.</p>

<pre>$ docker info</pre>

<p dir="ltr">You can set the storage driver in two different ways.</p>
<p dir="ltr">You can either use the -s or --storage-driver option along with the Docker run command. Or you can edit the /etc/docker/daemon.json file and include the following lines.</p>

<pre>{storge-driver:"aufs"}</pre>

<p dir="ltr">Now that we have understood the basics of storage drivers in Docker, let&rsquo;s see the different behaviors of different drivers when it comes to storing Docker objects in local machines.</p>
<h2 dir="ltr">Storage of Docker Images and Other Objects</h2>
<p dir="ltr">The driver-specific storage for Docker image contents will be in the path -&nbsp;</p>

<pre>/var/lib/docker/{driver-name}</pre>

<p dir="ltr">The metadata about the images is located in the JSON and layersize files in the following directory.</p>

<pre>/var/lib/docker/graph/id</pre>

<p dir="ltr">In the above paths, the driver-name is the default storage driver used by Docker in your machine and the id refers to the image ID.</p>
<p dir="ltr">If your host machine uses aufs storage driver for Docker, then you can look for image file contents in the following directory.</p>

<pre>/var/lib/docker/aufs/diff/id</pre>

<p dir="ltr">The directory mentioned below contains the local image information in a JSON file. This information can be retrieved using the <a href="https://techtutorialsite.com/list-docker-images/" rel="noopener noreferrer" target="_blank">Docker images command</a> in the command line.</p>

<pre>/var/lib/docker/repositories-aufs</pre>
         
<p dir="ltr">In the case of the devicemapper storage driver, the system stores the image information in the following directory.</p>
               <pre>/var/lib/docker/devicemapper/devicemapper/data</pre>
                
<p dir="ltr">The metadata related to the images can be found on the path -&nbsp;</p>
                    <pre>/var/lib/docker/devicemapper/devicemapper/metadata</pre>
               
<p dir="ltr">It&rsquo;s important to note that these files are not that big as they might seem to be because of the fact that they are thin provisioned sparse files.</p>
<p dir="ltr">If you are using Docker inside Mac through a <a href="https://techtutorialsite.com/docker-vs-virtual-machines/" rel="noopener noreferrer" target="_blank">Virtual Machine</a>, then you can find all your container and image information in the following directory.</p>
                    <pre>~/Library/Containers/com.docker.docker/Data/vms/0/Docker.raw</pre>
                
<p dir="ltr">On the new <a href="https://techtutorialsite.com/install-docker-on-windows/" rel="noopener noreferrer" target="_blank">Windows 10</a> which uses hyper-v, you can find the data in the <a href="https://techtutorialsite.com/docker-vs-virtual-machines/" rel="noopener noreferrer" target="_blank">Docker virtual</a> hard disk in the following path.</p>

<pre>C:\Users\Public\Documents\Hyper-V\Virtual hard disks\MobyLinuxVM.vhdx</pre>
                
<h2 dir="ltr">Wrapping Up!</h2>
<p dir="ltr">To sum up, the Docker storage drivers play a huge role in deciding the storage location of your metadata as well as all other information related to all the Docker objects including images, containers, volumes, etc.</p>
<p dir="ltr">However, the base directory path is /var/lib/docker and the subsequent directories depend upon the type of storage driver. Moreover, the location various with respect to the type of operating systems as well.&nbsp;</p>