Create a Dockerfile for a simple Node.js application that serves “Hello World” on port 3000.
Question
Create a Dockerfile for a simple Node.js application that serves “Hello World” on port 3000. Build the Docker image with tag my-node-app and run a container. Below is the sample index.js file.
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');
});
Tag the Docker image my-node-app
from Task 3 with a version tag v1.0.0
.
Solution
Dockerfile
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
docker build -f Dockerfile -t my-node-app:v1.0.0 .
Push the image to Docker Hub
docker login
Then build the image with your repo name, For eg, `syedjaferkhub`
docker push syedjaferkhub/my-node-app:v1.0.0
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