# Docker Storage

We will see how Docker stores the data & how it manages File Systems of the container.

Lets see how Docker stores data in the Local File System.Whenever we install Docker on our system it creates below folder structure:

\>>All files realted to container stores inside container folder.

\>>and the files related to images stores inside image folder. Any volume created by the Docker container are created under the volumes folder.&#x20;

![](/files/-MESXGDDKrBYJxN03A1M)

This is where Docker install its data by default

&#x20;So how Docker stores files on an image & a container . Something we remember that Docker build image in a layered architecture.

Each layer stores the changes from the previous layer.

Lets understand the advantages of the layered architecture .As we can see below on the right hand we have the similar Dockerfile with a different image while it using the same base image as ubuntu & using the same python & flask dependencies but using a different source code to create a different application & a different ENTRYPOINT as well.

\>>>If we build the image for the second app it will not build the first 3 layer instead it uses the same three layers that it build for the first app from the cache & only build the last two layers . This way Docker build image faster & efficiently saving Disk space. This is also applicable whenever we are updating our application code.

![](/files/-MEaCmVDxtbeb4NNEi4B)

All the layers are created when we run the Docker build command to form the final Docker image . So all of these are Docker image layers. Once the build is complete we cannot modify the content of these layers. So they are READ only & we can only modified them by reinitiating a new build .

\>>When we run an container based on that image using the Docker run command Docker create a container based on these layers & create a new writable layer on top of the image layer . The Writable layer is used to store data created by the container such as log files , tmp files or any file modified by the user on that container . The life of this layer though is only as long as container is alive . When the container is destroyed this layer & all of its changes stored in it are also destroyed .

\>>Remember same image layer is shared by all containers created by using this image.

![](/files/-MEaG2qA1JhRTYZAyHfJ)

\>>So if we create a new file called temp.txt. It will create this file in container layer which is R/W. We just said files in the Image layer is READ only means we cannnot edit anything in those layers.

\>>We can modify app.py but before we saved modified file Docker automatically creates a copy of file in R/W layer & now we able to modify different version of the file in the R/W layer. All future modifications will be done on this version of the file in the R/W layer. This is called COPY-ON-WRITE mechanism.

![](/files/-MEaHchG5ua0z1sLKGlG)

\>>What happens when we get rid of container . All of the data that was stored in the container layer also gets deleted. The change that we made to the app.py & the new temp file we created will also get removed. So what if we want to persist this data. For example we working with a database & we would like to preserve the data created by the container we could add a persistent volume to the container .

So , let's do this , fisrt create a volume using docker volume create command . It cerates folder inside /var/lib/docker/volumes directory. This process is Volume mounting.

\>>Now what if we want to store the data outside of DOCKER-HOST(external storage outisde docker host) default directory i:e /var/lib/docker/.

We can do that using below command :

docker run -v /data/mysql:/var/lib/mysql mysql&#x20;

& this process is called Bind-mounting. So there are two types of mounts.

a) Volume-Mouting  : It mount from the volumes directory which is the deafult directory of Docker .&#x20;

b) Bind-Mouting : It mount directory from any location in the DOCKER-HOST.

\>>Using -v is an old style the new way is to use --mount option : it is more verbose&#x20;

So who is maintaining all this creating layered architecture , moving files across layers to enable COPY & WRITE etc...Its the Storage Drivers. So Docker used Storage Dirvers to enable Layered architecture. Some of the common storage drivers are :

AUFS

ZFS & others.....

![](/files/-MEaKrjGp2xt3h_9oary)

The selection of storage dirvers depends on the underlying OS in use. For example : with Ubuntu the default storage driver is AUFS whereas this storage drivers is not available on other OS like Fedora & CentOS. In that case DeviceMapper is the better option . Docker will chooose best storage driver automatically based on the operating system. The different storage drivers also provides different performance & stability characteristics.&#x20;

![](/files/-MEaLaep6QLeiK7V8Vda)

Practical Implementation :

<https://github.com/kodekloudhub/webapp-color>

Run **docker info | more**&#x20;

It gives information which storage driver is installed on my system .

![](/files/-MEaN77SGq8P7EZAaGC1)

![](/files/-MEaNdjorkCP18KYP2xt)

![](/files/-MEaPqmlinmDvQez_rVe)

If we want to see the layers that build the Docker image . Just run&#x20;

**docker history simple-webapp**

![](/files/-MEaQ2fCwnsuv_5TBpCp)

![](/files/-MEaQU4CIdUz4Y-5IzRF)

Build Docker file image with a new Dockerfile2 .

![](/files/-MEaRMUWOw3S_hU-c8YR)

If we run **docker system df** command . It will show the actual Disk consumption of our Docker images :

![](/files/-MEaT7rrBBJ-De5jkUg0)

![](/files/-MEaTU-ruUsGTKP-nJN4)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://architectures.gitbook.io/project/docker/docker-engine-storage-and-networking/docker-storage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
