node.js - NodeJS Express Docker 实例退出,代码为 0

标签 node.js express docker docker-compose dockerfile

目标

我的 dockercompose.yml 中有 3 个实例,我只能启动其中 2 个实例,并且很难启动我的 Node 所表达的第三个实例。

<小时/>

项目目录

.
├── Icon\r
├── angular
│   ├── Dockerfile
│   ├── README.md
│   ├── dist
│   │   ├── css
│   │   │   └── style.css
│   │   ├── img
│   │   │   ├── external.png
│   │   │   ├── favicon.png
│   │   │   ├── footer_logo.png
│   │   │   └── logo.png
│   │   ├── index.html
│   │   └── js
│   │       ├── bundle.js
│   │       └── vendor.js
│   ├── docs
│   │   ├── app
│   │   │   ├── api.js
│   │   │   ├── app.config.js
│   │   │   ├── app.module.js
│   │   │   └── guide.js
│   │   ├── build
│   │   │   ├── index.html
│   │   │   ├── partials
│   │   │   │   ├── api
│   │   │   │   │   ├── common
│   │   │   │   │   │   ├── directive
│   │   │   │   │   │   │   └── app.html
│   │   │   │   │   │   ├── directive.html
│   │   │   │   │   │   ├── type
│   │   │   │   │   │   │   ├── AppController.html
│   │   │   │   │   │   │   └── AppSidebarController.html
│   │   │   │   │   │   └── type.html
│   │   │   │   │   ├── common.html
│   │   │   │   │   ├── components.auth
│   │   │   │   │   │   ├── service
│   │   │   │   │   │   │   └── AuthService.html
│   │   │   │   │   │   └── service.html
│   │   │   │   │   ├── components.auth.html
│   │   │   │   │   ├── components.contact
│   │   │   │   │   │   ├── directive
│   │   │   │   │   │   │   └── lengthCheck.html
│   │   │   │   │   │   ├── directive.html
│   │   │   │   │   │   ├── service
│   │   │   │   │   │   │   └── ContactService.html
│   │   │   │   │   │   ├── service.html
│   │   │   │   │   │   ├── type
│   │   │   │   │   │   │   └── ContactEditController.html
│   │   │   │   │   │   └── type.html
│   │   │   │   │   ├── components.contact.html
│   │   │   │   │   └── components.html
│   │   │   │   ├── api.html
│   │   │   │   ├── guide
│   │   │   │   │   └── howTo.html
│   │   │   │   └── guide.html
│   │   │   └── src
│   │   │       ├── api-data.js
│   │   │       ├── api.js
│   │   │       ├── app.config.js
│   │   │       ├── app.module.js
│   │   │       ├── guide-data.js
│   │   │       └── guide.js
│   │   ├── config
│   │   │   ├── index.js
│   │   │   ├── processors
│   │   │   │   ├── api-data.js
│   │   │   │   ├── guide-data.js
│   │   │   │   └── index-page.js
│   │   │   └── templates
│   │   │       ├── constant-data.template.js
│   │   │       ├── content.template.html
│   │   │       ├── indexPage.template.html
│   │   │       └── module.template.html
│   │   └── content
│   │       ├── api
│   │       │   └── index.md
│   │       └── guide
│   │           ├── howTo.md
│   │           └── index.md
│   ├── files
│   │   ├── default.conf
│   │   └── nginx.conf
│   ├── gulpfile.babel.js
│   ├── karma.conf.js
│   ├── mocks
│   │   └── firebase.mock.js
│   ├── npm-shrinkwrap.json
│   ├── package.json
│   ├── src
│   │   ├── app
│   │   │   ├── common
│   │   │   │   ├── app-footer.component.js
│   │   │   │   ├── app-footer.html
│   │   │   │   ├── app-header.component.js
│   │   │   │   ├── app-header.html
│   │   │   │   ├── app-pagination.html
│   │   │   │   ├── app-pagination.js
│   │   │   │   ├── app-sidebar.component.js
│   │   │   │   ├── app-sidebar.html
│   │   │   │   ├── app.component.js
│   │   │   │   ├── app.html
│   │   │   │   ├── app.module.js
│   │   │   │   ├── app.spec.js
│   │   │   ├── components
│   │   │   │   ├── auth
│   │   │   │   │   ├── auth-form
│   │   │   │   │   │   ├── auth-form.component.js
│   │   │   │   │   │   ├── auth-form.controller.js
│   │   │   │   │   │   ├── auth-form.html
│   │   │   │   │   │   └── auth-form.spec.js
│   │   │   │   │   ├── auth.module.js
│   │   │   │   │   ├── auth.spec.js
│   │   │   │   │   └── login
│   │   │   │   │       ├── login.component.js
│   │   │   │   │       ├── login.controller.js
│   │   │   │   │       ├── login.html
│   │   │   │   │       └── login.spec.js
│   │   │   │   ├── components.module.js
│   │   │   │   └── dashboard
│   │   │   │       ├── access-point-map
│   │   │   │       │   ├── access-point-map.component.js
│   │   │   │       │   └── access-point-map.html
│   │   │   │       ├── account-stats
│   │   │   │       │   ├── account-stats.component.js
│   │   │   │       │   └── account-stats.html
│   │   │   │       ├── dashboard.component.js
│   │   │   │       └── dashboard.html
│   │   │   ├── root.component.js
│   │   │   ├── root.html
│   │   │   └── services
│   │   │       ├── account.service.js
│   │   │       ├── auth.service.js
│   │   │       └── services.module.js
│   │   ├── img
│   │   │   ├── external.png
│   │   │   ├── favicon.png
│   │   │   ├── footer_logo.png
│   │   │   └── logo.png
│   │   ├── index.html
│   │   └── sass
│   │       ├── base
│   │       │   ├── _buttons.scss
│   │       │   ├── _forms.scss
│   │       │   ├── _headings.scss
│   │       │   ├── _list-group.scss
│   │       │   ├── _main.scss
│   │       │   ├── _media.scss
│   │       │   ├── _mixins.scss
│   │       │   ├── _page-header.scss
│   │       │   ├── _scaffolding.scss
│   │       │   ├── _sections.scss
│   │       │   ├── _table.scss
│   │       │   └── _tabset.scss
│   │       ├── components
│   │       │   ├── _auth.scss
│   │       │   ├── _footer.scss
│   │       │   ├── _header.scss
│   │       │   ├── _panels.scss
│   │       │   ├── _root.scss
│   │       │   ├── _sidebar.scss
│   │       │   └── _tablet.scss
│   │       ├── layout
│   │       │   ├── _loading.scss
│   │       │   └── _smb.scss
│   │       ├── style.scss
│   │       ├── utils
│   │       │   ├── _colors.scss
│   │       │   └── _typography.scss
│   │       └── vendors
│   │           ├── _bootstrap.scss
│   │           ├── _colorpicker.scss
│   │           ├── _fontawesome.scss
│   │           └── _rzslider.scss
│   └── templates.js
├── docker-compose.yml
└── express
    ├── Dockerfile
    ├── LICENSE
    ├── README.md
    ├── bin
    │   └── index.js
    ├── npm-debug.log
    ├── npm-shrinkwrap.json
    ├── package.json
    └── server
        ├── app.js
        ├── config
        │   └── config.json
        ├── controllers
        │   ├── accounts.js
        │   ├── auth.js
        │   ├── index.js
        │   ├── todoitems.js
        │   └── todos.js
        ├── migrations
        │   ├── 20160717103305-create-todo.js
        │   └── 20160717104733-create-todo-item.js
        ├── models
        │   ├── index.js
        │   ├── todo.js
        │   └── todoitem.js
        ├── routes
        │   └── index.js
        └── services
            ├── account.js
            ├── cpe.js
            ├── device.js
            └── index.js

