通过带有分阶段文件的厨师安装docker容器

时间:2014-11-09 18:03:34

标签: chef docker

所以我几天来一直在努力解决这个问题,但我觉得这应该是一个简单的解决方法(这可能是我对厨师或厨师码头食谱的误解)。

这是我的情景。我有一组容纳我的应用程序组件的容器。我有一个工作流程,基本上可以在各种环境中持续部署每个容器,每个容器都有不同的设置(例如,Dev将所有容器都放在一个VM上,但对于Staging / Prod,容器会分散在各种VM /硬件上)。 / p>

我正在尝试使用厨师通过刀片部署这些容器,然后在VM上运行一组配方。我的问题是我无法弄清楚如何为Dockerfile的COPY命令部署暂存文件。这是我正在使用的Dockerfile示例。 COPY命令应该将package.json和index.js文件复制到/ src目录并执行npm。

FROM    ubuntu:14.04
# Install dependencies and nodejs
RUN apt-get update -y
RUN apt-get install -y python-software-properties python g++ make curl
RUN curl -sL https://deb.nodesource.com/setup | sudo bash -
RUN apt-get update -y
RUN apt-get install -y build-essential nodejs
# Bundle app source
COPY . /src
# Install app source
RUN cd /src; ls; npm install
EXPOSE  8080
CMD ["node", "/src/index.js"]

我的食谱默认.rb看起来像这样:

include_recipe 'docker'
docker_image_build_file = '/tmp/docker_image_build.dockerfile'
cookbook_file "/etc/chef/package.json" do
  source 'package.json'
  action :create_if_missing
end
cookbook_file "/etc/chef/index.js" do
  source 'index.js'
  action :create_if_missing
end
cookbook_file docker_image_build_file do
  source 'Dockerfile'
end
docker_image 'node' do
  tag 'nodeTag'
  source docker_image_build_file
  action :build
end
docker_container 'node' do
  detach true
  port '8080:8888'
end

我试图在很多地方播放文件,包括docker / chef /目录,chef /目录和files / default /目录。什么都没有被复制。 COPY命令后,我的docker文件中的“ls”调用只打印“Dockerfile”,显然npm安装失败,因为它在/ src目录中找不到package.json:

<server> Step 14 : RUN cd /src; ls; npm install
<server>  ---> Running in 5f1dba1fd0cf
<server> Dockerfile
<server> npm ERR! install Couldn't read dependencies
<server> npm ERR! package.json ENOENT, open '/src/package.json'
<server> npm ERR! package.json This is most likely not a problem with npm itself.
<server> npm ERR! package.json npm can't find a package.json file in your current directory.
<server> 
<server> npm ERR! System Linux 3.13.0-37-generic
<server> npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install"
<server> npm ERR! cwd /src
<server> npm ERR! node -v v0.10.33
<server> npm ERR! npm -v 1.4.28
<server> npm ERR! path /src/package.json
<server> npm ERR! code ENOPACKAGEJSON
<server> npm ERR! errno 34
<server> npm ERR! 
<server> npm ERR! Additional logging details can be found in:
<server> npm ERR!     /src/npm-debug.log
<server> npm ERR! not ok code 0

我知道我可能会做一些非常愚蠢的事情,所以任何帮助都会受到赞赏!

P.S。我意识到在部署Dockerfiles时还有其他几个选项,但我真的很想利用厨师,因为除了部署和启动容器之外,我想用几本烹饪书来准备我的环境。

1 个答案:

答案 0 :(得分:0)

只是在这里发布,以防其他人遇到此问题。发生这种情况是因为我在docker_image中的源实际上应该是一个指向/ tmp的目录,我复制了那里的所有文件。这是我更新的(工作)食谱:

include_recipe 'docker'
docker_node_data = '/tmp/docker'
directory docker_node_data do
  action :create
end 
cookbook_file "#{docker_node_data}/package.json" do
  source 'package.json'
  action :create_if_missing
end
cookbook_file "#{docker_node_data}/Dockerfile" do
  source 'Dockerfile'
end
docker_image 'example/node' do
  tag 'latest'
  source docker_node_data
  action :build
end
docker_container 'example/node' do
  detach true
  port '8080:8080'
  action :run
end