【问题标题】:How to run Docker and node.js with remote configurations如何使用远程配置运行 Docker 和 node.js
【发布时间】:2015-11-28 19:44:05
【问题描述】:

我想为一个将配置文件的 URL 作为参数并使用该文件的开源应用程序提供一个简单易用的 Docker 容器。

Dockerfile 非常简单:

FROM phusion/baseimage
# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]

RUN curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
RUN apt-get update
RUN apt-get install -y nodejs git
ADD     . /src
RUN     cd /src; npm install; npm update
ENV NODE_ENV production
CMD     ["/usr/bin/node", "/src/gitevents.js"]

我发现在容器运行时(使用 ADD 或 ENTRYPOINT)无法添加文件,所以我试图在 node.js 中解决它:

docker run -e "CONFIG_URL=https://gist.githubusercontent.com/PatrickHeneise/c97ba221495df0cd9a3b/raw/fda1b8cd53874735349c6310a6643e6fc589a404/gitevents_config.js" gitevents

这会将CONFIG_URL 设置为我可以在节点中使用的环境变量。但是,我需要下载一个文件,它是异步的,在当前设置中不起作用。

if (process.env.NODE_ENV === 'production') {
  var exists = fs.accessSync(path.join(__dirname, 'common', 'production.js'), fs.R_OK);
  if (exists) {
    config = require('./production');
  } else {
    // https download, but then `config` is undefined when running the app the first time.
  }
}

node.js 中没有同步下载,有什么建议可以解决这个问题吗?

我希望 Docker 使用 ADDCMD 完成 curl 下载,但我不确定它是如何工作的?

【问题讨论】:

    标签: javascript node.js asynchronous docker config


    【解决方案1】:

    另一件事是考虑您的“配置文件”不是文件,而只是文本,并在运行时将内容传递给容器。

    CONFIG="$(curl -sL https://gist.githubusercontent.com/PatrickHeneise/c97ba221495df0cd9a3b/raw/fda1b8cd53874735349c6310a6643e6fc589a404/gitevents_config.js)"

    docker run -e "CONFIG_URL=${CONFIG}" gitevents

    【讨论】:

      【解决方案2】:

      ENTRYPOINT 和环境变量的组合怎么样?您将 Dockerfile 中的 ENTRYPOINT 设置为一个 shell 脚本,该脚本将下载环境变量中指定的配置文件,然后启动应用程序。 由于入口点脚本将接收 CMD 中的任何内容作为它的参数,因此应用程序启动步骤可以通过类似

      # Execute CMD.
      eval "$@"
      

      【讨论】:

      • 听起来很有趣。我会试试的。
      • 我用 curl 命令写了一个文件init.sh 然后运行节点,但感觉有点脏。通过 Dockerfile 中的 CMD 运行脚本。
      【解决方案3】:

      我设法重写了我的配置脚本以异步工作,但在我看来仍然不是最好的解决方案。

      var config = {};
      var https = require('https');
      var fs = require('fs');
      var path = require('path');
      
      config.load = function(fn) {
        if (process.env.NODE_ENV === 'production') {
          fs.access(path.join(__dirname, 'production.js'), fs.R_OK, function(error, exists) {
            if (exists) {
              config = require('./production');
            } else {
              var file = fs.createWriteStream(path.join(__dirname, 'production.js'));
              var url = process.env.CONFIG_URL;
      
              if (!url) {
                process.exit(-1);
              } else {
                https.get(url, function(response) {
                  response.pipe(file);
                  file.on('finish', function() {
                    file.close(function() {
                      return fn(require('./production'));
                    });
                  });
                });
              }
            }
          });
        } else if (process.env.NODE_ENV === 'test') {
          return fn(require('./test'));
        } else {
          return fn(require('./development'));
        }
      };
      
      module.exports = exports = config;
      

      【讨论】:

        猜你喜欢
        • 2017-08-21
        • 2020-09-08
        • 2023-04-04
        • 2021-03-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-10
        • 2023-02-06
        • 2014-08-31
        相关资源
        最近更新 更多