2

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

  • app1.js running on port 1001
  • app2.js running on port 1002
  • app3.js running on port 1003
  • app4.js running on port 1004

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
2
  • What is the problem though? Commented Aug 18, 2020 at 7:11
  • Check out the Docker Documentation for a recipe. Commented May 4, 2023 at 6:25

3 Answers 3

5

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.

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

2 Comments

Is there a reason you didn't answer the question?
How would you answer the equivalent question without Docker? "I have five separate Node applications; how do I run all of them in a single command?" The right answer for plain Node should be "run node appN.js five times" and not "pack them into a single multi-application process"; similar logic applies to containers.
-1

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.

Comments

-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.

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.