简体   繁体   English

使用 docker 构建 Node-js 生产环境并实时部署应用程序

[英]Node-js production build using docker and deploying application live

I've created a index.js file having express and CORS with port 5000, which helps me to interact with that application.我创建了一个 index.js 文件,其中包含 express 和 CORS 以及端口 5000,这有助于我与该应用程序进行交互。 In local I'm able to interact with my application.在本地,我可以与我的应用程序进行交互。 I need help to make that application go live and Running using docker.我需要帮助才能使该应用程序上线并使用 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.我不知道如何使用 docker 部署到生产环境并使应用程序上线。请有人帮助我。

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您可以使用 Dockerfile 创建 node.js 应用程序的 docker 映像,然后可以在生产服务器上运行 docker 映像

Create a file named "Dockerfile" and paste the below code创建一个名为“Dockerfile”的文件并粘贴以下代码

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

 docker login

Now you can push the docker image which you have created to dockerhub which is a container registry现在您可以将您创建的 docker 镜像推送到 dockerhub,它是一个容器注册表

docker push your-app-name

Now go to your production server and install Docker.现在转到您的生产服务器并安装 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现在该应用程序将在您的实时生产服务器上的端口 5001 上运行

Further you can using nginx on your production server to run the app on 80 port此外,您可以在生产服务器上使用 nginx 在 80 端口上运行应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM