[英]How to share data between host and containers using volumes in Docker Compose
I am playing with Docker Compose and volumes 我正在使用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
Let's take the following facts: 我们来看看以下事实:
~/var/www
~/var/www
/var/www
/var/www
下的主机写入数据 I have read docs here but is not clear to me how to deal with data volumes and host data. 我在这里阅读了文档,但我不清楚如何处理数据量和主机数据。
I want to share the data on the host with the container but I don't even know if the docker-compose.yml
file above is right or what needs to be changed in order to achieve what I need. 我想与容器共享主机上的数据,但我甚至不知道上面
docker-compose.yml
文件是正确的还是需要更改的内容才能实现我的需要。 I know how to do it using docker run
alone but doesn't have a clue for Docker Compose? 我知道如何使用
docker run
单独docker run
它,但是没有Docker Compose的线索?
Can any help me to get this working? 能帮助我做到这一点吗?
Update: playing with this 更新:玩这个
I have added this lines to the docker-compose.yml
file: 我已将此行添加到
docker-compose.yml
文件中:
volumes_from:
- volumes_source
And I run the docker-compose up
once again but this is the result: 然后我再次运行
docker-compose up
但这是结果:
php55devwork_volumes_data_1 exited with code 0
php55devwork_volumes_source_1 exited with code 0
I am not sure what is happening or why I am getting the error, any? 我不确定发生了什么或为什么我得到错误,是吗?
It looks like you are trying to define a "data container". 看起来您正在尝试定义“数据容器”。 This pattern used to be common, but this isn't necessary after the
docker volume
system was added in Docker 1.9 ( https://github.com/docker/docker/blob/master/CHANGELOG.md#190-2015-11-03 ) 这种模式过去很常见,但在Docker 1.9中添加
docker volume
系统后不需要这样( https://github.com/docker/docker/blob/master/CHANGELOG.md#190-2015-11- 03 )
This image that you are using, tianon/true
, is designed to run the "true" command, which does nothing other than return exit code 0, and then exit. 您正在使用的这个图像,
tianon/true
,旨在运行“true”命令,除了返回退出代码0之外什么都不做,然后退出。 This is why the container is showing as exited. 这就是容器显示为退出的原因。
Instead of using data containers, use a named volume. 使用命名卷而不是使用数据容器。 For example, the following approach using a data container:
例如,以下方法使用数据容器:
docker create --name data-container -v /sessions tianon/true
docker run --volume-from data-container -d myapp
becomes this: 成为这个:
docker volume create --name sessions
docker run -v sessions:/sessions -d myapp
Since you are using compose, you can define volumes using the volumes key. 由于您正在使用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
Full details and an example are located here: https://docs.docker.com/compose/compose-file/compose-file-v2/ 完整的详细信息和示例位于: https : //docs.docker.com/compose/compose-file/compose-file-v2/
However, you also mentioned wanting to share this volume data between the container and your host. 但是,您还提到要在容器和主机之间共享此卷数据。 In that case, neither a data container nor named volume is necessary.
在这种情况下,既不需要数据容器也不需要命名卷。 You can just specify a host volume directly:
您可以直接指定主机卷:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.