Sunday, February 27, 2022

Deploy Run Web Apps in Node.js Docker Containers

 in This Tutorial you will learn " How To Deploy Run Web Apps in Node.js Docker Containers

Docker is a software platform that allows you to build, test, and deploy applications quickly.
Node.js is an open-source and cross-platform JavaScript runtime environment.
_________________________________________________________________________________________
Server - Os:  Ubuntu 20.04.3 LTS 64Bit        | IP -192.168.1.80            |Hostname -  ubuntu.example.com
_________________________________________________________________________________________
lsb_release -d ; hostname -I
apt update ; apt -y install docker.io docker-compose ca-certificates curl gnupg-agent software-properties-common -y
docker --version ; docker-compose --version

mkdir ~/nodejs_docker  ; cd nodejs_docker
nano myapp.js
'use strict';
const express = require('express');
// Constants
const PORT = 3000;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World!');
});
app.listen(PORT, HOST);
console.log(`Running at http://${HOST}:${PORT}`);

nano package.json
{
  "name": "myapp",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "educationpurpose <educationpurpose@example.com>",
  "main": "myapp.js",
  "scripts": {
    "start": "node myapp.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}

nano Dockerfile
FROM node:13-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "myapp.js" ]

docker build .
docker run \
--name nodejs-docker-app \
-p 80:3000 \
-d \
b4d550e97c57
docker logs nodejs-docker-app
curl localhost:80
_________________________________________________________________________________________




No comments:

Post a Comment