0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Docker ComposeでExpressとRedisを接続できないとき

Posted at

出会ったエラー

Docker Compose使わないとできるのに、使うとこんなエラーが出ました。

Error: connect ECONNREFUSED 127.0.0.1:6379

TL;DR

docker-compose.ymlapp.jsはこんな感じにする

docker-compose.yml
version: '3'
services:
  express:
    build: .
    volumes:
      - .:/app
    tty: true
    ports:
      - 3000:3000
    environment:
      - NODE_ENV=development
      - PORT=3000
      - SESSION_SECRET=hogehoge
      - REDIS_HOST=redis_db
      - REDIS_PORT=6379
    depends_on:
      - redis_db
  
  redis_db:
    image: redis:latest
    ports:
      - 6379:6379
app.js
var session = require('express-session');
var RedisStore = require('connect-redis')(session)
var redis = require('redis')

var redisClient = redis.createClient({
  url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`
})

app.use(session({
  secret: process.env.SESSION_SECRET,
  store: new RedisStore({
    client: redisClient,
    prefix: 'sid:',
  }),
}))

ダメな例

ググった情報でこう書いてたけど、これはlocalhostでしか動きません。

app.js
var session = require('express-session');
var RedisStore = require('connect-redis')(session)
var redis = require('redis')

var redisClient = redis.createClient()

app.use(session({
  secret: process.env.SESSION_SECRET,
  store: new RedisStore({
    client: redisClient,
    host: process.env.REDIS_HOST,
    port: process.env.REDIS_PORT,
    prefix: 'sid:',
  }),
}))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?