【问题标题】:I am getting error when I try to dockerize my MERN application当我尝试将我的 MERN 应用程序 dockerize 时出现错误
【发布时间】:2020-09-25 01:13:39
【问题描述】:

这是我在终端中遇到的错误的 React.js Dockerfile:

FROM node:8
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY ./package.json /usr/src/app
RUN npm install

RUN npm build
EXPOSE 3000
CMD ["npm", "run", "start"] 

错误:-

react_1     | 
react_1     | > ecom-panther@0.1.0 start /usr/src/app
react_1     | > react-scripts start
react_1     | 
react_1     | ℹ 「wds」: Project is running at http://172.18.0.2/
react_1     | ℹ 「wds」: webpack output is served from 
react_1     | ℹ 「wds」: Content not from webpack is served from /usr/src/app/public
react_1     | ℹ 「wds」: 404s will fallback to /
react_1     | Starting the development server...
react_1     | 
ecom-panther_react_1 exited with code 0

对于 Node 和 Express,我得到了这个:

express_1   | (node:30) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
express_1   | server is running on port: 5000
express_1   | (node:30) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
express_1   |     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
express_1   |     at emitOne (events.js:116:13)
express_1   |     at Pool.emit (events.js:211:7)
express_1   |     at createConnection (/usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:561:14)
express_1   |     at connect (/usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:994:11)
express_1   |     at makeConnection (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:31:7)
express_1   |     at callback (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:264:5)
express_1   |     at Socket.err (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:294:7)
express_1   |     at Object.onceWrapper (events.js:315:30)
express_1   |     at emitOne (events.js:116:13)
express_1   |     at Socket.emit (events.js:211:7)
express_1   |     at emitErrorNT (internal/streams/destroy.js:73:8)
express_1   |     at _combinedTickCallback (internal/process/next_tick.js:139:11)
express_1   |     at process._tickCallback (internal/process/next_tick.js:181:9)
express_1   | (node:30) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
express_1   | (node:30) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

后端的 Docker 文件:-

FROM node:8
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
COPY . /usr/src/app
EXPOSE 5000
CMD ["npm","start"] 

这是我的 docker-compose.yml 文件

version:  '3' # specify docker-compose version

# Define the service/container to be run
services:
  react: #name of first service
    build: client #specify the directory of docker file
    ports:
    - "3000:3000" #specify port mapping

  express: #name of second service
    build: server #specify the directory of docker file
    ports:
    - "5000:5000" #specify port mapping
    links:
    - database #link this service to the database service

  database: #name of third service
    image: mongo #specify image to build contasiner flow 
    ports:
    - "27017:27017" #specify port mapping

如何在浏览器上运行前端,有什么简单的方法可以更好地做到这一点?

【问题讨论】:

  • 现在完成了..也许现在对你理解更有帮助
  • 更好,但现在错误/日志已删除。请将您的 IDE 中的错误以文本格式复制到问题中。它们也不应该以图像的形式呈现。
  • 第一张图片中的日志是最重要的——它们包含了答案。再次编辑后请联系我@halfer,我会提供答案。
  • @halfer 请立即查看
  • 更好,但也请添加您的express 1 日志。您仅添加了 react 1 日志。

标签: docker docker-compose


【解决方案1】:

错误 1:

stdin_open: true 添加到您的react 服务中,例如:

...
services:
  react: #name of first service
    build: client #specify the directory of docker file
    stdin_open: true
    ports:
    - "3000:3000" #specify port mapping
...

您可能需要重建或清理缓存,因此“docker-compose up --build”或“docker-compose build --no-cache”然后“docker-compose up"

错误 2:

在 index.js 文件中的数据库连接行或您命名的任何内容中:

mongodb://database:27017/  

其中“数据库”是您命名的 MongoDB 服务。您也可以将您的容器 IP 地址与 docker inspect &lt;container&gt; 一起使用,也可以使用那里的 IP。理想情况下,您希望在 Dockerfile 或 docker-compose.yml 中有一个 ENV:

ENV MONGO_URL mongodb://database:27017/

【讨论】:

  • 我没有看到你有两个问题,现在你有我的两个解决方案,请记住你有两个与你的问题相关的问题,很多人都在努力帮助你。
  • 嘿@halfer 当我尝试只对我的反应应用程序进行 dockerize 时,它​​会给出错误。命令:- sudo docker run -p 3000:3000 react:app 错误:- > my-app@0.1.0 start /usr/src/app > react-scripts start ℹ「wds」:项目在172.17.0.2运行ℹ 「wds」:webpack 输出来自 ℹ 「wds」:不是来自 webpack 的内容来自 /usr/src/app/public ℹ 「wds」:404s 将回退到 / 启动开发服务器...
  • @Deepikavijay:这听起来像是 MikZuit 试图帮助您解决的问题(“错误 1”)。您是否尝试过这里的建议?
  • 但他告诉我做这个 docker-compose yml 文件,但我只是创建一个单独的 docker 文件并创建图像,然后运行相同的图像。这是我的 dockerfile:- FROM node:10 # 创建应用程序目录 WORKDIR /usr/src/app # 安装应用程序依赖项 COPY package*.json ./ # RUN npm install --silent # 复制应用程序源代码 COPY 。 . EXPOSE 3000 CMD ["npm", "start"] 我需要在这里添加 "stdin_open: true"
  • @Deepikavijay 您面临的问题是 react-script github.com/facebook/create-react-app/issues/8688 的常见问题。我建议您在提出这个问题时保留您的文件,然后按照我的建议进行更改,只需在 docker-compose.yml 中添加一行即可。然后我使用“docker-compose up --build”或我在答案中更新的其他命令运行..
猜你喜欢
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多