Docker日志没有显示颜色(express + nodejs图像)

时间:2015-09-21 12:12:04

标签: node.js docker

一个小而有趣的问题(对我来说):

我试图从我写的小服务器(nodejs + express)创建docker镜像。我的服务器代码是:

var express = require('express');
var Inflector = require('inflected');
var colors = require('colors');

var app = express();

app.get('/hello/:name', function(req, res, next){
    var name = Inflector.titleize(req.params.name);
    console.log("Saying hello to " + name.yellow);
    res.send('Hello ' + name);
});

var port = 9090;
app.listen(port, function(){
    console.log(('App is running on port ' + port).inverse);
});

我使用此Dockerfile创建我的图像:

FROM centos:centos6

RUN     rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

RUN     yum install -y npm

COPY . /src

RUN cd /src; npm install

EXPOSE 9090

CMD ["node", "/src/index.js"]

使用常用命令构建和运行映像:

docker build -t username:centos-nodejs
docker run -p 9090:9090 username:centos-nodejs

我希望日志在命令行中显示颜色,因为它们没有docker(例如node index.js)。

原因是什么?我可以解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

不是这个特定问题的答案,但是如果您正在使用调试库并且遇到同样的问题,那么有一个未记录的环境变量即使在非TTY中也能启用颜色:

https://github.com/visionmedia/debug/blob/39ecd87bcc145de5ca1cbea1bf4caed02c34d30a/node.js#L45

因此,将 07-21 20:46:10.602 8141-8189/com.example.ganesha.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4 07-21 20:46:10.637 8141-8189/com.example.ganesha.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented 07-21 20:46:10.637 8141-8189/com.example.ganesha.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad154700, error=EGL_SUCCESS 07-21 20:46:19.300 8141-8141/com.example.ganesha.myapplication E/SQLiteLog: (1) no such table: Kailash 07-21 20:46:19.304 8141-8141/com.example.ganesha.myapplication D/AndroidRuntime: Shutting down VM 07-21 20:46:19.306 8141-8141/com.example.ganesha.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ganesha.myapplication, PID: 8141 android.database.sqlite.SQLiteException: no such table: Kailash (code 1): , while compiling: SELECT Thursday FROM Kailash at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316) at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202) at com.example.ganesha.myapplication.HostelierDbHelper.query(HostelierDbHelper.java:137) at com.example.ganesha.myapplication.MainActivity$1.onClick(MainActivity.java:83) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 添加到环境变量会修复调试库颜色。

答案 1 :(得分:1)

您需要使用“-it”选项运行容器:

docker run -it -p 9090:9090 username:centos-nodejs