如何使用Docker Compose中的卷在主机和容器之间共享数据
我在玩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
我们来看下面的事实:
- 我在主机下有一个目录:
~/var/www
- 这样的目录中的数据应该持续关于容器状态。
- 容器应该从
/var/www
下的主机写入数据
我已经在这里阅读文档,但不清楚如何处理数据量和主机数据。
我想在容器上共享主机上的数据,但是我甚至不知道上面docker-compose.yml
文件是正确的还是需要改变什么才能实现我所需要的。 我知道如何使用docker run
,但没有线索的Docker撰写?
任何能帮助我得到这个工作?
更新:玩这个
我已经将这行添加到docker-compose.yml
文件中:
volumes_from: - volumes_source
我再次运行docker-compose up
但是这是结果:
php55devwork_volumes_data_1 exited with code 0 php55devwork_volumes_source_1 exited with code 0
我不知道发生了什么,或者为什么我得到错误,任何?
看起来你正在试图定义一个“数据容器”。 这种模式过去很常见,但是在Docker 1.9中添加docker volume
系统后,这是不必要的( https://github.com/docker/docker/blob/master/CHANGELOG.md#190-2015-11- 03 )
您正在使用的这个图像tianon/true
devise用于运行“true”命令,除了返回退出码0之外什么都不做,然后退出。 这就是为什么容器显示为退出。
使用数据容器,而不是使用命名卷。 例如,使用数据容器的以下方法:
docker create --name data-container -v /sessions tianon/true docker run --volume-from data-container -d myapp
变成这样:
docker volume create --name sessions docker run -v sessions:/sessions -d myapp
由于您正在使用撰写,您可以使用卷键定义卷。
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
详细信息和示例位于以下位置: 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