将一个简单的 React 表单应用程序添加到现有的 Nodejs docker 镜像

时间:2021-03-03 15:31:27

标签: node.js reactjs docker docker-compose dockerfile

我创建了一个 Dockerfile,当使用 docker-compose 运行时,在浏览器中显示“Hello world”。现在我希望能够显示一个简单的反应表单,其中包含一些用于输入文本的字段和一个提交按钮。是否有任何我可以使用的简单项目的示例。我将如何更新我在下面显示的文件

这些是我使用的文件:

Package.json -

 {
  "name": "nodejs-hello",
  "version": "1.0.0",
  "description": "des",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "hh",
  "license": "ISC",
  "keywords": [
    "h"
  ],
  "dependencies": {
    "express": "^4.17.1"
  }
}

index.js -

    //now it load express module with `require` directive
var express = require('express')
var app = express()
const PORT = 8080;
//Define request response in root URL (/) and response with text Hello World!
app.get('/', function (req, res) {
  res.send('Hello World')
})
//Launch listening server on port 8080 and consoles the log.
app.listen(PORT, function () {
  console.log('app listening on port ' + PORT)
})

Dockerfile -

FROM node:latest

WORKDIR /app

COPY package.json index.js ./

RUN npm install

EXPOSE 8080

CMD node index.js

Docker Compose -

version: "3"

services:
  web:
    image: my-image:1.0
    build: .
    ports:
      - '8080:8080'

https://medium.com/faun/a-simple-docker-setup-for-simple-hello-world-nodejs-application-bcf79bb608a0

我现在所拥有的与该链接中的示例相似。

现在我只想能够在浏览器中显示一个响应表单。

1 个答案:

答案 0 :(得分:0)

只需创建您的 React 应用程序,然后添加这一行即可将 src 文件复制到容器中:

COPY . .

此外,将 CMD(如果您使用的是 CRA)更改为:

CMD [ "npm", "start" ]
相关问题