We’ll create a simple app, build a Docker image, and manage it locally. We’ll use Node.js for our example app. Let’s build something!
Create a directory my-app and add these files:
server.js:
const http = require('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, World!\n');});server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`);
});package.json:
{ "name": "my-app", "version": "1.0.0", "main": "server.js", "dependencies": { "http": "^0.0.1-security" }}
FROM node:20-alpineWORKDIR /appCOPY package*.json ./RUN npm installCOPY . .EXPOSE 3000CMD ["node", "server.js"]This uses a base Node image, copies files, installs deps, and runs the server.
Build: docker build -t my-app .
Run: docker run -d -p 3000:3000 my-app
Visit http://localhost:3000—see "Hello, World!"
FROM node:20-alpine AS builderWORKDIR /appCOPY package*.json ./RUN npm installCOPY . .RUN npm run build # If applicableFROM node:20-alpineWORKDIR /appCOPY --from=builder /app .CMD ["node", "server.js"]
Ready to transform your business with our technology solutions? Contact Us today to Leverage Our DevOps Expertise.
Contact Us