基本上,我尝试使用mongodump和mongorestore在使用Docker将其启动后初始化mongo数据库。 mongorestore失败,因为它无法连接到容器的主机名。任何帮助,将不胜感激。我已尝试过localhost
,但它也无法正常工作。
我的Dockerfile:
FROM mongo
RUN mongodump --host merchdev02.bcinfra.net --port 27017 --db ap
RUN mongorestore --host $(hostname) --port 27017 --db ap
错误:
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" "build" "/mnt/docker_build_52c7cc3c5608022927f471820455e02b"
Stderr: Sending build context to Docker daemon 26.62 kB
Sending build context to Docker daemon
2014/09/12 12:32:46 The command [/bin/sh -c mongorestore --host $(hostname) --port 27017 --db ap] returned a non-zero code: 255
Stdout: Step 0 : FROM mongo
---> 37ee18fca8d2
Step 1 : RUN mongodump --host merchdev02.bcinfra.net --port 27017 --db ap
---> Using cache
---> 176612e279d2
Step 2 : RUN mongorestore --host $(hostname) --port 27017 --db ap
---> Running in 365bd8348ed1
2014-09-12T12:32:46.444+0000 warning: Failed to connect to 172.17.0.25:27017, reason: errno:111 Connection refused
couldn't connect to [5d533df6cbc4:27017] couldn't connect to server 5d533df6cbc4:27017 (172.17.0.25), connection attempt failed
我使用Vagrant来引导Docker。我的Vagrantfile:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
# Setup docker
config.vm.define "mongo" do |v|
v.vm.synced_folder "logs", "/var/log/mongodb"
v.vm.provider "docker" do |d|
d.vagrant_vagrantfile = "../host-vm/Vagrantfile"
d.build_dir = "."
d.ports = ['27017:27017']
d.remains_running = true
d.name = "mongo"
end
end
end
最后我的主机VM用于Docker容器:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision "docker"
config.vm.provision "shell", inline:
"ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill"
config.vm.network :forwarded_port, guest: 27017, host: 27017
end
答案 0 :(得分:1)
mongo容器将mongod设置为其命令CMD["mongod"]
,这意味着它在您实际执行docker run ...
之前不会运行
我通常总是在我的docker容器中包含一个脚本ADD init.sh
,以处理在构建时没有意义的任何此类配置任务。