如何使用Docker Compose中的卷在主机和容器之间共享数据

Rey*_*rPM 15 docker docker-compose docker-volume

我正在使用Docker Compose和卷

version: '2'
services:
    php-apache:
        env_file:
          - dev_variables.env
        image: reypm/php55-dev
        build:
            context: .
            args:
                - PUID=1000
                - PGID=1000
        expose:
            - "80"
            - "9001"
        extra_hosts:
            # IMPORTANT: Replace with your Docker Host IP (will be appended to /etc/hosts)
            - "dockerhost:xxx.xxx.xxx.xxx"
        volumes_from:
            - volumes_source
    volumes_source:
        image: tianon/true
        volumes:
            - ../:/var/www
    volumes_data:
        image: tianon/true
        volumes:
            - ./data/sessions:/sessions
Run Code Online (Sandbox Code Playgroud)

我们来看看以下事实:

  • 我在主机下面有一个目录: ~/var/www
  • 此类目录中的数据应该与容器状态保持一致.
  • 容器应该从主机下写入数据 /var/www

我在这里阅读过文档,但我不清楚如何处理数据量和主机数据.

我想与容器共享主机上的数据,但我甚至不知道docker-compose.yml上面的文件是正确的还是需要更改的内容才能实现我的需要.我知道如何docker run单独使用它,但没有Docker Compose的线索?

能帮助我做到这一点吗?

更新:玩这个

我已将此行添加到docker-compose.yml文件中:

    volumes_from:
        - volumes_source
Run Code Online (Sandbox Code Playgroud)

docker-compose up再次运行,但这是结果:

php55devwork_volumes_data_1 exited with code 0
php55devwork_volumes_source_1 exited with code 0
Run Code Online (Sandbox Code Playgroud)

我不确定发生了什么或为什么我得到错误,是吗?

pro*_*erq 25

看起来您正在尝试定义"数据容器".这种模式过去很常见,但docker volume在Docker 1.9中添加系统后不需要这样(https://github.com/docker/docker/blob/master/CHANGELOG.md#190-2015-11-03)

您正在使用的此图像tianon/true旨在运行"true"命令,除了返回退出代码0之外什么都不做,然后退出.这就是容器显示为退出的原因.

使用命名卷而不是使用数据容器.例如,以下方法使用数据容器:

docker create --name data-container -v /sessions tianon/true
docker run --volume-from data-container -d myapp
Run Code Online (Sandbox Code Playgroud)

成为这个:

docker volume create --name sessions
docker run -v sessions:/sessions -d myapp
Run Code Online (Sandbox Code Playgroud)

由于您正在使用compose,因此可以使用卷键定义卷.

version: '2'
services:
    php-apache:
        env_file:
          - dev_variables.env
        image: reypm/php55-dev
        build:
            context: .
            args:
                - PUID=1000
                - PGID=1000
        expose:
            - "80"
            - "9001"
        extra_hosts:
            # IMPORTANT: Replace with your Docker Host IP (will be appended to /etc/hosts)
            - "dockerhost:xxx.xxx.xxx.xxx"
        volumes:
            - sessions:/sessions
            - docroot:/var/www
volumes:
    sessions:
        driver: local
    docroot:
        driver: local
Run Code Online (Sandbox Code Playgroud)

完整的详细信息和示例位于:https://docs.docker.com/compose/compose-file/compose-file-v2/

但是,您还提到要在容器和主机之间共享此卷数据.在这种情况下,既不需要数据容器也不需要命名卷.您可以直接指定主机卷:

version: '2'
services:
    php-apache:
        env_file:
          - dev_variables.env
        image: reypm/php55-dev
        build:
            context: .
            args:
                - PUID=1000
                - PGID=1000
        expose:
            - "80"
            - "9001"
        extra_hosts:
            # IMPORTANT: Replace with your Docker Host IP (will be appended to /etc/hosts)
            - "dockerhost:xxx.xxx.xxx.xxx"
        volumes:
            - ./data/sessions:/sessions
            - ../:/var/www
Run Code Online (Sandbox Code Playgroud)

  • 如果 Docker 的文档像你的答案一样清晰,我们都会节省很多时间...... (5认同)