Vagrant+Docker:容器启动后从未离开过"stopped"状态

Vagrant+Docker: The container started never left the "stopped" state

我正在尝试编写一个可部署的数据科学环境,该环境 运行 是 continuumio/anaconda3 Docker 容器中的 ubuntu/trusty64 Vagrant 盒子。这似乎 运行 很好,但是容器会立即关闭。按照文档 here 我尝试传递 d.rund.cmd 命令但没有成功。我希望能够执行相当于传递 Docker 命令

的 Vagrant
docker run -i -t -p 8888:8888 continuumio/anaconda3 /bin/bash -c "/opt/conda/bin/conda install jupyter -y --quiet && mkdir /opt/notebooks && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='*' --port=8888 --no-browser"

这样我就可以从 localhost:8888 访问笔记本(我已经通过遵循 this guide 对 Apache httpd 做了类似的事情)。不幸的是,我似乎无法获得正确的语法,我们将不胜感激。

我在下面包含了我的 HostVagrantfile 和 Vagrantfile。

HostVagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Specify Vagrant version, Vagrant API version, and Vagrant clone location
Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"

# Create and configure the VM(s)
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # Use Docker provisioner
  config.vm.provision "docker"

  # Workaround
  config.vm.provision "shell", inline:
     "ps aux | grep 'sshd:' | awk '{print }' | xargs kill"

  # Assign a friendly name to this host VM
  config.vm.hostname = "docker"

  config.vm.box = 'ubuntu/trusty64'

  config.vm.network "forwarded_port", guest: 8888, host: 8888

  config.vm.synced_folder ".", "/vagrant"
end

Vagrantfile:

# -*- 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.define "apache" do |a|

    a.vm.provider "docker" do |d|

      d.image = "continuumio/anaconda3"
      d.name = "anacond3-container"
      d.ports = ["80:80", "433:443", "8888:8888"]
      d.remains_running = true
      d.vagrant_vagrantfile = "HostVagrantfile"
    end
  end
end

更新 这是我尝试 运行:

时得到的结果

$ vagrant docker-run -i -t -p 8888:8888 continuumio/anaconda3 /bin/bash -c "/opt/conda/bin/conda install jupyter -y --quiet && mkdir /opt/notebooks && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='*' --port=8888 --no-browser"

An invalid option was specified. The help for this command
is available below.

Usage: vagrant docker-run [command...]

Options:

        --[no-]detach                Run in the background
    -t, --[no-]tty                   Allocate a pty
    -r, --[no-]rm,                   Remove container after execution
    -h, --help                       Print this help

最后自己解决了。需要打破 docker 运行 命令

docker run -i -t -p 8888:8888 continuumio/anaconda3 /bin/bash -c "/opt/conda/bin/conda install jupyter -y --quiet && mkdir /opt/notebooks && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='*' --port=8888 --no-browser"

在拉取图像后使用 vagrant docker 运行、args、cmd 命令。而不是使用

config.vm.provider "docker" do |d|
  d.image = "continuumio/anaconda3"
end

使用

config.vm.provision "docker" do |d|
  d.pull_images "continuumio/anaconda3"
  d.run "continuumio/anaconda3",
    args: "-t -p 8888:8888",
    cmd: $jupyterServer
end

并定义

$jupyterServer = <<SCRIPT
/bin/bash -c "/opt/conda/bin/conda install jupyter -y --quiet && mkdir /opt/notebooks && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='*' --port=8888 --no-browser"
SCRIPT

我还把两个Vagrantfile合二为一:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Specify Vagrant version, Vagrant API version
Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"

$jupyterServer = <<SCRIPT
/bin/bash -c "/opt/conda/bin/conda install jupyter -y --quiet && mkdir /opt/notebooks && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='*' --port=8888 --no-browser"
SCRIPT

# Create and configure the VM(s)
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "ubuntu/trusty64"

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--memory", "2048"]
    vb.customize ["modifyvm", :id, "--cpus", "2"]
  end

  # Use Docker provisioner
  config.vm.provision "docker" do |d|
    d.pull_images "continuumio/anaconda3"
    d.run "continuumio/anaconda3",
      args: "-t -p 8888:8888",
      cmd: $jupyterServer
  end

  config.vm.network "forwarded_port", guest: 8888, host: 8888
  config.vm.synced_folder ".", "/vagrant"
end