Knowledge Bank
  • My GitBook
  • Miscellaneous
  • Project
    • Rider and Intellij
    • Code
    • Frontend
      • Condition
      • AddConditionModalDialog
    • Backend
    • e2e
      • fragments
  • JAVASCRIPT
    • Promise
    • Destructuring
    • Spread Syntax and Rest Parameters
    • Typescript
      • Examples of Types
      • React Typescript
    • This
    • Dot Notation vs Bracket Notation
    • Shallow vs Deep Clone
    • New ES Edition
  • C#
    • Project Note
    • Basic
    • Shortcut and Debugging
  • Programming Paradigms
    • SOLID Principles
    • Object Oriented Programming (OOP)
      • Evolution of OOP (Procedural to OOP)
      • Instantiation
      • 4 Pillars of OOP
      • Extra
    • Functional Programming (FP)
      • Idempotent
      • Imperative vs Declarative
      • Immutability
      • High Order Function and Closure
      • Currying
      • Partial Application
      • Memoization and Caching
      • Compose and Pipe
      • Extra
      • Example of FP
    • OOP vs FP
      • Composition vs Inheritance
  • DATA STRUCTURE
    • Big O
    • Data Structure
    • Array
    • Hash Table
    • Linked List
    • Queue and Stack
    • Tree
      • Binary Heap
      • Trie
    • Graph
      • Example of Graph
  • React-Redux
    • MobX
    • Best Practices
  • Algorithms
    • Recursion
      • Examples of Recursion
    • Sorting
    • Searching and Traversal
    • Dynamic Programming
  • REFACTORING
    • Clean Code
      • Formatting
      • Error Handling
      • Concurrency
      • Testing
      • SOLID Principles
      • Classes
      • Objects and Data Structures
      • Variables
      • Functions
    • Code Smells
      • Long Function
      • Duplicate Code
      • Loops
      • Double Negative
      • Christmas Tree Code
      • Complex Condition
      • Primitive Obsession
      • Speculative Generality
      • God Class
      • Long Parameter List
  • Junior to Senior
    • AWS
      • Lambda
    • Session + Authentication
    • Redis
    • Kubernetes
      • Networking
      • Services
      • Deployment
      • Replica Set
      • YAML
      • pod-definition.yml
      • Kubectl
      • Pods
      • Fundamentals
    • Docker
      • Operating System - Extra
      • Dockerfile - Docker Image
      • Docker Storage
      • Docker Network
      • Docker Registry
      • Docker Command
      • Docker Compose
      • Docker Compose - Postgres
    • Security
      • Logging
      • HTTPS, Cross-Site-Scripting (XSS) and Cross-Site-Request-Forgery (CSRF)
      • 3rd Party Library
      • Injection
      • Code Secret, Secure Header, Access Control, Data Management, Authentication
    • CI/CD
    • SPA vs Server-Side Rendering
    • Performance
      • Optimized Code
      • Critical Render Path
      • Backend Optimization
      • Minimized Files and Images
      • Minimized Delivery
  • SECURITY
    • Encryption
    • SSH
  • Command
  • Cheatsheet
    • NPM
    • GIT
  • Writing Template
    • Guide
    • API
    • ChangeLog
    • FAQ
  • Linux
Powered by GitBook
On this page

Was this helpful?

  1. Junior to Senior
  2. Docker

Docker Storage

PreviousDockerfile - Docker ImageNextDocker Network

Last updated 4 years ago

Was this helpful?

How docker store data on the local file system? When you install docker on a system, it creates this folder structure at directory: var/lib/docker

When we run docker build, we are building image layers to form final docker image and these image layers are read only. You can modify them only by initiating a new build.

When you run a container, docker create a container based on these image layers and create a new writable layer (read-write) on top of image layers.

The writable layer is used to store data created by the container such as log file or temp file generated by the container or any file modified by user on the container. The life of this layer is as long as the container is alive.

What happen if I wish to modify the source code in the Read-Only after running a container? Remember that the same image layer may be shared between container. Does it mean I cannot modify the files inside the container? No, because I can still modify the file, but before I save the modified file, docker automatically create a copy of the file in the read write layer and I will be modifying different version of the file in the read-write layer. All future modification will be done on this copy of the file in the read-write layer. This called copy-on-write mechanism. So the modification does not happen in the read-only layer and thus image will remain the same all the time until you rebuild the image.

For persistence, you run docker volume create data_volume and this create a folder called data_volume under /var/lib/docker. When you run command, docker run -v data_volume:/car/lib/mysql mysql I could mount this volume inside the docker container read/write layer.

Volume Mounting

Mount volume from volume directory. So, it means even though you do not run the command docker volume create data_volume2, when you run docker run -v data_volume2:/car/lib/mysql mysql, docker will automatically create data_volume2 folder.

Bind Mounting

Mount a directory from any location on the docker host. For example, you have data at external storage on the docker host and we would like to store databases data on that volume and not in the default var/lib/docker volume folder. We can use docker run -v /data/mysql:var/lib/mysql mysql

The new method of is use --mount. Example (see above).