【问题标题】:Installing docker containers via chef with staged files通过带有暂存文件的厨师安装 docker 容器
【发布时间】:2015-01-06 00:36:18
【问题描述】:

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

这是我的场景。我有一组容器来托管我的应用程序的组件。我有一个工作流程,基本上在各种环境中连续部署这些容器中的每一个,每个环境都有不同的设置(例如,Dev 将使所有容器都存在于一个 VM 上,但对于 Staging/Prod,容器将分散在各种 VM/硬件上)。

我正在尝试使用 chef 通过刀部署这些容器,然后在 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"]

我的食谱的 default.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

我知道我可能在做一些非常愚蠢的事情,所以任何帮助都将不胜感激!

附:我意识到在部署 Dockerfiles 时还有其他几个选项,但我真的在尝试使用 chef,因为除了部署和启动容器之外,我还想使用几本食谱来准备我的环境。

【问题讨论】:

    标签: chef-infra docker


    【解决方案1】:

    只是在这里发布,以防其他人遇到此问题。发生这种情况是因为我在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      相关资源
      最近更新 更多