mahen3d
mahen3d

Reputation: 7724

Docker how to start nodejs app with redis in the Container?

I have simple but curious question, i have based my image on nodejs image and i have installed redis on the image, now i wanted to start redis and nodejs app both running in the container when i do the docker-compose up. However i can only get one working, node always gives me an error. Does anyone has any idea to

  1. How to start the nodejs application on the docker-compose up ?
  2. How to start the redis running in the background in the same image/container ?

My Docker file as below.

# Set the base image to node
FROM node:0.12.13

# Update the repository and install Redis Server
RUN apt-get update && apt-get install -y redis-server libssl-dev wget curl gcc

# Expose Redis port 6379
EXPOSE      6379
# Bundle app source
COPY ./redis.conf /etc/redis.conf

EXPOSE  8400

WORKDIR /root/chat/
CMD ["node","/root/www/helloworld.js"]
ENTRYPOINT  ["/usr/bin/redis-server"]

Error i get from the console logs is

[36mchat_1 | [0m[1] 18 Apr 02:27:48.003 # Fatal error, can't open config file 'node'

Docker-yml is like below

chat:
    build: ./.config/etc/chat/
    volumes:
        - ./chat:/root/chat
    expose:
        - 8400
    ports:
        - 6379:6379  
        - 8400:8400   
    environment:
        CODE_ENV: debug
        MYSQL_DATABASE: xyz
        MYSQL_USER: xyz
        MYSQL_PASSWORD: xyz        
    links:
        - mysql   
    #command: "true"  

Upvotes: 3

Views: 3687

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

A docker file can have but one entry point(either CMD or ENTRYPOINT, not both). But, you can run multiple processes in a single docker image using a process manager like systemd. There are countless recipes for doing this all over the internet. You might use this docker image as a base:

https://github.com/million12/docker-centos-supervisor

However, I don't see why you wouldn't use docker compose to spin up a separate redis container, just like you seem to want to do with mysql. BTW where is the mysql definition in the docker-compose file you posted?

Here's an example of a compose file I use to build a node image in the current directory and spin up redis as well.

web:
  build: .
  ports:
    - "3000:3000"
    - "8001:8001"
  environment:
    NODE_ENV: production
    REDIS_HOST: redis://db:6379
  links:
    - "db"
db:
  image: docker.io/redis:2.8

It should work with a docker file looking like the one you have minus trying to start up redis.

Upvotes: 2

Related Questions