首页 > 解决方案 > “docker-compose down --volume”后数据丢失?

问题描述

我在 docker-compose 文件中添加了一个未命名的卷。例如:

  app:
    volumes:
      - /external/path:/internal/path

在我测试了 docker-compose 之后,我把它拿下来了:

sudo docker-compose down --volumes

我假设它只删除命名卷。但是,情况似乎并非如此,我丢失了/external/path.

有什么办法可以恢复吗?

标签: dockerdocker-compose

解决方案


这不是 docker-compose 的行为。这是一个显示其工作原理的最小示例:

$ mkdir bind
                                                                                                                        
$ vi docker-compose.yml

$ cat docker-compose.yml                                         
version: '3.7'                           
                                                     
services:                                                        
  test:                            
    image: busybox 
    command: tail -f /dev/null
    volumes:                  
    - /vol/anonymous          
    - named:/vol/named        
    - ./bind:/vol/bind        

volumes:    
  named:    

启动服务并在卷中创建数据:

$ docker-compose up -d 
Creating network "test_default" with the default driver
Creating volume "test_named" with default driver
Creating test_test_1 ... done

$ docker-compose exec test sh
/ # cd /vol
/vol # ls
anonymous  bind       named
/vol # ls anonymous/
/vol # echo hello anonymous >anonymous/data.txt
/vol # echo hello bind >bind/data.txt
/vol # echo hello named >named/data.txt
/vol # exit

检查容器以获取匿名卷 ID:

$ docker ps -l
CONTAINER ID   IMAGE     COMMAND               CREATED              STATUS              PORTS     NAMES
80e4bf2c65b1   busybox   "tail -f /dev/null"   About a minute ago   Up About a minute             test_test_1

$ docker inspect 80e 
[                    
    { 
        "Id": "80e4bf2c65b1a28dcf33e74af30ed53896b09bb236490f4bc9cfdcd2074c4d2c",
        "Created": "2020-12-04T15:19:48.088249937Z",
        "Path": "tail",
        "Args": [
            "-f",
            "/dev/null"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 10947,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-12-04T15:19:48.628296876Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
...
        "Mounts": [
            {      
                "Type": "volume",
                "Name": "test_named", 
                "Source": "/home/docker/volumes/test_named/_data", 
                "Destination": "/vol/named",
                "Driver": "local",          
                "Mode": "rw",               
                "RW": true,            
                "Propagation": ""      
            },                         
            {                          
                "Type": "bind",
                "Source": "/home/bmitch/data/docker/test/bind",
                "Destination": "/vol/bind", 
                "Mode": "rw",       
                "RW": true,  
                "Propagation": "rprivate"
            }, 
            {  
                "Type": "volume",  
                "Name": "d3fdead93dba2db526e6b1fb8501bf7fd8b412108efec8a720f137ccf3e0f021",
                "Source": "/home/docker/volumes/d3fdead93dba2db526e6b1fb8501bf7fd8b412108efec8a720f137ccf3e0f021/_data",
                "Destination": "/vol/anonymous",
                "Driver": "local",
                "Mode": "",          
                "RW": true,          
                "Propagation": ""
            }
        ],

从另一个容器查看所有三个卷中的数据:

$ docker run -it --rm \
    -v d3fdead93dba2db526e6b1fb8501bf7fd8b412108efec8a720f137ccf3e0f021:/vol/anonymous \
    -v "$(pwd)/bind:/vol/bind" \
    -v test_named:/vol/named \
    busybox sh
/ # cd vol
/vol # find .
.
./bind
./bind/data.txt
./anonymous
./anonymous/data.txt
./named
./named/data.txt
/vol # exit

从容器外部可以看到绑定挂载数据:

$ ls -al bind 
total 12      
drwxr-xr-x  2 bmitch bmitch 4096 Dec  4 10:20 .
drwxr-xr-x 14 bmitch bmitch 4096 Dec  4 10:19 ..
-rw-r--r--  1 root   root     11 Dec  4 10:20 data.txt

--volumes现在使用该选项停止服务,与-v

$ docker-compose down --volumes      
Stopping test_test_1 ... done   
Removing test_test_1 ... done        
Removing network test_default       
Removing volume test_named

命名和匿名卷消失了,但绑定挂载上的数据仍然存在:

$ docker volume ls 
DRIVER    VOLUME NAME 
local     1fb7aea19e66f595f4900407eac7240479eed40dc3b91b72bb129182ae82240c
local     a8820115a79de66173a18c2cd7bb3a08a1f4075b88c0bed8c1aa1de92792c601
local     d555896ef05e0410003a4ae4cce943b9719a3306061fde0d03424d42b7f0d6cd
... 
    
$ ls -al bind  
total 12       
drwxr-xr-x  2 bmitch bmitch 4096 Dec  4 10:20 .
drwxr-xr-x 14 bmitch bmitch 4096 Dec  4 10:19 ..
-rw-r--r--  1 root   root     11 Dec  4 10:20 data.txt

有什么办法可以恢复吗?

如果您丢失了数据,则需要从备份中恢复。当您要求它删除某些内容时,Docker 不会将您的卷数据保存在另一个位置。


推荐阅读