Complete Node.js container implementation with multi-version support
Some checks failed
Cloud Node Container / Build-and-Push (18) (push) Failing after 10s
Cloud Node Container / Build-and-Push (20) (push) Failing after 8s
Cloud Node Container / Build-and-Push (22) (push) Failing after 8s

- Add Dockerfile with AlmaLinux 9 base, Nginx reverse proxy, and PM2
- Support Node.js versions 18, 20, 22 with automated installation
- Implement memory-optimized configuration (256MB minimum, 512MB recommended)
- Add Memcached session storage for development environments
- Create comprehensive documentation (README, USER-GUIDE, MEMORY-GUIDE, CLAUDE.md)
- Include example applications (simple website and REST API)
- Add Gitea CI/CD pipeline for automated multi-version builds
- Provide local development script with helper utilities
- Implement health monitoring, log rotation, and backup systems

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-07-21 16:00:46 -07:00
parent 36285fa102
commit 2989cd590a
28 changed files with 1777 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
module.exports = {
apps: [{
name: 'node-app',
script: 'index.js',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '256M', // Restart if app uses more than 256MB
kill_timeout: 3000,
wait_ready: true,
listen_timeout: 3000,
env: {
NODE_ENV: 'development',
PORT: 3000,
NODE_OPTIONS: '--max-old-space-size=200' // Limit V8 heap to 200MB
},
env_production: {
NODE_ENV: 'production',
PORT: 3000,
NODE_OPTIONS: '--max-old-space-size=200'
},
log_file: '/home/myuser/logs/nodejs/app.log',
error_file: '/home/myuser/logs/nodejs/error.log',
out_file: '/home/myuser/logs/nodejs/out.log',
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
log_type: 'json',
merge_logs: true,
max_restarts: 5,
min_uptime: '10s'
}]
};

101
configs/index.js Normal file
View File

@@ -0,0 +1,101 @@
const express = require('express');
const session = require('express-session');
const app = express();
const port = process.env.PORT || 3000;
// Middleware
app.use(express.json());
app.use(express.static('public'));
// Session configuration with Memcache (only in DEV mode when memcached is available)
if (process.env.NODE_ENV !== 'production') {
try {
const MemcachedStore = require('connect-memcached')(session);
app.use(session({
store: new MemcachedStore({
hosts: ['localhost:11211']
}),
secret: process.env.SESSION_SECRET || 'your-secret-key-change-in-production',
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // Allow HTTP in development
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}
}));
console.log('Memcached session store initialized');
} catch (err) {
console.log('Memcached not available, using memory store');
app.use(session({
secret: process.env.SESSION_SECRET || 'your-secret-key-change-in-production',
resave: false,
saveUninitialized: false,
cookie: {
secure: false,
maxAge: 24 * 60 * 60 * 1000
}
}));
}
} else {
// Production session configuration (expects external session store)
app.use(session({
secret: process.env.SESSION_SECRET || 'your-secret-key-change-in-production',
resave: false,
saveUninitialized: false,
cookie: {
secure: true, // HTTPS only in production
maxAge: 24 * 60 * 60 * 1000
}
}));
}
// Health check endpoint
app.get('/ping', (req, res) => {
res.json({
status: 'ok',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
version: process.env.npm_package_version || '1.0.0'
});
});
// Default route
app.get('/', (req, res) => {
res.json({
message: 'Cloud Node Container is running!',
nodeVersion: process.version,
environment: process.env.NODE_ENV || 'development',
timestamp: new Date().toISOString()
});
});
// Info endpoint
app.get('/info', (req, res) => {
res.json({
nodeVersion: process.version,
platform: process.platform,
arch: process.arch,
uptime: process.uptime(),
memory: process.memoryUsage(),
env: process.env.NODE_ENV || 'development'
});
});
// Session demo endpoint
app.get('/session', (req, res) => {
if (!req.session.visits) {
req.session.visits = 0;
}
req.session.visits++;
res.json({
sessionId: req.sessionID,
visits: req.session.visits,
message: 'Session is working with ' + (process.env.NODE_ENV !== 'production' ? 'Memcached' : 'default store')
});
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
});

45
configs/nginx.conf Normal file
View File

@@ -0,0 +1,45 @@
user nginx;
worker_processes 1; # Single worker for memory efficiency
worker_rlimit_nofile 1024;
error_log /var/log/nginx/error.log warn; # Less verbose logging
pid /run/nginx.pid;
events {
worker_connections 512; # Reduced for memory efficiency
use epoll;
multi_accept on;
}
http {
# Memory-optimized settings
client_body_buffer_size 16k;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 2 1k;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main buffer=16k flush=2m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30; # Reduced from 65
keepalive_requests 100;
types_hash_max_size 1024; # Reduced from 2048
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Optimized gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_comp_level 2; # Lower compression for less CPU/memory usage
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/conf.d/*.conf;
}

27
configs/package.json Normal file
View File

@@ -0,0 +1,27 @@
{
"name": "cloud-node-container",
"version": "1.0.0",
"description": "Default Node.js application for Cloud Node Container",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"express": "^4.18.2",
"express-session": "^1.17.3",
"connect-memcached": "^1.0.0"
},
"devDependencies": {
"nodemon": "^3.0.1"
},
"keywords": [
"nodejs",
"express",
"container",
"docker"
],
"author": "",
"license": "MIT"
}