1

I have a node project that has a web server and a service on the root.

--myNodeProj
  --app.js //the web server
  --service.js //an update service

In my package.json I have the following:

"scripts": {
    "start": "node app.js",
    "service": "node service.js"
  },

For my DockerFile I have:

FROM node:8

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "npm", "start" ]

The CMD will run the app.js (webserver). How do I build another container with the service? Do I create another Dockerfile? Would the docker build command look different?

1
  • you don't need another dockerfile, all you need to do is override default cmd and run service command while running your built image here is the official docs for it link Commented Mar 19, 2019 at 17:57

2 Answers 2

1

You can override the command - docker run <image> node service.js

https://docs.docker.com/engine/reference/run/#general-form

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

1 Comment

Thanks for the answer. I have posted an alternative solution too.
0

I ended up using docker-compose. You need to create a docker-compose.yml file with the following code:

version: '3'
services:
  web:
    # will build ./docker/web/Dockerfile
    build:
      context: .
      dockerfile: ./docker/web/Dockerfile
    ports:
      - "3000:3000"
    env_file:
      - web.env
  service:
    # will build ./docker/service/Dockerfile
    build:
      context: .
      dockerfile: ./docker/service/Dockerfile
    env_file:
      - service.env

This files reference 2 Dockerfiles that build the containers:

For service

FROM node:8

# Create app directory
WORKDIR /usr/src/service

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

CMD [ "node", "service.js" ]

For web:

FROM node:8

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

#EXPOSE 8080
CMD [ "npm", "start" ]

Notice that I can only do one NPM start. I call the service directly using node.

When I want to build containers, I issue the command:

docker-compose build

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.