node.js无法使用docker-compose连接到Redis?

时间:2019-08-02 05:17:24

标签: node.js docker redis docker-compose

  • 我正在使用docker-compose部署一个nodejs应用。但是由于某些原因,即使我确实使用了depends_on,应用程序容器也无法连接到redis容器,在container应用程序上,我仍然可以对redis docker进行ping操作。但是pm2日志仍然显示它无法连接。
  • 然后当我分离connect redis代码并单独运行时,它仍然可以正常运行。我认为可能与其他模块发生冲突,因此我发布了package.json列表。

我的应用代码:

var redis = require('redis');
var client = redis.createClient();
const subscriber = redis.createClient();

var sessionMiddleware = session({
    secret: 'abc',
    store: new redisStore({ host: "redis-docker" ,url: "redis-docker", port: 
     "6379", client: client }),
    resave: false,
    saveUninitialized: true,
    cookie: { maxAge: 604800000 }
});

docker-compose文件:

docker-compose.yml
version: '3.7'
services:
  redis-docker:
    container_name: redis
    image: redis
    networks:
      - abc
    volumes:
      - redis-data:/data:rw
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - "6379:6379"
    hostname: redis-docker
    # command: ["redis-server", "--appendonly", "yes"]

  app:
    container_name: app
    image: app
    restart: always
    ports:
      - "4004:4004"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    env_file:
      - .env
    build: .
    depends_on:
      - redis-docker
    # command: ["npm", "start"]
    networks:
      - abc
    links:
      - redis-docker

volumes:
  redis-data:
networks:
  abc:

这是我使用过的所有模块:

node
{
  "name": "webphone-ui",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo 'OK'"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "abortcontroller-polyfill": "^1.1.8",
    "axios": "^0.16.2",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "babel-register": "^6.26.0",
    "bluebird": "^3.5.2",
    "body-parser": "^1.18.2",
    "connect-multiparty": "^2.1.0",
    "connect-redis": "^3.3.2",
    "cors": "^2.8.4",
    "cron": "^1.6.0",
    "dotenv": "^6.0.0",
    "dotenv-webpack": "^1.5.7",
    "express": "^4.16.2",
    "express-ip": "^1.0.3",
    "express-session": "^1.15.6",
    "firebase-admin": "^5.10.0",
    "form-data": "^2.3.1",
    "jsonwebtoken": "^8.2.0",
    "mime": "^1.6.0",
    "morgan": "^1.9.0",
    "node-schedule": "^1.3.1",
    "nodemailer": "^4.4.1",
    "react": "^15.6.2",
    "react-audio-player": "^0.6.4",
    "react-bootstrap-table": "^4.3.1",
    "react-dom": "^15.6.2",
    "react-helmet": "^5.2.0",
    "react-multilingual": "^1.5.0",
    "react-redux": "^5.0.6",
    "react-router": "3.0.5",
    "react-scroll": "^1.7.3",
    "redis": "^2.8.0",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0",
    "rotating-file-stream": "^1.3.4",
    "socket.io": "^2.2.0",
    "sweetalert": "^2.0.8",
    "uuid": "^3.1.0",
    "webpack": "^3.10.0",
    "webpack-dev-middleware": "^1.12.2",
    "webpack-hot-middleware": "^2.21.0",
    "winston": "^2.4.0"
  },
  "devDependencies": {}
}

如果有人以前通过此问题,请帮助我。我搜索并尝试了很多方法,但没有希望。 谢谢。

1 个答案:

答案 0 :(得分:-1)

更新

在进一步调查之前,似乎连接字符串无效。

var client = redis.createClient(6379,"redis-docker"); 
    app.use(session({
      secret: 'ThisIsHowYouUseRedisSessionStorage',
      name: '_redisPractice',
      resave: false,
      saveUninitialized: true,
      cookie: { secure: false }, // Note that the cookie-parser module is no longer needed
      store: new redisStore({ host: 'redis-docker', port: 6379, client: redisClient, ttl: 86400 }),
    }));

managing-node-js-express-sessions-with-redis

下一步,还有其他方法,可以尝试并肯定会起作用的一种方法是遵循这些步骤。

  • 创建网络docker network create abc
  • 然后检查docker inspect network abc
  • 从配置中获取IP

这是网络检查中的配置

 "IPAM": {
        "Driver": "default",
        "Options": {},
        "Config": [
            {
                "Subnet": "172.26.0.0/16",
                "Gateway": "172.26.0.1"
            }
        ]
    }

更新您的nodejs脚本

var redis = require('redis');
var client = redis.createClient(6379,"172.26.0.1");
client.on('error', function(err){
  console.log('Something went wrong ', err)
});
client.set('my test key', 'my test value', redis.print);
client.get('my test key', function(error, result) {
  if (error) throw error;
  console.log('GET result ->', result)
})

您撰写时不需要链接。

version: '3.7'
services:
  redis-docker:
    container_name: redis
    image: redis
    networks:
      - abc
    ports:
      - "6379:6379"
    hostname: redis-docker
    # command: ["redis-server", "--appendonly", "yes"]

  app:
    container_name: app
    image: app
    restart: always
    ports:
      - "4004:4004"
    env_file:
      - .env
    build: .
    command: tail -f /dev/null # or your command
    networks:
      - abc
volumes:
  redis-data:
networks:
  abc:
    driver: bridge

enter image description here

Docker网络

enter image description here