“数据容器”目录如何挂载到主机目录以保存持久数据

“数据容器”目录如何挂载到主机目录以保存持久数据

我正在尝试将我的 Rails 应用程序 Docker 化。

目前我有两个容器:

  1. “数据容器”。此容器中包含 Rails 应用程序代码和所有必要的 gem。“数据容器”公开 Rails 应用程序根目录和 gem 目录的卷

  2. “Web 服务器容器”。此容器中运行 Nginx + Passenger。“Web 服务器容器”与“数据容器”卷绑定。

一切正常。

但我需要将所有日志和用户上传的图像(存储在 Rails 应用程序根目录“public/images”下)目录备份到主机目录。我知道我可以启动连接到“数据容器”的新容器并将一些备份到主机目录。但这种方法并不是我真正想要实现的。我希望所有日志和图像都是即时的,并且始终写入主机目录。

我尝试将主机目录挂载到“数据容器”目录,我希望将数据存储在主机上,但这种方法不起作用。

启动并检查“数据容器”

docker run --name application -v $HOME/cabin/data/log:/opt/application/log -v $HOME/cabin/data/images:/opt/application/public/images foo/application

docker inspect application

"Volumes": {
    "/opt/application": "/var/lib/docker/vfs/dir/0c2a96af0e13d2b1c451610dc7bd561cffd246564e1966cb55628d329b4ee62b",
    "/opt/application/log": "/home/vagrant/cabin/data/log",
    "/opt/application/public/images": "/home/vagrant/cabin/data/images",
    "/webapp_gems": "/var/lib/docker/vfs/dir/b2a08475272e1a04eae121094c0369a38c22dd92ca76fc2c2418eeabea31e266",
    "/webapp_lib64": "/var/lib/docker/vfs/dir/44df839567e15c471e3ccd33a28778d7f67496c32f6110703d4d5e15677e00f6"
},
"VolumesRW": {
    "/opt/application": true,
    "/opt/application/log": true,
    "/opt/application/public/images": true,
    "/webapp_gems": true,
    "/webapp_lib64": true
}

启动并检查“Web 服务器容器”

docker run -d -p 9101:9101 --name server -v $HOME/cabin/data/log:/var/log/nginx -v $HOME/cabin/data/run:/var/run/nginx -v $HOME/cabin/configuration/nginx:/etc/nginx --volumes-from application foo/server nginx -c /etc/nginx/demo.conf


docker inspect server

"Volumes": {
    "/etc/nginx": "/home/vagrant/cabin/configuration/nginx",
    "/opt/application": "/var/lib/docker/vfs/dir/0c2a96af0e13d2b1c451610dc7bd561cffd246564e1966cb55628d329b4ee62b",
    "/opt/application/log": "/home/vagrant/cabin/data/log",
    "/opt/application/public/images": "/home/vagrant/cabin/data/images",
    "/var/log/nginx": "/home/vagrant/cabin/data/log",
    "/var/run/nginx": "/home/vagrant/cabin/data/run",
    "/webapp_gems": "/var/lib/docker/vfs/dir/b2a08475272e1a04eae121094c0369a38c22dd92ca76fc2c2418eeabea31e266",
    "/webapp_lib64": "/var/lib/docker/vfs/dir/44df839567e15c471e3ccd33a28778d7f67496c32f6110703d4d5e15677e00f6"
},
"VolumesRW": {
    "/etc/nginx": true,
    "/opt/application": true,
    "/opt/application/log": true,
    "/opt/application/public/images": true,
    "/var/log/nginx": true,
    "/var/run/nginx": true,
    "/webapp_gems": true,
    "/webapp_lib64": true
}

通过运行应用程序/主页/流浪者/机舱/数据/日志/主页/流浪者/小屋/数据/图像没有从应用程序容器写入任何数据

我的环境

docker info

Containers: 2
Images: 92
Storage Driver: devicemapper
 Pool Name: docker-8:1-524459-pool
 Pool Blocksize: 64 Kb
 Data file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
 Data Space Used: 7605.2 Mb
 Data Space Total: 102400.0 Mb
 Metadata Space Used: 8.3 Mb
 Metadata Space Total: 2048.0 Mb
Execution Driver: native-0.2
Kernel Version: 2.6.32-431.3.1.el6.x86_64
Operating System: <unknown>

docker version

Client version: 1.2.0
Client API version: 1.14
Go version (client): go1.3.1
Git commit (client): fa7b24f
OS/Arch (client): linux/amd64
Server version: 1.2.0
Server API version: 1.14
Go version (server): go1.3.1
Git commit (server): fa7b24f

uname -a

Linux vagrant-centos65.vagrantup.com 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

cat /etc/redhat-release

CentOS release 6.5 (Final)

谢谢你的建议

相关内容