How does Dockerfiler use multiple CMD commands

  docker, question

CMD is used to run npmstart (port 3000) and node./server/server.js (port 8080) in Dockerfile.

Dockerfile:

FROM node:8.9-alpine
 
 ENV NODE_ENV production
 
 WORKDIR /usr/src/app
 
 COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
 
 RUN npm install --production --silent && mv node_modules ../
 
 COPY . .
 
 CMD [ "npm", "start", "node", "./server/server.js" ]

Only 3000 ports can be opened locally

Project directory:
图片描述

docker-compose.yml:

version: '2.1'
 
 services:
 chat:
 image: chat
 container_name: chat
 build: .
 environment:
 NODE_ENV: production
 ports:
 - "3000:3000"
 - "8080:8080"
 volumes:
 - ./:/usr/src/app
 links:
 - mongo
 mongo:
 container_name: mongo
 image: mongo
 ports:
 - "27017:27017"

There are two ways. One is CMD does not use brackets to frame the commands and links them with “&&”symbols.

# Use nohup box, otherwise npm start will not execute the following after it is executed
 CMD nohup sh -c 'npm start && node ./server/server.js'

Another method is to use the ENTRYPOINT command instead of CMD to specify an executed shell script, and then write the command to be executed in the entrypoint.sh file:

ENTRYPOINT ["./entrypoint.sh"]

Sh file is as follows:

// entrypoint.sh
 nohup npm start &
 nohup node ./server/server.js &

I hope I can help you.