There are 2 options for storing data:
There are 3 options for mounting data from docker host:
Reference: https://github.com/moby/moby/issues/21485
On host
Host bind mount
tmpfs mount
- Within the container on its own filesystem - Not recommended for write heavy workloads
- Outside the container
There are 3 options for mounting data from docker host:
- Volumes
- Bind mount
- Tmpfs mount
Volumes
- Stored on host filesystem
- Managed by docker (/var/lib/docker/volumes/)
- Option recommended by docker
- Can create named volumes or anonymous volumes
- Support use of volume drivers that allow you to write data outside container. For eg Flocker, Convoy, sshfs etc See list here
- Good option when sharing data between containers
- Reference: https://docs.docker.com/engine/admin/volumes/volumes/
Bind mounts
- Stored on host filesystem
- Can be anywhere on host
- Other process can see and modify
- Reference: https://docs.docker.com/engine/admin/volumes/bind-mounts/
Tmpfs mount
- Stored in host memory
- Used for storing secrets or temporary data
- Fastest
- Reference: https://docs.docker.com/engine/admin/volumes/tmpfs/
Performance Difference
Following shows performance of different mount options on Ubuntu instance on AWS t2.micro. (Doesn't include volume driver option as it will vary from driver to driver). Look for kB/s or MB/sReference: https://github.com/moby/moby/issues/21485
On host
ubuntu@host:~/nks$ sudo time dd if=/dev/zero of=/home/ubuntu/nks/out.txt bs=512 count=1000 oflag=dsync 1000+0 records in 1000+0 records out 512000 bytes (512 kB, 500 KiB) copied, 1.91264 s, 268 kB/s 0.02user 0.00system 0:01.91elapsed 1%CPU (0avgtext+0avgdata 2032maxresident)k 0inputs+8000outputs (0major+82minor)pagefaults 0swapsNamed volume
ubuntu@host:~/nks$ sudo docker volume create my-vol my-vol ubuntu@host:~/nks$ sudo docker volume ls DRIVER VOLUME NAME local my-vol ubuntu@host:~/nks$ sudo docker run --rm --net=none --log-driver=none --read-only -v "my-vol:/nks" ubuntu bash -c "time dd if=/dev/zero of=/nks/out.txt bs=512 count=1000 oflag=dsync" 1000+0 records in 1000+0 records out 512000 bytes (512 kB, 500 KiB) copied, 1.90823 s, 268 kB/s real 0m1.909s user 0m0.000s sys 0m0.028s
Host bind mount
ubuntu@host:~/nks$ sudo docker run --rm --net=none --log-driver=none --read-only -v "/home/ubuntu/nks:/nks" ubuntu bash -c "time dd if=/dev/zero of=/nks/out.txt bs=512 count=1000 oflag=dsync" 1000+0 records in 1000+0 records out 512000 bytes (512 kB, 500 KiB) copied, 2.10316 s, 243 kB/s real 0m2.104s user 0m0.028s sys 0m0.000s
tmpfs mount
ubuntu@host:~/nks$ sudo docker run --rm --net=none --log-driver=none --read-only --mount type=tmpfs,destination=/nks ubuntu bash -c "time dd if=/dev/zero of=/nks/out.txt bs=512 count=1000 oflag=dsync" 1000+0 records in 1000+0 records out 512000 bytes (512 kB, 500 KiB) copied, 0.000757215 s, 676 MB/s real 0m0.001s user 0m0.000s sys 0m0.000s ubuntu@host:~/nks$
Storing data within container
- Data is not persistent across container lifecycle
- Inefficient for write heavy workloads due to disk block allocation and write through storage drivers
- Pick driver that works best for you. Choice may be limited by the OS.
- Reference: https://docs.docker.com/engine/userguide/storagedriver/