You are the Node.js developer for the "Dog vs. Cat voting app" project. You are given a basic Dockerfile and the source code for the "result" Node.js app.
Goal: take the Dockerfile in this directory and make it the ULTIMATE for a combination of local development, production, and testing of the "result" app using all the things you learned in this section.
- Create a multi-stage Dockerfile that supports specific images for production, testing, and development.
- devDependencies should not exist in production image.
- Use
npm cito install production dependencies. - Use Scenario 1 for setting up node_modules (the simple version).
- Set NODE_ENV properly for dev and prod.
- The dev stage should run (CMD) nodemon from devDependencies. Either by updating the
$PATHor hard-coding the path to nodemon. - Edit docker-compose.yml to target the dev stage.
- Add LABELS from OCI standard (values are up to you) to all images.
- Add
npm config listoutput before runningnpm install. - Create a test stage that runs
npm audit. ./testsdirectory should only exist in test image.- Healthchecks should be added for production image.
- Prevent repeating costly commands like npm installs or apt-get if possible.
- Only
COPY . .source code once, thenCOPY --fromto get it into other stages.
- Add a security scanner to test stage and test it. Use Trivy (the modern replacement for deprecated microscanner). See Trivy documentation.
- Add Best Practices from an earlier section, including:
- Enable BuildKit and try a build.
- Add tini to images so containers will receive shutdown signals.
- Enable the non-root Node.js user for all dev/prod images.
- You might need root user for test or scanning images depending on what you're doing (test and find out!)
- Build all stages as their own tag.
ultimatenode:testshould be bigger thenultimatenode:prod - All builds should finish.
- Run dev/test/prod images, and ensure they start as expected.
docker-compose upshould work and you can vote athttp://localhost:5000and see results athttp://localhost:5001.- Ensure prod image doesn't have unnecessary files by running
docker run -it <imagename>:prod bashand checking it:- ls contents of
/app/node_modules/.bin, it should not containnodemonor devDependencies. - ls contents of
/appin prod image, it should not contain./testsdirectory.
- ls contents of
Good Luck!