Utilizing Docker for Containerizing Node.js Applications
Steps for containerizing Node.js applications using Docker to ensure consistency across development and production environments.
0 likes
147 views
Rule Content
{
"title": "Utilizing Docker for Containerizing Node.js Applications",
"description": "Steps for containerizing Node.js applications using Docker to ensure consistency across development and production environments.",
"category": "Node.js Cursor Rules",
"rules": [
{
"id": "docker-node-multi-stage-builds",
"description": "Use multi-stage builds in Docker to create leaner and more secure images by separating build and production stages.",
"recommendation": "Implement multi-stage builds in your Dockerfile to copy only necessary artifacts to the final image, reducing size and potential security risks.",
"example": {
"before": "FROM node:14\nWORKDIR /app\nCOPY . .\nRUN npm install\nCMD [\"node\", \"app.js\"]",
"after": "FROM node:14 AS build\nWORKDIR /app\nCOPY . .\nRUN npm ci && npm run build\n\nFROM node:14-slim\nWORKDIR /app\nCOPY --from=build /app/dist ./\nCOPY package*.json ./\nRUN npm ci --production\nUSER node\nEXPOSE 8080\nCMD [\"node\", \"app.js\"]"
},
"references": [
"https://www.woyce.io/nodejs/node-js-best-practices-2024/"
]
},
{
"id": "docker-node-non-root-user",
"description": "Run Node.js applications in Docker containers as a non-root user to enhance security.",
"recommendation": "Create and switch to a non-root user in your Dockerfile to minimize security risks associated with running containers as root.",
"example": {
"before": "FROM node:14\nWORKDIR /app\nCOPY . .\nRUN npm install\nCMD [\"node\", \"app.js\"]",
"after": "FROM node:14\nWORKDIR /app\nCOPY . .\nRUN npm install\nRUN useradd -m appuser\nUSER appuser\nCMD [\"node\", \"app.js\"]"
},
"references": [
"https://medium.com/@thakkarkinjal2000/top-best-practices-for-containerizing-node-js-web-applications-99e0746158d4"
]
},
{
"id": "docker-node-dockerignore",
"description": "Use a .dockerignore file to exclude unnecessary files from the Docker image, reducing size and potential security risks.",
"recommendation": "Create a .dockerignore file to prevent copying files like node_modules, .git, and local environment files into the Docker image.",
"example": {
"before": "No .dockerignore file present.",
"after": "Create a .dockerignore file with the following content:\n\nnode_modules\n.git\n.env\nDockerfile\n.dockerignore"
},
"references": [
"https://infinitejs.com/posts/streamline-nodejs-docker/"
]
},
{
"id": "docker-node-healthcheck",
"description": "Implement health checks in Docker to monitor the application's status and ensure reliability.",
"recommendation": "Add a HEALTHCHECK instruction in your Dockerfile to periodically check the application's health and enable automatic recovery if necessary.",
"example": {
"before": "FROM node:14\nWORKDIR /app\nCOPY . .\nRUN npm install\nCMD [\"node\", \"app.js\"]",
"after": "FROM node:14\nWORKDIR /app\nCOPY . .\nRUN npm install\nHEALTHCHECK --interval=5s --timeout=3s CMD curl --fail http://localhost:3000 || exit 1\nCMD [\"node\", \"app.js\"]"
},
"references": [
"https://artemee-lemann.medium.com/node-js-containerization-best-practices-38c05a3444b8"
]
},
{
"id": "docker-node-graceful-shutdown",
"description": "Ensure Node.js applications handle termination signals to allow graceful shutdowns in Docker containers.",
"recommendation": "Implement signal handlers in your Node.js application to manage SIGTERM and SIGINT signals, ensuring proper resource cleanup during container shutdown.",
"example": {
"before": "const express = require('express');\nconst app = express();\napp.listen(3000);",
"after": "const express = require('express');\nconst app = express();\nconst server = app.listen(3000);\n\nprocess.on('SIGTERM', () => {\n server.close(() => {\n console.log('Process terminated');\n });\n});"
},
"references": [
"https://unifiedguru.com/9-tips-for-containerizing-your-node-js-application-docker/"
]
}
]
}