npm 的 docker-compose up 失败

时间:2021-01-21 15:56:45

标签: javascript node.js docker npm docker-compose

我在运行 docker-compose up 命令时遇到以下异常:

Creating compose_node-app_1 ... done
Attaching to compose_node-app_1
node-app_1  | 
node-app_1  | > start
node-app_1  | > node index.js
node-app_1  | 
node-app_1  | /usr/app/node_modules/assert-options/dist/src/index.js:16
node-app_1  |                 throw new Error('Option "' + a + '" is not recognized.');
node-app_1  |                 ^
node-app_1  | 
node-app_1  | Error: Option "host" is not recognized.
node-app_1  |     at assertOptions (/usr/app/node_modules/assert-options/dist/src/index.js:16:23)
node-app_1  |     at $main (/usr/app/node_modules/pg-promise/lib/main.js:194:15)
node-app_1  |     at Object.<anonymous> (/usr/app/index.js:13:12)
node-app_1  |     at Module._compile (node:internal/modules/cjs/loader:1108:14)
node-app_1  |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
node-app_1  |     at Module.load (node:internal/modules/cjs/loader:973:32)
node-app_1  |     at Function.Module._load (node:internal/modules/cjs/loader:813:14)
node-app_1  |     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
node-app_1  |     at node:internal/main/run_main_module:17:47
node-app_1  | npm ERR! code 1
node-app_1  | npm ERR! path /usr/app
node-app_1  | npm ERR! command failed
node-app_1  | npm ERR! command sh -c node index.js
node-app_1  | 
node-app_1  | npm ERR! A complete log of this run can be found in:
node-app_1  | npm ERR!     /root/.npm/_logs/2021-01-21T15_49_47_966Z-debug.log

以下是我在不同文件中的内容:

docker-compose.yml

version: '3.4'
services:
        # postgres-server:
        #         network_mode: host
        #         image: postgres
        #         restart: always
        #         environment:
        #                 POSTGRES_PASSWORD: example
        # adminer:
        #         network_mode: host
        #         image: adminer
        #         restart: always
        #         ports:
        #                 - '8080:8080'
        node-app:
                restart: on-failure
                build: .
                ports:
                        - '8089:8089'

index.js

const express = require('express');
const app = express();
const hostname = '127.0.0.1';
app.get('/', (req, res)=> {

  res.send('Welcome to K8s session')
});
app.listen(8089, hostname, ()=>{
  console.log('Listening to port 8080')
});

package.json

{
  "dependencies":{
    "express":"*"
  },
  "scripts":{
    "start": "node index.js"
  }
}

Dockerfile

FROM node:alpine

WORKDIR /usr/app
COPY . .
RUN npm install --global
COPY ./package.json .

CMD ["npm", "start"]

如果我通常使用 docker 命令来构建和运行这个容器然后能够这样做..但我正在尝试通过 docker-compose 因为想要扩展使用 db.. 有人可以建议这个主机异常是什么吗?我们如何排除故障?

谢谢

1 个答案:

答案 0 :(得分:0)

这不是 docker-compose 问题,而是与您在 express 中使用 listen 命令有关。

您无需在 listen 调用中设置主机名,将其设置为回送只会使其自行侦听,这是行不通的。

删除主机名,你应该是金色的。