59 directories, 166 files
<小时/>

后端

Dockerfile

# Create image based on the official Node 6 image from the dockerhub
FROM node:6

RUN useradd --user-group --create-home --shell /bin/false nodejs

# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app

# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app

# Copy dependency definitions
COPY package.json /usr/src/app

# Install dependecies
RUN npm install

# Get all the code needed to run the app
COPY . /usr/src/app

# Expose the port the app runs in
EXPOSE 3000

# Serve the app
CMD ["node", "server/app.js"]

根据我的文件结构,上面的文件是否有任何错误?

<小时/>

前端

Dockerfile

FROM alpine:3.6

RUN apk add --update nginx && rm -rf /var/cache/apk/*
RUN mkdir -p /tmp/nginx/client-body

COPY files/nginx.conf /etc/nginx/nginx.conf
COPY files/default.conf /etc/nginx/conf.d/default.conf
COPY dist /usr/share/nginx/html

CMD ["nginx", "-g", "daemon off;"]
<小时/>

dockercompose.yml

version: '2.1'

    # Define the services/containers to be run
    services:
      angular:
        build: angular # specify the directory of the Dockerfile
        network_mode: bridge
        ports:
        - 8080:80/tcp
        volumes:
        - /Users/bheng/Sites/BASE/angular/dist:/usr/share/nginx/html:rw
    
      express: #name of the second service
        build: express # specify the directory of the Dockerfile
        ports:
          - "3000:3000" #specify ports forewarding
        mem_limit: 300m
        memswap_limit: 1g
        volumes:
          - .:/home/nodejs/app
          - /home/nodejs/app/node_modules
    
        links:
          - database
    
      database: # name of the third service
        image: postgres
        environment:
          POSTGRES_PASSWORD: ih4cku
        ports:
          - "5432:5432" # specify port forewarding
<小时/>

结果

我无法启动第三个实例。

我不断得到

docker-compose up
Starting base_angular_1 ... 
Starting base_database_1 ... 
Starting base_database_1
Starting base_database_1 ... done
Starting base_express_1 ... 
Starting base_express_1 ... done
Attaching to base_angular_1, base_database_1, base_express_1
database_1  | LOG:  database system was shut down at 2017-08-30 00:54:24 UTC
database_1  | LOG:  MultiXact member wraparound protections are now enabled
database_1  | LOG:  database system is ready to accept connections
database_1  | LOG:  autovacuum launcher started

base_express_1 exited with code 0

如何进一步进行调试?

最佳答案

好的,在您发布 app.js 后,我想我可以帮助您。

当您运行 Node app.js 时,该 app.js 正在执行并成功退出,因为它只是一个模块,但没有服务器启动。

您需要这样的文件:

创建一个bin文件夹并在里面创建一个文件,将该文件命名为www 名称只是建议,您可以随意命名。

#!/usr/bin/env node

/**
 * Load any undefined ENV variables
 */
