• Introducing mount points
  • How to share data between the host and a container
  • How to share data between containers
  • Using temporary, in-memory filesystems
  • Managing data with volumes
  • Advanced storage with volume plugins

    4.1 File trees and mount points

    the image that a container is created from is mouted at that container’s file tree root.
    The best place to start is by understanding the three most common types of storage mounted into containers:

  • Bind mounts

  • In-memory storage
  • Docker volumes

    4.2 Bind mounts

    1. touch ~/example.log
    2. cat >~/example.conf <<EOF
    3. server {
    4. listen 80;
    5. server_name localhost;
    6. access_log /var/log/nginx/custom.host.access.log main;
    7. location / {
    8. root /usr/share/nginx/html;
    9. index index.html index.htm;
    10. }
    11. }
    12. EOF
    1. CONF_SRC=~/example.conf; \
    2. CONF_DST=/etc/nginx/conf.d/default.conf; \
    3. LOG_SRC=~/example.log; \
    4. LOG_DST=/var/log/nginx/custom.host.access.log; \
    5. docker run -d --name diaweb \
    6. --mount type=bind,src=${CONF_SRC},dst=${CONF_DST} \
    7. --mount type=bind,src=${LOG_SRC},dst=${LOG_DST} \
    8. -p 80:80 \
    9. nginx:latest
    ```shell docker rm -f diaweb

CONF_SRC=~/example.conf; \ CONF_DST=/etc/nginx/conf.d/default.conf; \ LOG_SRC=~/example.log; \ LOG_DST=/var/log/nginx/custom.host.access.log; \ docker run -d —name diaweb \ —mount type=bind,src=${CONF_SRC},dst=${CONF_DST},readonly=true \ —mount type=bind,src=${LOG_SRC},dst=${LOG_DST} \ -p 80:80 \ nginx:latest

  1. <a name="H6MkW"></a>
  2. # 4.3 In-memory storage
  3. ```shell
  4. docker run --rm \
  5. --mount type=tmpfs,dst=/tmp \
  6. --entrypoint mount \
  7. alpine:latest -v
  1. docker run --rm \
  2. --mount type=tmpfs,dst=/tmp,tmpfs-size=16k,tmpfs-mode=1770 \
  3. --entrypoint mount \
  4. alpine:latest -v

4.4 Docker volumes

  1. docker volume create \
  2. --driver local \
  3. --label example=location \
  4. location-example
  5. docker volume inspect \
  6. --format "{{json .Mountpoint}}" \
  7. location-example

image.png

4.4.1 Volumes provide container-independent data management

4.4.2 Using volumes with a NoSQL database

  1. docker volume create \
  2. --driver local \
  3. --label example=cassandra \
  4. cass-shared
  1. docker run -d \
  2. --volume cass-shared:/var/lib/cassandra/data \
  3. --name cass1 \
  4. cassandra:2.2
  1. docker run -it --rm \
  2. --link cass1:cass \
  3. cassandra:2.2 cqlsh cass
  1. select *
  2. from system.schema_keyspaces
  3. where keyspace_name = 'docker_hello_world';
  1. create keyspace docker_hello_world
  2. with replication = {
  3. 'class' : 'SimpleStrategy',
  4. 'replication_factor': 1
  5. };
  1. select *
  2. from system.schema_keyspaces
  3. where keyspace_name = 'docker_hello_world';
  1. docker run -d \
  2. --volume cass-shared:/var/lib/cassandra/data \
  3. --name cass2 \
  4. cassandra:2.2
  5. docker run -it --rm \
  6. --link cass2:cass \
  7. cassandra:2.2 \
  8. cqlsh cass
  9. select *
  10. from system.schema_keyspaces
  11. where keyspace_name = 'docker_hello_world';

4.5 Shared mount points and sharing files

  1. LOG_SRC=~/web-logs-example
  2. mkdir ${LOG_SRC}
  3. docker run --name plath -d \
  4. --mount type=bind,src=${LOG_SRC},dst=/data \
  5. dockerinaction/ch4_writer_a
  6. docker run --rm \
  7. --mount type=bind,src=${LOG_SRC},dst=/data \
  8. alpine:latest \
  9. head /data/logA
  10. cat ${LOG_SRC}/logA
  1. docker volume create \
  2. --driver local \
  3. logging-example
  4. docker run --name plath -d \
  5. --mount type=volume,src=logging-example,dst=/data \
  6. dockerinaction/ch4_writer_a
  7. docker run --rm \
  8. --mount type=volume,src=logging-example,dst=/data \
  9. alpine:latest \
  10. head /data/logA
  11. #the following command is not useful
  12. cat "$(docker volume inspect \
  13. --format "{{json .Mountpoint}}" logging-example)"/logA
  14. docker stop plath

4.5.1 Anonymous volumes and the volumes-from flag

  1. docker run --name fowler \
  2. --mount type=volume,dst=/library/PoEAA \
  3. --mount type=bind,src=/tmp,dst=/library/DSL \
  4. alpine:latest \
  5. echo "Fowler collection created"
  6. docker run --name knuth \
  7. --mount type=volume,dst=/library/TAoCP.vol1 \
  8. --mount type=volume,dst=/library/TAoCP.vol2 \
  9. --mount type=volume,dst=/library/TAoCP.vol3 \
  10. --mount type=volume,dst=/library/TAoCP.vol4.a \
  11. alpine:latest \
  12. echo "Knuth collection created"
  13. docker run --name reader \
  14. --volumes-from fowler \
  15. --volumes-from knuth \
  16. alpine:latest ls -l /library/
  17. docker inspect --format "{{json .Mounts}}" reader

4.6 Cleaning up volumes

  1. docker volume prune --filter example=cassandra

4.7 Advanced storage with volume plugins