如何用远程configuration运行Docker和node.js

我想为一个开源应用程序提供一个简单的Docker容器,它将一个configuration文件的URL作为参数并使用这个文件。

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设置为我可以在节点中使用的环境variables。 但是,我需要下载一个文件,这是asynchronous,哪种不能在当前的设置。

 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中没有同步下载,我有什么build议可以解决这个问题?

我很乐意让Docker用ADD或者CMD做一个curl下载的工作,但是我不知道这是如何工作的?

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

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

docker运行-e“CONFIG_URL = $ {CONFIG}”gitevents

如何组合ENTRYPOINT和环境variables? 您将ENTRYPOINT中的ENTRYPOINT设置为可以下载环境variables中指定的configuration文件的shell脚本,然后启动该应用程序。 由于入口点脚本会接收CMD中的任何参数,所以应用程序的开始步骤可以通过类似的方式来完成

 # Execute CMD. eval "$@" 

我设法重新写我的configuration脚本工作asynchronous,仍然不是我眼中最好的解决scheme。

 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;