在Docker + Vagrant中安装Redis最简单的方法是什么?

我想有一个运行Redis的Docker容器,在一个Vagrant VM里面。 我希望事情尽可能简单,只需要最less的configuration。

一点研究表明:

  • Vagrant提供了一个Dockerconfiguration器
  • Docker 为Redis提供了一个可信的Dockerfile

所以设置应该是非常简单的,我希望只需要连接点。 所以我想出了这个Vagrantfile

 VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "chef/centos-6.5" config.vm.provision "docker" do |docker| docker.run "dockerfile/redis" end end 

看起来很简单 – 很好。 现在,当我跑上vagrant up我会期待:

  1. 虚拟机启动。
  2. Docker被安装。
  3. Docker守护进程dockerd启动。
  4. 下载并安装Redis dockerfile/redis的Docker容器。
  5. 容器启动。

所有似乎工作,除了第3步,因为有了这个Vagrantfile我得到:

 ==> default: Verifying vmnet devices are healthy... ==> default: Preparing network adapters... ==> default: Starting the VMware VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 192.168.249.190:22 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection refused. Retrying... ==> default: Machine booted and ready! ==> default: Forwarding ports... default: -- 22 => 2222 ==> default: Configuring network adapters within the VM... ==> default: Waiting for HGFS kernel module to load... ==> default: Enabling and configuring shared folders... default: -- /Users/ernst/nikon/nikon-elk-vm2: /vagrant ==> default: Running provisioner: docker... default: Installing Docker (latest) onto machine... default: Configuring Docker to autostart containers... ==> default: Starting Docker containers... ==> default: -- Container: dockerfile/redis The following SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed! rm -f /var/lib/vagrant/cids/cc3cfcdebad9167099d85261c6311a0011838655 docker run --cidfile=/var/lib/vagrant/cids/cc3cfcdebad9167099d85261c6311a0011838655 -d --name dockerfile-redis dockerfile/redis Stdout from the command: Stderr from the command: 2014/11/26 12:38:40 Cannot connect to the Docker daemon. Is 'docker -d' running on this host? 

我显然可以恢复使用shell提供程序,但我很想保持简单的Vagrantfile并使用Vagrant提供的Vagrantfile provisioner。

那么我错过了什么?

你的Vagrantfile是正确的。

这似乎是chef/centos-6.5盒子的问题。

在这个框中,你必须先更新device-mapper-libs

我用这个Vagrantfile成功了:

 VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "chef/centos-6.5" config.vm.provision "shell", inline: "yum update -y device-mapper-libs" config.vm.provision "docker" do |docker| docker.run "redis" end end 

