To avoid re-installing node_modules during the docker build process, consider creating a base image that already includes them.
FROM node:18-alpine3.16
WORKDIR /usr/src/app
COPY package* ./
RUN npm install
docker build -t myimage .
You can tag and push this image to a registry like Dockerhub, or use it as your new base image locally.
FROM myimage
WORKDIR /usr/src/app #node_modules are already included here
COPY . . # copy your code inside the image, or map the folder if you are in development
RUN npm run build # perform a build if needed
CMD ["mycommand","myargument"]
Keep in mind that you will need to rebuild your base image whenever you add or update node modules.
Consider using 'npm ci' instead of 'npm install' to maintain consistent versions of your node modules with each installation.
Stay vigilant about security vulnerabilities in both your node modules and base images.
Periodically run docker scan myimage
to check for updates required by your node_modules or base images.
During development, it's perfectly acceptable to map your code folders with the docker image. You don't necessarily have to copy your code into the image while developing. Just map it to your WORKDIR.