roman95
roman95

Reputation: 73

Dockerfile - Build docker image for multiple Node.js app

How do I run all my Node.js file in a single container?

Dockerfile

FROM node:latest
WORKDIR /rootfolder
COPY package.json ./
RUN npm install
COPY . .
RUN chmod +x /script.sh
RUN /script.sh

script.sh

#!/bin/sh
node ./app1.js
node ./app2.js
node ./app3.js
node ./app4.js

Upvotes: 2

Views: 7949

Answers (3)

Henrique Abreu
Henrique Abreu

Reputation: 1

Ideally you should run each app on a separated container, if your applications are different. In the case they are equal and you want to run multiple instances on different ports

docker run -p <your_public_tcp_port_number>:3000 <image_name> 

or a good docker-compose.yaml would suffice.

Technically you may want to run each different application on a different container and run multiple instances of the same application in order to make it easy to version each of your app on a newer independent image. It will allows you to independently stop, deploy and start your apps on the production environment.

Upvotes: -1

David Maze
David Maze

Reputation: 160041

You would almost always run these in separate containers. You're allowed to run multiple containers from the same image, you can override the default command for an image when you start it up, and you can remap the ports an application uses when you start it.

In your Dockerfile, delete the RUN /script.sh line at the end. (That will try to start the servers during the image build, which you don't want.) Now you can build and run containers:

docker build -t myapp .      # build the image
docker network create mynet  # create a Docker network
docker run \                 # run the first container...
  -d \                       #   in the background
  --net mynet \              #   on that network
  --name app1 \              #   with a known name
  -p 1001:3000 \             #   publishing a port
  myapp \                    #   from this image
  node ./app1.js             #   running this command
docker run \
  -d \
  --net mynet \
  --name app2 \
  -p 1002:3000 \
  myapp \
  node ./app2.js

(I've assumed all of the scripts listen on the default Express port 3000, which is the second port number in the -p options.)

Docker Compose is a useful tool for running multiple containers together and can replicate this functionality. A docker-compose.yml file matching this setup would look like:

version: '3.8'
services:
  app1:
    build: .
    ports:
      - 1001:3000
    command: node ./app1.js
  app2:
    build: .
    ports:
      - 1002:3000
    command: node ./app2.js

Compose will create a Docker network on its own, and take responsibility for naming the images and containers. docker-compose up will start all of the services in parallel.

Upvotes: 5

Jose Cabrera Zuniga
Jose Cabrera Zuniga

Reputation: 2637

You need to expose the ports first using:

EXPOSE 1001
...
EXPOSE 1004

in your dockerfile and later run the container using the -p parameter as with -p 1501:1001 to expose -for example- the port 1501 of the host to work as the 1001 port of the container.

ref: https://docs.docker.com/engine/reference/commandline/run/

However, it is suggested to minimize the number of programs to be run from a docker container. So, you might like to have a container for each of your js scripts.

Yet, Nothing stops you from using:

docker exec -it yourDockerMachineName bash

several times where you can use each of your node cmds.

Upvotes: -1

Related Questions