Moving to containers part 2: Redis & mongo

Installing and setting up Redis is a breeze, you just follow the instructions here, once done, create a redis container for your app using dokku redis:create then push it’ll automatically be linked to your app.

Inside your app a new enviroment variable will appear, REDIS_URL which will be in the format redis://:.

I’m using redis inside the sample app for session storage but it expects host and port to be seperate, like so

	app.use(express.session({
		store: new RedisStore({
			host: <Host>
			port: <Port>
		}),
		secret: 'THIS_APP_ROCKX'
	}))

Since good DevOps 12 factor practices require you using environment variables forconfiguration, what I did was use a regex to parse out the information like so

//The dokku redis plugin insists on putting the entire redis url in the ENV
	//So we'll parse it here 
	var REDIS_URL = process.env.REDIS_URL.match(new Regexp(/^redis:\/\/(.*):(\d*)$/));

	//Configure the redis session manager
	app.use(express.session({
		store: new RedisStore({
			host: REDIS_URL[1],
			port: REDIS_URL[2]
		}),
		secret: 'THIS_APP_ROCKX'
	}))

And there we go, redis will be configured via environment variables.

Moving along, I also want to install MongoDB, so I installed the Dokku mongodb plugin from here

Getting that working followed a similar process. I built a connection string from the environment variables and passed it to mongoose.connect() and I was off to the races.

In the next part I’ll talk more about how I test the software.