2016-05-10 111 views

回答

1

在其README.mddocker-api gem狀態本文檔:

require 'docker' 

# Create a Container. 
container = Docker::Container.create('Cmd' => ['ls'], 'Image' => 'base') 

# Attach to the Container. Currently, the below options are the only valid ones. 
# By default, :stream, :stdout, and :stderr are set. 
container.attach(:stream => true, :stdin => nil, :stdout => true, :stderr => true, :logs => true, :tty => false) 
# => [["bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroot\nrun\nsbin\nselinux\nsrv\nsys\ntmp\nusr\nvar", []] 

# If you wish to stream the attach method, a block may be supplied. 
container = Docker::Container.create('Image' => 'base', 'Cmd' => ['find/-name *']) 
container.tap(&:start).attach { |stream, chunk| puts "#{stream}: #{chunk}" } 
stderr: 2013/10/30 17:16:24 Unable to locate find/-name * 
# => [[], ["2013/10/30 17:16:24 Unable to locate find/-name *\n"]] 

# If you want to attach to stdin of the container, supply an IO-like object: 
container = Docker::Container.create('Image' => 'base', 'Cmd' => ['cat'], 'OpenStdin' => true, 'StdinOnce' => true) 
container.tap(&:start).attach(stdin: StringIO.new("foo\nbar\n")) 
# => [["foo\nbar\n"], []] 

這是否幫助?我能問你爲什麼試圖使用docker-api?你不能只用docker volumes (-v)

+1

我真的不明白。您提供的示例中連接的本地音量在哪裏?我只能在這些示例中找到stdout和stdin重定向? – nino

2

當前README錯過了關於如何在容器中使用卷的部分。你應該罰款與以下命令與容器的文件夾中運行/foo

container = Docker::Container.create('Cmd' => %w[test -d /foo], 
    'Image' => 'debian:wheezy', 
    'Volumes' => {'/foo' => {}} 
) 

如果需要使用本地文件夾安裝,更新Volumes' => {'/foo' => '/local_foo'} 您可以參考測試用例:

container_spec.rb

+1

這不起作用。當我嘗試使用本地文件夾時,出現錯誤「json:無法將對象解組爲類型的Go值」。我也檢查了docker inspect,並且我看到當我使用選項-v時,它會在條目「Mounts」下添加一個條目,並且您提供的信息會在「Volumes」 – nino

+0

下執行此操作。 github.com/swipely/dockly/blob/master/lib/dockly/build_cache/docker.rb#L61? – BMW

+0

和這個:https://github.com/swipely/docker-api/issues/344 – BMW