如何使用 Docker Compose 中的卷在主机和容器之间共享数据
How to share data between host and containers using volumes in 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
下写入来自主机的数据
我已阅读文档 here 但我不清楚如何处理数据量和主机数据。
我想和容器共享主机上的数据,但我什至不知道上面的docker-compose.yml
文件是否正确,或者需要更改什么才能实现我的需要。我知道如何单独使用 docker run
但不知道 Docker Compose?
任何人都可以帮助我完成这项工作吗?
更新:玩这个
我已将此行添加到 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
我不确定发生了什么或为什么会收到错误,有吗?
您似乎正在尝试定义 "data container"。这种模式以前很常见,但是在Docker 1.9 (https://github.com/docker/docker/blob/master/CHANGELOG.md#190-2015-11-03)
中添加了docker volume
系统后就没有必要了
您正在使用的此映像 tianon/true
旨在 运行 "true" 命令,该命令除了 return 退出代码 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
由于您使用的是 compose,因此您可以使用 volumes 键定义卷。
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
我正在玩 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
下写入来自主机的数据
我已阅读文档 here 但我不清楚如何处理数据量和主机数据。
我想和容器共享主机上的数据,但我什至不知道上面的docker-compose.yml
文件是否正确,或者需要更改什么才能实现我的需要。我知道如何单独使用 docker run
但不知道 Docker Compose?
任何人都可以帮助我完成这项工作吗?
更新:玩这个
我已将此行添加到 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
我不确定发生了什么或为什么会收到错误,有吗?
您似乎正在尝试定义 "data container"。这种模式以前很常见,但是在Docker 1.9 (https://github.com/docker/docker/blob/master/CHANGELOG.md#190-2015-11-03)
中添加了docker volume
系统后就没有必要了
您正在使用的此映像 tianon/true
旨在 运行 "true" 命令,该命令除了 return 退出代码 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
由于您使用的是 compose,因此您可以使用 volumes 键定义卷。
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