Docker在启动后运行失败(节点:19)未处理的PromiseRejection警告:错误:已退出,代码为3



运行docker build . -t app后,一旦我运行docker run -p 8080:8080 app,Docker容器或节点就会抛出一个错误,阻止我查看应用程序。每当我在本地运行节点应用程序时,我通常只运行npm run dev,一切都很好。我尝试过更改Dockerfile中的节点版本、清理npm缓存、运行不同的命令等。似乎什么都不起作用,但我认为这个错误是由一些端口问题引起的,因为当我尝试在EC2服务器中运行应用程序时,我也看到了同样的情况。

这是我看到问题的输出

user@MacBook-Pro-2 Website % docker run -p 8080:8080 app
> permitivity@1.0.0 dev /usr/src/app
> node build/dev-server.js
> Starting dev server...
{ parser: "babylon" } is deprecated; we now treat it as { parser: "babel" }.
DONE  Compiled successfully in 18640ms2:44:03 AM
> Listening at http://localhost:8080
(node:19) UnhandledPromiseRejectionWarning: Error: Exited with code 3
at ChildProcess.<anonymous> (/usr/src/app/node_modules/opn/index.js:85:13)
at Object.onceWrapper (events.js:422:26)
at ChildProcess.emit (events.js:315:20)
at maybeClose (internal/child_process.js:1021:16)
at Socket.<anonymous> (internal/child_process.js:443:11)
at Socket.emit (events.js:315:20)
at Pipe.<anonymous> (net.js:674:12)
(node:19) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:19) [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.

我的Dockerfile很简单:

FROM node:12-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm i
COPY . .
CMD ["npm", "start"]

您需要通过在Dockerfile的末尾添加1行来向主机公开docker端口;

曝光8080

同时将完整命令写入RUN npm install。可能是包裹的缘故。

因此,在我的dev-server.js中,打开端口后,服务器尝试在浏览器中打开url。显然,这在docker容器中不起作用,因此进程崩溃。

devMiddleware.waitUntilValid(() => {
console.log('> Listening at ' + uri + 'n')
// when env is testing, don't need open it
if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
opn(uri)
}
_resolve()
})

我所做的只是注释掉opn(uri)

相关内容

最新更新