const http = require('http');
const port = 8080;
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, this is a simple Node.js server!');
});
// Start the server
server.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Kubernetes Service in GCP
=========================
-Docker
Step-1 Login to GCP Console.
Step-2 Open Cloud shell
Step-3 Go to Cloud Shell and verify the whether docker is available or not by using
below command
docker --version
Step-4 Create a new directory
mkdir demo
Step-5 Change the directory
cd demo
Step-6 under directory create simple node js application so open file like
server.js
vi server.js
|-once vi editor open then press i key for insert mode and paste below code
const http = require('http');
const port = 8080;
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, this is a simple Node.js server!');
});
// Start the server
server.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
|-save the file => press ESC key and type :wq! and press enter
Step-7 if you want to show the content of file then
cat server.js
Step-8 verify node version
node -v
Step-9 Create Dockerfile
vi Dockerfile
FROM node:22.16.0
EXPOSE 8080
COPY server.js /server.js
CMD node server.js
Step-10 Create docker image for your application using Dockerfile
docker build -t my-app:v1.0 .
Step-11 list out docker images
docker images
Step-12 Running Docker Container
docker run -d -p 8090:8080 my-app:v1.0
Step-13 Go to Web Preview in cloud shell.
click in web preview--->go to change port
Step-14 list out running container
docker ps
Step-15 stop your container
docker stop <container_id>
Step-16 Go to search bar and search cloud regsitry
Step-18 Go to Cloud shell & build an image in gcr
docker build -t gcr.io/$DEVSHELL_PROJECT_ID/my-app:v1.0 .
Step-19 pushing image to GCR.
gcloud auth configure-docker
docker tag my-app:v1.0 gcr.io/$DEVSHELL_PROJECT_ID/my-app:v1.0
docker push gcr.io/$DEVSHELL_PROJECT_ID/my-app:v1.0