docker - 当我尝试对我的 MERN 应用程序进行 dockerize 时出现错误
问题描述
这是我在终端中遇到的错误的 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
我如何在浏览器上运行前端,是否有任何简单的方法可以更好地做到这一点?
解决方案
错误一:
添加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 <container>
那里的 IP。理想情况下,您希望在 Dockerfile 中有一个 ENV,或者docker-compose.yml
:
ENV MONGO_URL mongodb://database:27017/
推荐阅读
- spring-boot - Spring Boot Native 缓存:如何通过单个键/元素过期/删除缓存数据
- bash - Mac bash 并行运行命令,其中一个进程不退出
- gnuplot - gnuplot:等宽的超文本?
- c - 如何使用 scanf 函数在输入的数字之间添加 (*)?
- mysql - MySQL数据库容器在发送流量时崩溃?
- amazon-web-services - Azure 弹性池的替代品
- javascript - 根据javascript中的多个分隔符拆分字符串(包括单个反斜杠作为分隔符)
- c++ - 构造函数参数的引用成员按值传递
- javascript - 第三方软件包中的“require(./.....”)的 Webpack 捆绑问题
- xslt -
输出到变量并访问它