0

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.

1 Answer 1

1

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:4000 -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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this steps. how I can config the Nginx. i mean how I can replace the index.html with this docker image?
@Teja Are you serving an HTML from the node.js app? If yes then it will automatically be part of the node.js app

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.