2015-11-28 18 views
0

我想为开源应用程序提供一个简单且简单的Docker容器,它将配置文件的URL作为参数并使用此文件。如何使用远程配置运行Docker和node.js

的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"] 

我没有发现添加文件时,容器中运行(与地址或入口点),所以我试图去解决它在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做一个卷曲下载的工作,但我不确定这是如何工作的?

回答

0

我设法重新编写我的配置脚本以使其异步工作,但仍然不是我眼中的最佳解决方案。

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; 
1

ENTRYPOINT和环境变量的组合如何?您将Dockerfile中的ENTRYPOINT设置为shell脚本,该shell脚本可以下载环境变量中指定的配置文件,然后启动该应用程序。 由于入口点脚本将获得无论是在CMD,因为它的参数,应用程序启动的步骤可以通过类似

# Execute CMD. 
eval "[email protected]" 
+0

听起来很有意思。我会尝试的。 – Patrick

+0

我用curl命令写了一个'init.sh'文件,然后运行节点,但是感觉有点脏。通过Dockerfile中的CMD运行脚本。 – Patrick

相关问题