- 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
touch ~/example.log
cat >~/example.conf <<EOF
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/custom.host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
EOF
```shell docker rm -f diawebCONF_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} \
--mount type=bind,src=${LOG_SRC},dst=${LOG_DST} \
-p 80:80 \
nginx:latest
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
<a name="H6MkW"></a>
# 4.3 In-memory storage
```shell
docker run --rm \
--mount type=tmpfs,dst=/tmp \
--entrypoint mount \
alpine:latest -v
docker run --rm \
--mount type=tmpfs,dst=/tmp,tmpfs-size=16k,tmpfs-mode=1770 \
--entrypoint mount \
alpine:latest -v
4.4 Docker volumes
docker volume create \
--driver local \
--label example=location \
location-example
docker volume inspect \
--format "{{json .Mountpoint}}" \
location-example
4.4.1 Volumes provide container-independent data management
4.4.2 Using volumes with a NoSQL database
docker volume create \
--driver local \
--label example=cassandra \
cass-shared
docker run -d \
--volume cass-shared:/var/lib/cassandra/data \
--name cass1 \
cassandra:2.2
docker run -it --rm \
--link cass1:cass \
cassandra:2.2 cqlsh cass
select *
from system.schema_keyspaces
where keyspace_name = 'docker_hello_world';
create keyspace docker_hello_world
with replication = {
'class' : 'SimpleStrategy',
'replication_factor': 1
};
select *
from system.schema_keyspaces
where keyspace_name = 'docker_hello_world';
docker run -d \
--volume cass-shared:/var/lib/cassandra/data \
--name cass2 \
cassandra:2.2
docker run -it --rm \
--link cass2:cass \
cassandra:2.2 \
cqlsh cass
select *
from system.schema_keyspaces
where keyspace_name = 'docker_hello_world';
4.5 Shared mount points and sharing files
LOG_SRC=~/web-logs-example
mkdir ${LOG_SRC}
docker run --name plath -d \
--mount type=bind,src=${LOG_SRC},dst=/data \
dockerinaction/ch4_writer_a
docker run --rm \
--mount type=bind,src=${LOG_SRC},dst=/data \
alpine:latest \
head /data/logA
cat ${LOG_SRC}/logA
docker volume create \
--driver local \
logging-example
docker run --name plath -d \
--mount type=volume,src=logging-example,dst=/data \
dockerinaction/ch4_writer_a
docker run --rm \
--mount type=volume,src=logging-example,dst=/data \
alpine:latest \
head /data/logA
#the following command is not useful
cat "$(docker volume inspect \
--format "{{json .Mountpoint}}" logging-example)"/logA
docker stop plath
4.5.1 Anonymous volumes and the volumes-from flag
docker run --name fowler \
--mount type=volume,dst=/library/PoEAA \
--mount type=bind,src=/tmp,dst=/library/DSL \
alpine:latest \
echo "Fowler collection created"
docker run --name knuth \
--mount type=volume,dst=/library/TAoCP.vol1 \
--mount type=volume,dst=/library/TAoCP.vol2 \
--mount type=volume,dst=/library/TAoCP.vol3 \
--mount type=volume,dst=/library/TAoCP.vol4.a \
alpine:latest \
echo "Knuth collection created"
docker run --name reader \
--volumes-from fowler \
--volumes-from knuth \
alpine:latest ls -l /library/
docker inspect --format "{{json .Mounts}}" reader
4.6 Cleaning up volumes
docker volume prune --filter example=cassandra