这可以使用数据容器存档。
看看这个 bash 脚本:
#!/bin/bash
docker kill data-test-container > /dev/null 2>&1
docker rm data-test-container > /dev/null 2>&1
docker rmi data-test-image > /dev/null 2>&1
docker build -t data-test-image - <<END_DOCKERFILE
FROM busybox
RUN mkdir /data && dd if=/dev/urandom bs=1K count=100 of=/data/persistent
VOLUME /data
END_DOCKERFILE
docker run -d --name=data-test-container data-test-image true
echo File checksum in image:
docker run --rm --volumes-from=data-test-container busybox md5sum /data/persistent
docker run --rm --volumes-from=data-test-container busybox dd if=/dev/urandom bs=1K count=100 of=/data/persistent
echo File checksum after change:
docker run --rm --volumes-from=data-test-container busybox md5sum /data/persistent
docker rm data-test-container
docker run -d --name=data-test-container data-test-image true
echo File checksum after restore:
docker run --rm --volumes-from=data-test-container busybox md5sum /data/persistent
这是输出:
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM busybox
---> 4986bf8c1536
Step 1 : RUN mkdir /data && dd if=/dev/urandom bs=1K count=100 of=/data/persistent
---> Running in 117d1884d448
100+0 records in
100+0 records out
---> ac22e944c73c
Removing intermediate container 117d1884d448
Step 2 : VOLUME /data
---> Running in 52cf4b446255
---> d7f3494b1f72
Removing intermediate container 52cf4b446255
Successfully built d7f3494b1f72
b7c7fe133479afed41f0af31df7a4535d19353bc976b25393a2c6fe68344ac6b
File checksum in image:
029ae64fe02ebfb6a3ebf0af31965826 /data/persistent
100+0 records in
100+0 records out
File checksum after change:
767260fa798a55d639de0f775e18595c /data/persistent
data-test-container
19b9e3a5d3286c7a9bee101b1984ac033a2dd8f0bd7a6648f6c451b5e0cded32
File checksum after restore:
029ae64fe02ebfb6a3ebf0af31965826 /data/persistent
如果您想将实时数据放入图像中(而不是必须从头开始构建),快照应该是可行的方法,但我还没有尝试过,并且有人告诉我有一个讨厌的错误使无法删除快照的 Docker 映像存储库服务器代码(如果您设置本地服务器来托管快照)。不过,这个错误现在可能已经修复了。