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

时间:2014-11-26 14:23:58

标签: redis vagrant docker

我想在Vagrant VM中运行一个运行Redis的Docker容器。我希望事情变得尽可能简单,因此配置最少。

一些研究表明:

因此设置应该非常简单,我希望只需连接点。所以我想出了这个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. VM启动。
  2. Docker已安装。
  3. Docker守护程序dockerd已启动。
  4. 下载并安装Redis dockerfile/redis的Docker容器。
  5. 容器已启动。
  6. 除了第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提供的docker配置程序。

    那么我错过了什么?

2 个答案:

答案 0 :(得分:2)

我认为你混淆了提供者和供应者。 Provisioner提供了配置VM的环境。对于Docker而言,这意味着您可以将容器添加到docker-host,以便以某种方式“配置”VM。其他供应商是“壳牌”,“厨师”或“木偶”。提供程序是生成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容器。由于我们强制容器的主机,如果它不可用,vagrant会自动启动它。您可以使用vagrant up docker-host --provider=virtualbox直接启动主机。一切开始后,您可以使用vagrant ssh docker-host进入docker-host。请注意,大多数Docker容器没有SSH,因此您无法使用vagrant ssh container来ssh它们。只要容器正在运行,您可以使用以下“技巧”来查看容器:

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

附加说明:

ENV['VAGRANT_DEFAULT_PROVIDER'] - 'docker' - 设置默认的提供者。对于Docker,您不必键入vagrant up --provider=docker。默认值为virtualbox

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

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

进一步阅读:

Vagrant Multi-Machine

Vagrant Docker Provisioner

Vagrant Docker Provider

答案 1 :(得分:2)

你的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

VM内部:

[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