Vagrant + Docker:容器启动从未离开“停止”状态

时间:2016-09-25 20:52:57

标签: docker vagrant anaconda vagrantfile vagrant-windows

我正在尝试编写一个可部署的数据科学环境,在NullReferenceException Vagrant框中运行continuumio/anaconda3 Docker容器。这似乎运行正常,但容器立即关闭。在文档here之后,我尝试传递ubuntu/trusty64d.run命令,但没有成功。我希望能够做到Vagrant相当于传递Docker命令

d.cmd

这样我就可以从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" 访问笔记本了(我已经通过跟随this guide完成了与Apache httpd类似的事情)。不幸的是,我似乎无法获得正确的语法,任何帮助都将不胜感激。

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

HostVagrantfile:

localhost:8888

Vagrantfile:

# -*- 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 $2}' | 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

更新 这是我在尝试运行时得到的结果:

# -*- 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"

1 个答案:

答案 0 :(得分:0)

最终管理自己。需要打破docker run命令

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 run,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

我还将两个Vagrantfiles合并为一个:

# -*- 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