11

I have a Docker image which is a node.js application. The app retrieves some configuration value from Redis which is running locally. Because of that, I am trying to install and run Redis within the same container inside the Docker image.

How can I extend the Docker file and configure Redis in it?

As of now, the Dockerfile is as below:

FROM node:carbon

WORKDIR /app

COPY package.json /app

RUN npm install

COPY . /app

EXPOSE 3011

CMD node /app/src/server.js

Benjamin
  • 3,499
  • 8
  • 44
  • 77

3 Answers3

17

The best solution would be to use docker compose. With this you would create a redis container, link to it then start your node.js app. First thing would be to install docker compose detailed here - (https://docs.docker.com/compose/install/).

Once you have it up and running, You should create a docker-compose.yml in the same folder as your app's dockerfile. It should contain the following

version: '3'
services:
  myapp:
    build: .  
    ports:
     - "3011:3011"
    links:
     - redis:redis
  redis:
    image: "redis:alpine"

Then redis will be accessible from your node.js app but instead of localhost:6379 you would use redis:6379 to access the redis instance.

To start your app you would run docker-compose up, in your terminal. Best practice would be to use a network instead of links but this was made for simplicity.

This can also be done as desired, having both redis and node.js on the same image, the following Dockerfile should work, it is based off what is in the question:

FROM node:carbon

RUN wget http://download.redis.io/redis-stable.tar.gz && \
    tar xvzf redis-stable.tar.gz && \
    cd redis-stable && \
    make && \
    mv src/redis-server /usr/bin/ && \
    cd .. && \
    rm -r redis-stable && \
    npm install -g concurrently   

EXPOSE 6379

WORKDIR /app

COPY package.json /app

RUN npm install

COPY . /app

EXPOSE 3011

EXPOSE 6379

CMD concurrently "/usr/bin/redis-server --bind '0.0.0.0'" "sleep 5s; node /app/src/server.js" 

This second method is really bad practice and I have used concurrently instead of supervisor or similar tool for simplicity. The sleep in the CMD is to allow redis to start before the app is actually launched, you should adjust it to what suits you best. Hope this helps and that you use the first method as it is much better practice

Clive Makamara
  • 2,845
  • 16
  • 31
  • Is there a way to have redis on the same container that is running the node.js App? so that I can access redis via 127.0.0.1:6379 – Benjamin Apr 22 '18 at 15:04
  • Yes but that will have you run into problems later on, let me edit my answer to give you an alternative – Clive Makamara Apr 22 '18 at 15:06
  • 1
    Updated the answer please have a look, and I hope you do indeed use the first solution, because when moving to production the redis connection string shouldn't be hard coded to localhost in the first place, because that's what seems to have been done -- just a guess though – Clive Makamara Apr 22 '18 at 15:25
  • Thanks, I can now see the Redis being installed on the same container, However when I use the Redis Client application to access that redis instance by providing the container IP and Redis port (6379), I can't connect to it. I have tried exposing 6379 in my Docker file also. – Benjamin Apr 23 '18 at 04:44
  • 1
    Maybe add the option --bind "0.0.0.0" to the redis-server command, I'll update the answer, this should allow you to expose the server if everything is working correctly – Clive Makamara Apr 23 '18 at 05:36
  • 1
    you have to use docker run -d -p 3001:3001 -p 6379:6379 testapp – Clive Makamara Apr 23 '18 at 05:55
  • 1
    Container links are a deprecated legacy feature of docker. Can you update this answer to use bridge or overlay networks instead? – John Jul 26 '19 at 18:34
2

My use case was to add redis server in alpine tomcat flavour:

So this worked:

FROM tomcat:8.5.40-alpine


RUN apk add --no-cache redis  
RUN apk add --no-cache screen 

EXPOSE 6379

EXPOSE 3011


## Run Tomcat
CMD screen -d -m -S Redis /usr/bin/redis-server --bind '0.0.0.0' && \
${CATALINA_HOME}/bin/catalina.sh run
EXPOSE 8080
Ankush
  • 499
  • 1
  • 7
  • 17
2

If you are looking for a bare minimum docker with nodejs and redis-server, this works :

FROM nikolaik/python-nodejs:python3.5-nodejs8

RUN apt-get update
apt-get -y install redis-server

COPY . /app
WORKDIR /app
nohup redis-server &> redis.log &

and then you can have further steps for your node application.

Dreams
  • 5,854
  • 9
  • 48
  • 71