skip to Main Content

is there any way i can use redis service for my nodejs, i’am using alpine docker and somehow my node-redis client is not able to connect to redis (it says "can not connect to 127.0.0.1:6479"), i’ve tried many thing but it is not working,
and i want to achieve by only docker file not docker-compose.
here is my docker file code

FROM alpine
RUN apk add --update nodejs npm redis
EXPOSE 6379 9000
WORKDIR /server
COPY . /server/
RUN npm install
RUN redis-server &
CMD npm start

2

Answers


  1. You could run multiple commands in one Docker container, but that’s a bad practice. You could do this by having your npm start run a script that runs both your server and Redis, or using a package like concurrently or npm-run-all. Docker good practice is to have one executable per container, though, so the Docker-correct thing to do would be to put Redis in a separate container.

    Login or Signup to reply.
  2. Your issue is RUN redis-server &. RUN statements are run at build time, so when your container starts up, the redis-server process isn’t there anymore. You need to start it at run-time on the CMD statement like this

    CMD redis-server & npm start
    

    But as Zac points out, the ‘docker way’ to do it is to run Redis is a separate container. There are ready made redis images on docker hub you can use.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search