require('dotenv').config();

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('app:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}
<小时/>

然后确保安装这些软件包:

npm install debug --save
npm install dotenv --save

如果需要,可以从 www 文件中删除这些内容。我只是将它们包含在这里,以便我的共享 www 文件运行。

然后,在 Express 应用的 Dockerfile 中,使用:

Node ./bin/www

此命令启动服务器,使进程保持事件状态并为您的 app.js Express 模块提供服务。确保您在 www 中需要 app.js 文件的位置与您的文件位置匹配。我将 ../app 放在 require 中。

如何将其包含在 DockerFile 中?

我推荐这个: 在 package.json 中创建一个 start 条目:

“开始”:“Node ./bin/www”

脚本部分。

然后,更新您的Dockerfile:

CMD npm startCMD ["npm", "start"]

关于node.js - NodeJS Express Docker 实例退出,代码为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45950380/

相关文章:

javascript - 如何破译 Node js中在javascript中的加密js中加密的字符串

javascript - 有效载荷字节与实际字节不同

javascript - 我尝试将 Node js 中的 JSON 发送到客户端,但它不起作用

javascript - 代理 Node 请求新端口并充当反向代理

android - 在Docker中找不到Watchman,但react-native成功构建了 bundle

docker - 启动后不久,沙盒代理容器就会崩溃

node.js - 能不能把 "node_modules"目录复制到别的机器上,不用每次都运行 "npm install"?

javascript - 如何在 Next.js 中为多个 url 创建一个处理程序?

node.js - 通过 Visual Studio 部署时,Express 4 应用程序无法在 Azure 中运行

docker - 在docker-compose上为sql:的db:和db_backup的最佳图像是什么?