我在 Windows 7 机器上使用 vagrant 和 docker,VirtualBox 作为 docker 主机 VM 的提供者。我有一个用docker
提供者定义的流浪项目,以便vagrant up
启动mongo
容器。我没有mongo
在 docker 主机 VM 中配置容器,因为我有时会想要mongo
,有时postgres
等等。
对于这种mongo
情况,我正在尝试针对这个正在运行的mongo
容器运行一些 mongo 集合初始化脚本,以使用 vagrant 设置索引等。除了之外,mongo
镜像不运行任何东西,对于容器也是如此,因此使用配置器来运行这些操作不是一个选项。mongod
has_ssh
false
我尝试在链接到容器 running 的单独容器中运行 mongo shell mongodb
,但要使其正常工作,我需要能够在命令在容器中运行时扩展mongo
环境变量(MONGO_PORT_27017_TCP_ADDR
和)。MONGO_PORT_27017_TCP_PORT
我一直在尝试使用 来执行此操作vagrant docker-run
,但我找不到正确解释命令以正确扩展这些环境变量的方法。每次尝试都会导致mongo
命令抱怨没有提供主机和端口(环境变量扩展为空)。
我最终希望能够做的是:
- 启动一个链接到正在运行的 mongo 容器的容器(因此它获取其 env 变量)
- 将 javascript 文件上传到包含我想做的 mongo 初始化的新容器
- 在容器上运行 shell 命令,如下所示:
mongo "$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/db-name" initialize.js
- 取出容器
我可以尝试任何建议或其他方法吗?
典型的失败命令示例
Powershell 是这个 Windows 系统上的命令行解释器:
PS C:\Users\hoobajoob\Documents\Eclipse\workspace-sts-luna\project\.environment\vagrant\mongo-provisioner> vagrant docker-run default -- mongo
'$MONGO_PORT_27017_TCP_ADDR:`$MONGO_PORT_27017_TCP_PORT/test' --eval "printjson(db.getCollectionNames())"
==> default: Docker host is required. One will be created if necessary...
default: Docker host VM is already ready.
==> default: Creating the container...
default: Name: mongo-provisioner_default_1449193166_1449193166
default: Image: mongo
default: Cmd: mongo $MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/test
default: Link: mongo-container:mongo
default:
default: Container is starting. Output will stream in below...
default:
default: MongoDB shell version: 3.0.7
default: 2015-12-04T01:38:20.037+0000 E QUERY Error: Missing host name in connection string ":/test"
default: at Error (<anonymous>)
default: at connect (src/mongo/shell/mongo.js:160:15)
default: at (connect):1:6 at src/mongo/shell/mongo.js:160
default: exception:
default: connect failed
default:
A Docker command executed by Vagrant didn't complete successfully!
The command run along with the output from the command is shown
below.
Command: "docker" "run" "--name" "mongo-provisioner_default_1449193166_1449193166" "--link" "mongo-container:mongo" "--rm=true" "mongo" "mongo" "$MONGO_POR
T_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/test" "--eval" "printjson(db.getCollectionNames())"
Stderr: exception: connect failed
Stdout: MongoDB shell version: 3.0.7
2015-12-04T01:38:20.037+0000 E QUERY Error: Missing host name in connection string ":/test"
at Error (<anonymous>)
at connect (src/mongo/shell/mongo.js:160:15)
at (connect):1:6 at src/mongo/shell/mongo.js:160
来源
码头主机Vagrantfile
:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.hostname = "docker-host"
config.vm.box_check_update = false
config.ssh.insert_key = false
config.vm.box = "williamyeh/ubuntu-trusty64-docker"
config.vm.network "forwarded_port", guest: 27017, host: 27017
config.vm.synced_folder ".", "/vagrant", disabled: true
end
mongod
容器Vagrantfile
:_
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker'
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provider "docker" do |docker|
docker.vagrant_vagrantfile = "../docker-host/Vagrantfile"
docker.image = "mongo"
docker.ports = ['27017:27017']
docker.name = 'mongo-container'
end
end
mongo shell
容器Vagrantfile
(这是我试图运行命令的容器,以便vagrant docker-run
链接到mongod
容器的 mongo 环境变量得到正确扩展):
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Specify Vagrant version and Vagrant API version
Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker'
# Create and configure the Docker container(s)
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provider "docker" do |docker|
docker.vagrant_vagrantfile = "../docker-host/Vagrantfile"
docker.image = "mongo"
docker.link("mongo-container:mongo")
end
end