Create a Dockerfile for a simple Node.js application that serves “Hello World” on port 3000.
Question
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});Solution
FROM node:20.13.1-alpine
WORKDIR /app
COPY index.js /app
COPY package.json /app
COPY package-lock.json /app
RUN npm i
CMD ["node", "index.js"]Build the image
Push the image to Docker Hub
PreviousPull the nginx image from Docker Hub and run it as a container. Map port 80 of the container to portNextRun a container from the ubuntu image and start an interactive shell session inside it.
Last updated