graph TD
A[Docker Container] --> B[Node.js Runtime]
B --> C[Express Server]
C --> D[Application Code]
D --> E[Node Modules]
A --> F[Port 3000]
sequenceDiagram
participant D as Developer
participant B as Docker Build
participant I as Docker Image
participant C as Container
participant A as App
D->>B: docker build
B->>I: Create Image
D->>I: docker run
I->>C: Start Container
C->>A: Run Node.js App
A->>D: Expose Port 3000
# Base image
FROM node:18
# Set working directory
WORKDIR /app
# Install dependencies first (layer caching)
COPY package*.json ./
RUN npm install
# Copy application code
COPY . .
# Expose port
EXPOSE 3000
# Start command
CMD ["npm", "start"]
Excluded files and directories:
node_modules/
: Local dependenciesnpm-debug.log
: Debug logsDockerfile
: Build file.dockerignore
: Ignore file.git/
: Version control.gitignore
: Git ignore file
# Build the image
docker build -t mytodolist .
# Run the container
docker run -p 3000:3000 mytodolist
- Container Port: 3000
- Host Port: 3000
- Access URL: http://localhost:3000
-
Layer Optimization
- Dependencies installed separately (Implemented via separate COPY for package.json and npm install)
- Leverages Docker cache (Achieved by installing dependencies before copying app code)
- Minimizes rebuild time (Dependencies only reinstalled when package.json changes)
-
Security
- Non-root user context (Inherited from official Node.js image's best practices)
- Minimal image contents (Implemented via .dockerignore to exclude unnecessary files)
- Excluded sensitive files (Configured in .dockerignore to exclude .git, logs, etc.)
-
Resource Management
- Single process per container (Implemented via single CMD running npm start)
- Port explicitly exposed (EXPOSE 3000 in Dockerfile)
- Clean shutdown handling (Node.js process handles SIGTERM signals)
-
Development Workflow
- Hot-reloading capable (NOT YET IMPLEMENTED - Would require nodemon and volume mounting)
- Environment variable support (NOT YET IMPLEMENTED - No .env or environment variables configured)
- Volume mounting possible (NOT YET IMPLEMENTED - Need instructions below)
To enable hot-reloading and development with volumes:
- Install nodemon:
npm install --save-dev nodemon
- Add dev script to package.json:
"scripts": {
"dev": "nodemon server.js"
}
- Update Dockerfile for development:
# Development stage
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
EXPOSE 3000
CMD ["npm", "run", "dev"]
- Run container with volume mounting:
docker run -p 3000:3000 -v $(pwd):/app mytodolist
To add environment variable support:
- Create .env file:
PORT=3000
NODE_ENV=development
- Add to .gitignore and .dockerignore:
.env
- Install dotenv:
npm install dotenv
- Update server.js to use environment variables:
require('dotenv').config();
const port = process.env.PORT || 3000;
-
Port Conflicts
# Solution: Use different port docker run -p 3001:3000 mytodolist
-
Build Failures
- Check Node.js version compatibility
- Verify package.json integrity
- Confirm network connectivity
-
Container Access
- Verify Docker Desktop running
- Check port mapping
- Confirm host firewall settings