/var/lib/docker. For example, listing the contents of this directory might display the following:
aufs, builder, containers, image, network, plugins, and swarm are clearly visible. Docker stores container-related files in the containers folder and image-related files in the image folder.
The storage driver plays a crucial role in managing image layers and associated files. You can check which storage driver is active by running
docker info.docker info command, you will see several details including the Docker version (e.g., 17.09) and the storage driver. In this example, the storage driver is aufs, which is the default on Debian and Ubuntu systems. The root directory for the storage driver is /var/lib/docker/aufs.
Listing the contents of the /var/lib/docker/aufs directory provides further insight:
- The diff folder contains the actual content of each image layer. Every instruction in a Dockerfile (for instance, copying source code into the image) creates a new layer stored within this directory.
- The layers folder holds metadata indicating how these image layers are stacked.
- The mnt folder stores information about the associated mount points.
Pulling the Hello-World Image
Next, pull the sample Docker image known as “hello-world” to see how Docker populates these directories:/var/lib/docker/aufs again shows the directories (while they might still appear empty at the top level, their subdirectories are now populated):
docker history command along with its image ID. First, list the available images:
- A script is copied into the image.
- The copied script is then set as the container’s command.
Building a Sample Python Flask Web Application
Next, let’s build a custom Docker image containing a simple Python Flask web application. In the sample application folder namedsimple-webapp-docker, you will find two files:
app.py– the source code of the application.Dockerfile– the build instructions.
Step 1: Exploring the Application Files
Navigate to the application directory and list its contents:How the Dockerfile Works
- Base Image: The application is built on top of Ubuntu 17.04.
- Package Installation: The package list is updated and Python along with pip is installed.
- Dependency Installation: The Flask package is installed via pip.
- Copying Source Code: The application source code (
app.py) is copied into the/opt/directory. - Launching the Application: An entry point is established to run the Flask application.
Step 2: Building the Docker Image
Build the Docker image by running:The unnamed image is the one you just built because no repository name or tag was specified. To make management easier, tag your image using the
-t parameter.Inspecting the AUFS Diff Directory
After building an image, Docker stores the actual content in the AUFS diff directory. To see the disk usage and structure under this directory, run:Demonstrating Layered Architecture with Multiple Dockerfiles
Docker’s layered architecture allows reuse of base layers (like operating system and dependencies) even if only the application code changes. Consider the following example with two Dockerfiles:Dockerfile (Original Application)
Dockerfile2 (Updated Application Version)
app2.py) is rebuilt. Note that when any step changes, Docker clears the cache for that step and all subsequent layers.
Creating an Updated Application
To demonstrate this:-
Copy the existing application file and Dockerfile:
-
Edit
app2.py(for example, change “Welcome” to “Welcome 2”) and updateDockerfile2to referenceapp2.py: -
Build the new image with the updated Dockerfile:
docker system df command presents the actual disk consumption without counting shared layers multiple times. For an even more detailed view, include the verbose flag (-v) to examine each image’s layer breakdown.
Conclusion
In this article we explored:- The default file structure created by Docker in
/var/lib/docker - How storage drivers like AUFS organize images into layers using directories such as
diff,layers, andmnt - How to inspect Docker images using the
docker historycommand - The efficiency of Docker’s layer caching during image rebuilds after minor application changes
- Monitoring actual disk usage with the
docker system dfcommand