虚拟机内部:

 [vagrant@localhost ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7d55185f859c redis:2 "/entrypoint.sh redi 10 seconds ago Up 9 seconds 6379/tcp redis 

我想你是困惑的提供者和提供者。 Provisioner提供了configurationVM的环境。 在Docker的情况下,这意味着你可以添加容器到docker-host,这样可以用某种方式“configuration”VM。 其他供应商是“壳牌”,“厨师”或“木偶”。 提供者是产生虚拟机的工具,在这里是容器。 其他提供者是例如“VirtualBox”或“AWS”。

这里有一些代码可以帮助你开始:

 ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker' Vagrant.configure(2) do |config| config.vm.define "docker-host", autostart: false do |host| host.vm.hostname = "docker-host" host.vm.box = "chef/centos-6.6" # The following line terminates all ssh connections. Therefore # Vagrant will be forced to reconnect. # That's a workaround to have the docker command in the PATH host.vm.provision "shell", inline: "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill" host.vm.provision "docker" end host.vm.define 'container', primary: true do |ws| ws.vm.hostname = 'container' ws.vm.provider "docker" do |d| d.image = "library/redis:latest" d.vagrant_machine = "dev-host" d.vagrant_vagrantfile = "./Vagrantfile" d.force_host_vm = true end end end 

输出:

 $ vagrant up container Bringing machine 'container' up with 'docker' provider... ==> container: Docker host is required. One will be created if necessary... container: Vagrant will now create or start a local VM to act as the Docker container: host. You'll see the output of the `vagrant up` for this VM below. container: container: Importing base box 'chef/centos-6.6'... container: Matching MAC address for NAT networking... container: Checking if box 'chef/centos-6.6' is up to date... container: Setting the name of the VM: dev-host container: Clearing any previously set network interfaces... container: Preparing network interfaces based on configuration... container: Adapter 1: nat container: Forwarding ports... container: 22 => 2222 (adapter 1) container: Running 'pre-boot' VM customizations... container: Booting VM... container: Waiting for machine to boot. This may take a few minutes... container: SSH address: 127.0.0.1:2222 container: SSH username: vagrant container: SSH auth method: private key container: Warning: Connection timeout. Retrying... container: Warning: Connection timeout. Retrying... container: Warning: Remote connection disconnect. Retrying... container: Warning: Remote connection disconnect. Retrying... container: Warning: Remote connection disconnect. Retrying... container: Warning: Remote connection disconnect. Retrying... container: Warning: Remote connection disconnect. Retrying... container: Warning: Remote connection disconnect. Retrying... container: Warning: Remote connection disconnect. Retrying... container: Warning: Remote connection disconnect. Retrying... container: Machine booted and ready! container: Checking for guest additions in VM... container: Setting hostname... container: Mounting shared folders... container: /vagrant => <PATH> container: /tmp/vagrant-cache => <PATH> container: Configuring cache buckets... container: Running provisioner: docker... container: Installing Docker (latest) onto machine... container: Configuring Docker to autostart containers... container: Configuring cache buckets... container: Running provisioner: shell... container: Running: inline script container: Configuring cache buckets... ==> container: Syncing folders to the host VM... container: Mounting shared folders... container: /var/lib/docker/docker_1417023698_82709 => <PATH> ==> container: Warning: When using a remote Docker host, forwarded ports will NOT be ==> container: immediately available on your machine. They will still be forwarded on ==> container: the remote machine, however, so if you have a way to access the remote ==> container: machine, then you should be able to access those ports there. This is ==> container: not an error, it is only an informational message. ==> container: Creating the container... container: Name: virtual-dev_container_1417023701 container: Image: library/redis:latest container: Volume: /var/lib/docker/docker_1417023698_82709:/vagrant container: container: Container created: 2f020d26797f3bd0 ==> container: Starting container... ==> container: Provisioners will not be run since container doesn't support SSH. 

用法:

使用vagrant up启动redis容器。 因为我们强迫容器的stream浪者会自动启动它,如果它不可用。 你可以通过vagrant up docker-host --provider=virtualbox直接启动主机。 一切开始后,您可以通过使用vagrant ssh docker-host 。 请注意,大多数Docker容器没有SSH,因此您无法使用vagrant ssh container 。 只要容器正在运行,你可以做什么来查看容器是使用以下“技巧”:

  • SSH进入docker主机
  • 使用docker ps复制容器的ID
  • 使用docker exec -it <ID-of-container> /bin/bash
  • 现在你在没有SSH的容器上

补充笔记:

ENV['VAGRANT_DEFAULT_PROVIDER'] - 'docker' – 设置默认提供者。 在Docker的情况下,你不必inputvagrant up --provider=docker 。 默认是virtualbox

d.vagrant_vagrantfile = "./Vagrantfile" – 设置docker主机的Vagrantfile的位置。 因此,您可以为每个容器拥有一个自己的Vagrantfile! 无论如何,这个例子使用了多机器结构[ 1 ]。

您可以将docker-hostcontainer添加到您的命令中。 例如vagrant up docker-host --provider=virtualbox

进一步阅读:

stream浪者多机

Vagrant Docker Provisioner

stream浪docker提供商