A basic docker script for a node.js app:


# Mandatory, every dockerfile must be based on another image
FROM node:16

  

# This sets the current working dir, all further action will be made relative to this path
WORKDIR /app

  

# We copy the package.json and install before deps first for performance reasons.
# This is the longest step so dependencies are cache for subsequent steps
COPY package.json.
RUN npm install


# We can now copy the rest of the source code across, we don't need re-run npm install as dependencies are installed already

# '. .' looks kinda confusing. This just means copy everything from dev working dir to the docker working dir

COPY . .

# This doesnt actually do anything and acts more as documentation that port 3000 is being exposed and can be port forwarded for external access
EXPOSE 3000
  

# run the bash commands that will start the app

CMD ['node', "index.js"]
docker build . -t node-app-image

. is the path of where the Dockerfile is -t the name of of the image

to run a built image

docker run -p 3000:3000 -d --name node-app node-app-image

docker-basics