在docker容器中使用express的req-flash会话问题

时间:2016-05-01 11:00:44

标签: node.js express docker

我有一个快速的应用程序,我使用redis进行会话。我使用最新的redis Docker映像并将其连接到隔离的docker bridge网络。

如果我构建应用程序的Docker镜像并启动它(将其连接到与redis服务器相同的桥接网络并将其暴露出来),我会遇到问题(如下所示,错误,如下所示)和req-flash到主机的端口。)

如果我执行到app容器,我可以使用桥接网络正常连接到Redis容器。

主机' redis-cache'是redis容器的名称。

如果我没有使用Docker作为应用程序,应用程序会按预期运行,但仍然使用redis容器。

我也遇到过与docker-compose相同的问题。

我使用vagrant作为主机,使用ubuntu / trusty64。 节点的版本是4.4.3(主机和泊坞窗映像)。 Docker的版本是1.11.1。

Dockerfile(我通常使用node:4-onbuild,但正在尝试别的东西):

FROM ubuntu
EXPOSE 3000
ADD . /app
WORKDIR /app
RUN apt-get update -y && apt-get install curl -y && curl -sL https://deb.nodesource.com/setup_4.x | bash - \
&& apt-get install -y nodejs && npm install --verbose
CMD npm start

带有req-flash的应用程序的一个设计示例:

var express = require('express');
var http = require('http');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var redis = require('redis');
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var client = redis.createClient();
var flash = require('req-flash');

var PORT = '3000';
var app = express();
var redisSessionStore = new redisStore({host: 'redis-cache', port: 6379, client: client,ttl :  260});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
    secret: 'mySecret',
    store: redisSessionStore,
    saveUninitialized: false,
    resave: false
}));

app.use(flash({locals: 'flash'}));

app.get('/test', function(req, res, next) {
    req.flash('testMessage', 'testing message');
    res.redirect('/');
});

app.get('/', function(req, res, next) {
    res.send(req.flash());
});

var server = http.createServer(app).listen(PORT);

Uppon访问' / test':

Error: Sessions are required.
  at flash (/app/node_modules/req-flash/index.js:22:26)
  at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
  at trim_prefix (/app/node_modules/express/lib/router/index.js:312:13)
  at /app/node_modules/express/lib/router/index.js:280:7
  at Function.process_params (/app/node_modules/express/lib/router/index.js:330:12)
  at next (/app/node_modules/express/lib/router/index.js:271:10)
  at session (/app/node_modules/express-session/index.js:159:61)
  at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
  at trim_prefix (/app/node_modules/express/lib/router/index.js:312:13)
  at /app/node_modules/express/lib/router/index.js:280:7

Redis容器运行命令:

docker run -d --name redis-cache --net=iso_net_0 redis

App container run命令:

docker run --name my-app-cont --net=iso_net_0 -p 3000:3000 myapp

docker ps的输出:

CONTAINER ID        IMAGE               COMMAND                     CREATED              STATUS              PORTS                    NAMES
9c14402243ee        myapp               "/bin/sh -c 'npm star"   About   a minute ago   Up About a minute   0.0.0.0:3000->3000/tcp   my-app-cont
57d79b87c3c4        redis               "docker-entrypoint.sh"   6 minutes ago        Up 6 minutes        6379/tcp                 redis-cache

docker network ls:

NETWORK ID          NAME                DRIVER
1e85a3458e9c        bridge              bridge              
9f6db289f96a        host                host                
7fdb5e3d4b83        iso_net_0           bridge              
1eb87fe46785        none                null

iso_net_0是我创建的桥接隔离网络

docker network inspect iso_net_0:

[
{
    "Name": "iso_net_0",
    "Id": "7fdb5e3d4b83afe7fe53122c1c8486b0d00c092afcb8a72dd44d62c875187bd9",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
        "Driver": "default",
        "Options": {},
        "Config": [
            {
                "Subnet": "172.18.0.0/16",
                "Gateway": "172.18.0.1/16"
            }
        ]
    },
    "Internal": false,
    "Containers": {
        "4588028f5a56f10258336b6dc4501e1c99b0145a4163473d76da4816062cb44c": {
            "Name": "my-app-cont",
            "EndpointID": "923ff4054a7c39cfc9c11ee3d49a810efadc504611205b6ce3db4c359190ded2",
            "MacAddress": "02:42:ac:12:00:03",
            "IPv4Address": "172.18.0.3/16",
            "IPv6Address": ""
        },
        "b5fcd1fa987e6a5fa4c3280c77a72f01ad23163fa56c5d3fd04ef75f15b67b6a": {
            "Name": "redis-cache",
            "EndpointID": "9cec94f3587048b9b6f8ac658cfa067f9684c2ddc48ec3d5531299b1e3dc71e9",
            "MacAddress": "02:42:ac:12:00:02",
            "IPv4Address": "172.18.0.2/16",
            "IPv6Address": ""
        }
    },
    "Options": {},
    "Labels": {}
}
]

0 个答案:

没有答案