Have you ever wondered how Google Cloud Run magically scales your services down to zero and still manages to respond to incoming requests instantly? It feels like serverless magic, but under the hood, it’s powered by
Knative
—a technology that lets Kubernetes dynamically start and stop containers on demand.
Wouldn’t it be awesome to build something like that ourselves?
Today, we’re going to roll up our sleeves and create a Node.js-powered on-demand container system. 💡 We’ll:
✅ Start a container only when needed
✅ Forward incoming requests to it
✅ Auto-stop the container after inactivity
✅ Handle errors gracefully
Let’s go! 🚀
Step 1: The Simplest Server in a Container 🛠️
First, let’s create a tiny HTTP server that will run inside our container.
📄 server.js
import http from "http";
const hostname = "0.0.0.0";
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello from your on-demand container!n");
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
javascript
Now, let’s dockerize it.
📄 Dockerfile
FROM node:22
WORKDIR /app
COPY ./server.js ./server.js
CMD ["node", "server.js"]
plain text
Build the image:
docker build . -t simple-server:local
shell
Step 2: The Magic—Dynamically Starting a Container 🎩✨
Now, let’s write a script that:
- Listens for HTTP requests
- Checks if the container is running
- Starts it if needed
- Forwards requests to the container
📄 index.js
import http from "node:http";
import { proxyRequest, isPortOpen, startContainer, stopContainer } from "./utils.js";
const CONTAINER_PORT = 8080;
const INACTIVITY_TIMEOUT = 30_000; // 30 seconds
let lastRequestTime = Date.now();
let containerRunning = false;
const httpServer = http.createServer(async (req, res) => {
console.log("➡️ Incoming request detected...");
lastRequestTime = Date.now();
if (!(await isPortOpen(CONTAINER_PORT))) {
console.log("🚀 Container is not running. Starting it now...");
1 Comment
codehammer
Have you ever wondered how Google Cloud Run magically scales your services down to zero and still manages to respond to incoming requests instantly? Let’s try to mimic that feature with NodeJS