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
2
  • 1
    As redid to it? It's not clear what you're asking. Commented Apr 22, 2018 at 12:54
  • 1
    I am trying to have redis on the container that is running my node.js application. Commented Apr 22, 2018 at 13:06

3 Answers 3

18

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

Sign up to request clarification or add additional context in comments.

7 Comments

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
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
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
you have to use docker run -d -p 3001:3001 -p 6379:6379 testapp
Container links are a deprecated legacy feature of docker. Can you update this answer to use bridge or overlay networks instead?
|
4

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.