Try   HackMD

Build a docker Image

Steps

  • Create a file in root directory of the project

  • Add in these contents

    • Base Image
      FROM node:14
    • Create app working directory
      WORKDIR /usr/src/app
    • Copy Dependencies
    ​​​​COPY package*.json ./
    ​​​​COPY yarn.lock ./
    
    • Install dependencies
      RUN npm install
    • Copy other source files
      COPY . .
    • Expose a port if required
      EXPOSE 8080
    • Run program
      CMD ["node","index.js"]
  • Should look like

# Base Image
FROM node:14

# 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 ./
COPY yarn.lock ./


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

# Bundle app source
COPY . .

# expose port
EXPOSE 8080

# last command to execute the node program `node index`
CMD [ "node", "index.js" ]
  • Create a .dockerignore file in the same directory
node_modules
npm-debug.log
  • Building image

Go to the directory that has your Dockerfile and run the following command to build the Docker image. The -t flag lets you tag your image so it's easier to find later using the docker images command:

docker build . -t <your username>/node-web-app

  • Check and list your docker images
docker images
  • Run your image
docker run -p 49160:8080 -d <your username>/node-web-app
  • Limit CPU or other resources
$ docker run --cpus=2 -m 512m <image>