1 minute read

Tags: ,


Section 5 Overview

  • Defining the problem of persistent data
  • Data Volumes
  • Data Mounts

Container Lifetime & Persistent Data

  • Containers are usually immutable and ephemeral
  • Immutable infrastructure: only re-deploy containers, never change
  • This is the ideal scenario, but what about databases, or unique data?
  • Docker gives us features to ensure these separation of concerns
  • This is known as persistent data
  • Two ways: Volumes and Bind Mounts
  • Volumes: make special location outsides of container UFS (union file system)
  • Bind Mounts: link container path to host path

Container Lifetime & Persistent Data

Docker 工作現場 實戰寶典 pg. 142


docker create volume create datavol

docker volume ls
  • ex:
docker container run -it --rm -v datavol:/data alpine
echo "This is a named volume demo" > /data/demo.txt
  • 離開 alpine
docker container run --rm -v datavol:/data ubuntu cat /data/demo.txt
  • inspect
docker volume inspect datavol
  • 用 tree 看 (linux)
sudo tree -a /var/lib/docker/volumes/datavol
  • 生活好涼拌

    • docker volume create -help
    • docker volume ls -help
    • docker volume inspect -help

Persisten Data Bind Mounting

mkdir $HOME/data_share
echo "data sharing demo" > $HOME/data_share/demo.txt
docker container run --rm -v $(HOME)/data_share:/data ubuntu cat /data/demo.txt


作業 Named Volumes and Bind Mounts