Home > Mobile >  Node-js production build using docker and deploying application live
Node-js production build using docker and deploying application live

Time:07-12

I've created a index.js file having express and CORS with port 5000, which helps me to interact with that application. In local I'm able to interact with my application. I need help to make that application go live and Running using docker.

const express = require('express');
const mongoose = require('mongoose')
var cors = require('cors')
const port = 5000

require('dotenv').config();
const app = express();
app.use(express.json())
app.use(cors())


app.get('/', (req, res) => {
    response.send("Back-end connected");
})

app.listen(port, () => console.log("Listening...!"))

I have no idea how to deploy to the production using docker and make application live.Could please some one help me on this.

CodePudding user response:

You can use a Dockerfile to create a docker image of your node.js app and then you can run the docker image on a production server

Create a file named "Dockerfile" and paste the below code

FROM node:16

# Create app directory
WORKDIR /usr/../app

COPY package*.json ./
COPY . .

# Install dependencies
RUN npm install

EXPOSE 4000

CMD [ "node", "index.js" ] 

Use this command to build the image

docker build . -t your-app-name

Now login to docker using command

 docker login

Now you can push the docker image which you have created to dockerhub which is a container registry

docker push your-app-name

Now go to your production server and install Docker.

Run the app using below command

docker run -p 5001:5000 -d your-app-name

Now the app will be running on port 5001 on your live production server

Further you can using nginx on your production server to run the app on 80 port

  • Related