如何使用Docker Compose v2使卷永久化

jim*_*mmy 16 singlestore docker docker-compose

我意识到其他人有类似的问题,但这使用v2 compose文件格式,我没有找到任何东西.

我想制作一个非常简单的测试应用程序来玩MemSQL,但我不能让卷不被删除docker-compose down.如果我已经理解了Docker Docs,那么在没有明确说明的情况下不应删除卷.一切似乎都可以使用,docker-compose up但在下降后再次上升所有数据都会从数据库中删除.

作为一个好的做法,我建议使用单独的memsqldata服务作为单独的数据层.

这是我的docker-compose.yml:

version: '2'
services:
    app:
        build: .
        links:
            - memsql
    memsql:
        image: memsql/quickstart
        volumes_from:
            - memsqldata
        ports:
            - "3306:3306"
            - "9000:9000"
    memsqldata:
        image: memsql/quickstart
        command: /bin/true
        volumes:
            - memsqldatavolume:/data

volumes:
    memsqldatavolume:
        driver: local
Run Code Online (Sandbox Code Playgroud)

BMi*_*tch 23

我意识到这是一个旧的解决线程,其中OP指向容器中的目录而不是它们已经安装的卷,但是想要清除我看到的一些错误信息.

docker-compose down不删除卷,docker-compose down -v如果您还想删除卷,则需要运行.这是直接来自docker-compose的帮助文本(请注意"默认情况下"列表):

$ docker-compose down --help
Stops containers and removes containers, networks, volumes, and images
created by `up`.

By default, the only things removed are:

- Containers for services defined in the Compose file
- Networks defined in the `networks` section of the Compose file
- The default network, if one is used

Networks and volumes defined as `external` are never removed.

Usage: down [options]

Options:
...
    -v, --volumes       Remove named volumes declared in the `volumes` section
                        of the Compose file and anonymous volumes
                        attached to containers.
...

$ docker-compose --version
docker-compose version 1.12.0, build b31ff33
Run Code Online (Sandbox Code Playgroud)

这是一个示例yml,其中包含要测试的命名卷和一个虚拟命令:

$ cat docker-compose.vol-named.yml
version: '2'

volumes:
  data:

services:
  test:
    image: busybox
    command: tail -f /dev/null
    volumes:
    - data:/data

$ docker-compose -f docker-compose.vol-named.yml up -d
Creating volume "test_data" with default driver
Creating test_test_1
Run Code Online (Sandbox Code Playgroud)

启动容器后,由于图像在该位置为空,因此将该卷初始化为空.我在那个位置创建了一个快速的hello世界:

$ docker exec -it test_test_1 /bin/sh
/ # ls -al /data
total 8
drwxr-xr-x    2 root     root          4096 May 23 01:24 .
drwxr-xr-x    1 root     root          4096 May 23 01:24 ..
/ # echo "hello volume" >/data/hello.txt
/ # ls -al /data
total 12
drwxr-xr-x    2 root     root          4096 May 23 01:24 .
drwxr-xr-x    1 root     root          4096 May 23 01:24 ..
-rw-r--r--    1 root     root            13 May 23 01:24 hello.txt
/ # cat /data/hello.txt
hello volume
/ # exit
Run Code Online (Sandbox Code Playgroud)

在Docker之外可以看到该卷,并且在以下情况之后仍然存在docker-compose down:

$ docker volume ls | grep test_
local               test_data

$ docker-compose -f docker-compose.vol-named.yml down
Stopping test_test_1 ... done
Removing test_test_1 ... done
Removing network test_default

$ docker volume ls | grep test_
local               test_data
Run Code Online (Sandbox Code Playgroud)

重新创建容器使用旧卷,文件仍然可见:

$ docker-compose -f docker-compose.vol-named.yml up -d
Creating network "test_default" with the default driver
Creating test_test_1

$ docker exec -it test_test_1 /bin/sh
/ # cat /data/hello.txt
hello volume
/ # exit
Run Code Online (Sandbox Code Playgroud)

并运行docker-compose down -v最终删除容器和卷:

$ docker-compose -f docker-compose.vol-named.yml down -v
Stopping test_test_1 ... done
Removing test_test_1 ... done
Removing network test_default
Removing volume test_data

$ docker volume ls | grep test_

$
Run Code Online (Sandbox Code Playgroud)

如果您发现只有在使用停止/启动而不是向下/向上的情况下才会保留数据,那么您的数据将存储在容器(或可能是匿名卷)中,而不是存储在您指定的卷中,并且容器不是执着.确保容器内数据的位置正确,以避免这种情况发生.

要调试容器中存储数据的位置,我建议docker diff在容器上使用.这将显示在该容器内创建,修改或删除的所有文件,这些文件在删除容器时将丢失.例如:

$ docker run --name test-diff busybox \
  /bin/sh -c "echo hello docker >/etc/hello.conf"

$ docker diff test-diff
C /etc
A /etc/hello.conf
Run Code Online (Sandbox Code Playgroud)


jim*_*mmy 4

这可以追溯到 MemSQL 的错误文档。memsql/quickstart容器中的 MemSQL 数据路径与独立安装(以及 MemSQL 文档中)不同,并且绝对不像/memsql有人告诉我的那样。/var/lib/memsql/data