下面是演示,以便您更好地理解:
一次之后:
# docker-compose up -d
您可以使用以下任何方法来了解可用的容器卷:
# docker inspect -f '{{ .Mounts }}' <your-container-id-or-name>
# docker inspect <your-container-id-or-name> | jq --raw-output .[].Mounts
例如我有 mariadb 容器:
root@sys:/home/akshay/Documents/test2# docker inspect 00f70198a466 | jq --raw-output .[].Mounts
[
{
"Type": "volume",
"Name": "2a583fc243a9a2bb80cf45a80e5befbdc88db3b14026ff2349f80345f58c9562",
"Source": "/var/lib/docker/volumes/2a583fc243a9a2bb80cf45a80e5befbdc88db3b14026ff2349f80345f58c9562/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
在这里,您可以看到Source:
主机上的实际路径,以及Destination:
容器内的绝对路径。
现在说,例如,如果您希望存储在云上,首先停止您的容器,将您的外部存储安装在主机上,Source:
如果需要,移动/复制内容。
然后在卷中你只需要设置路径,例如
# creating local directory
# mkdir mysql-data
# copying contents whatever in volume
# cp -r /var/lib/docker/volumes/2a583fc243a9a2bb80cf45a80e5befbdc88db3b14026ff2349f80345f58c9562/_data mysql-data
# we copied data to directory of our interest
# instead of keeping in /var/lib/docker/volumes/...../_data
docker-compose down
在你的 docker-compose.yml 里面
volumes:
# local mount
- "./mysql-data:/var/lib/mysql"
# path to your remote storage ex: upload directory
- "/path/where/s3-bucket/mounted:/var/www/somesite/uploads/"
接着
# now we refer volumes in local directory
docker-compose up -d
如果没有可用的卷,则只需输入您的容器,如下所示,并找出您想要将数据保存在容器外的目录的绝对路径。
# with bash
# docker exec -it <your-container-id-or-name> bash
# or with shell
# docker exec -it <your-container-id-or-name> sh
# and then browser folders
# for example
root@sys:~# docker exec -it 00f70198a466 bash
root@00f70198a466:/# pwd
/
root@00f70198a466:/# ls
bin boot dev docker-entrypoint-initdb.d etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@00f70198a466:/# cd /var/lib/mysql/
root@00f70198a466:/var/lib/mysql# pwd
/var/lib/mysql