running two nodejs apps in one docker image

how can i run two different nodejs apps in one docker image?
two different CMD [ "node", "app.js"] and CMD [ "node", "otherapp.js"] won’t work, cause there can be only one CMD directive in Dockerfile.

  • docker with postgres and bash
  • rg.apache.spark.SparkException: Invalid master URL: spark://tasks.501393358-spark-master:7077
  • bind docker containers services start on the specific port
  • How to install Tomcat in Docker Windows Server Container?
  • override default docker run from host with options/arguments
  • Dockerize Logstash, Redis setup
  • Troubles using docker-machine to setup Swarm
  • Running Travis locally using docker
  • Running composer install in docker container
  • Docker machine using generic driver: x509: certificate signed by unknown authority when trying to add remote host to local machine
  • How to get 'man' working in an Alpine Linux Docker container?
  • iptables not working on macvlan traffic in container
  • One Solution collect form web for “running two nodejs apps in one docker image”

    I recommend using pm2 as the entrypoint process which will handle all your NodeJS applications within docker image. The advantage of this is that pm2 can bahave as a proper process manager which is essential in docker. Other helpful features are load balancing, restarting applications which consume too much memory or just die for whatever reason, and log management.

    Here’s a Dockerfile I’ve been using for some time now:

    #A lightweight node image
    FROM mhart/alpine-node:6.5.0
    
    #PM2 will be used as PID 1 process
    RUN npm install -g pm2@1.1.3
    
    # Copy package json files for services
    
    COPY app1/package.json /var/www/app1/package.json
    COPY app2/package.json /var/www/app2/package.json
    
    # Set up working dir
    WORKDIR /var/www
    
    # Install packages
    RUN npm config set loglevel warn \
    # To mitigate issues with npm saturating the network interface we limit the number of concurrent connections
        && npm config set maxsockets 5 \
        && npm config set only production \
        && npm config set progress false \
        && cd ./app1 \
        && npm i \
        && cd ../app2 \
        && npm i
    
    
    # Copy source files
    COPY . ./
    
    # Expose ports
    EXPOSE 3000
    EXPOSE 3001
    
    # Start PM2 as PID 1 process
    ENTRYPOINT ["pm2", "--no-daemon", "start"]
    
    # Actual script to start can be overridden from `docker run`
    CMD ["process.json"]
    

    process.json file in the CMD is described here

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.