docker-compose up时,nodejs应用程序退出,代码为0

时间:2018-07-01 08:59:57

标签: node.js docker docker-compose

我在运行docker-compose up时遇到以下错误

nginx_1       | 2018/07/03 06:54:17 [emerg] 1#1: host not found in upstream "admin:1123" in /etc/nginx/nginx.conf:19
nginx_1       | nginx: [emerg] host not found in upstream "admin:1123" in /etc/nginx/nginx.conf:19
nginx_1 exited with code 1

docker-compose.yml

version: "2"

volumes: 
  mongostorage:  

services:  
  app:
    build: ./app
    ports:
      - "3000"
    links:
      - mongo
      - redis

    volumes:
      - ./app:/var/www/app
      - /var/www/app/node_modules

  adminmongo:
    build: ./adminMongo
    ports:
      - "4455"
    links:
      - mongo    
    command: node app.js

  admin:
    build: ./admin
    ports:
      - "1123"
    links:
      - mongo
      - redis
    command: node admin_app.js

  nginx:
    build: ./nginx
    ports:
      - "80:80"
      - "1123:1123"
      - "4455:4455"
    links:
      - app:app
      - admin:admin

  mongo:
    image: mongo:2.4
    environment:
      - MONGO_DATA_DIR=/data/db
    volumes:
      - mongostorage:/data/db
    ports:
      - "27017:27017"

  redis:
    image: redis
    volumes:
      - ./data/redis/db:/data/db
    ports:
      - "6379:6379"    

应用的dockerfile

FROM node:9.8
RUN mkdir -p /var/www/app
WORKDIR /var/www/app
COPY . /var/www/app
RUN npm install -g gulp pm2 notify-send
RUN npm install 
CMD ["pm2-docker", "./bin/www"]

管理员的dockerfile

FROM node:9.8
RUN mkdir -p /var/www/sibkladmin
WORKDIR /var/www/sibkladmin
COPY . /var/www/sibkladmin
RUN npm install -g gulp pm2 bcrypt
RUN npm install 

nginx的dockerfile

FROM nginx:latest

EXPOSE 80
EXPOSE 1123
EXPOSE 4455

COPY nginx.conf /etc/nginx/nginx.conf

nginx.conf

events {
  worker_connections  1024;
}

http{

    upstream app.local{
        least_conn;
        server app:3000 weight=10 max_fails=3 fail_timeout=30s;
    }

    upstream app.local:4455{
        least_conn;
        server adminmongo:4455 weight=10 max_fails=3 fail_timeout=30s;
    }

    upstream app.local:1123{
        least_conn;
        server admin:1123 weight=10 max_fails=3 fail_timeout=30s;
    }



    server {
        listen 80;

        server_name app.local;

        location / {
            proxy_pass http://app.local;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }

    server {
        listen 1123;

        server_name app.local:1123;

        location / {
            proxy_pass http://app.local:1123;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }

    server {
        listen 4455;

        server_name app.local:4455;

        location / {
            proxy_pass http://sibklapp.local:4455;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
}

已更新:

在docker-compose构建之后收到错误

npm ERR! path /var/www/admin/node_modules/bcrypt/node_modules/abbrev
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall rename
npm ERR! enoent ENOENT: no such file or directory, rename '/var/www/admin/node_modules/bcrypt/node_modules/abbrev' -> '/var/www/admin/node_modules/bcrypt/node_modules/.abbrev.DELETE'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-07-05T10_03_43_640Z-debug.log
ERROR: Service 'admin' failed to build: The command '/bin/sh -c npm install' returned a non-zero code: 254

2 个答案:

答案 0 :(得分:2)

您可以尝试:

NGINX dockerfile

更改:

COPY nginx.conf /etc/nginx/nginx.conf

COPY nginx.conf /etc/nginx/nginx.conf.d

删除EXPOSE端口无需公开端口。

撰写文件

将撰写文件更新为:

version: "2"

volumes: 
  mongostorage:  

services:  
  app:
    build: ./app
    ports:
      - "3000:3000"
    links:
      - mongo
      - redis
    volumes:
      - ./app:/var/www/app
      - /var/www/app/node_modules
    command: node app.js

  adminmongo:
    build: ./adminMongo
    ports:
      - "4455:4455"
    links:
      - mongo    
    volumes:
      - ./adminMongo:/var/www/adminMongo
      - /var/www/adminMongo/node_modules
    command: node app.js

  admin:
    build: ./admin
    ports:
      - "1123:1123"
    links:
      - mongo
      - redis
    volumes:  
      - ./admin:/var/www/admin
      - /var/www/admin/node_modules
    command: node admin_app.js

  nginx:
    build: ./nginx
    links:
      - app:app
      - admin:admin

  mongo:
    image: mongo:2.4
    environment:
      - MONGO_DATA_DIR=/data/db
    volumes:
      - mongostorage:/data/db
    ports:
      - "27017:27017"

  redis:
    image: redis
    volumes:
      - ./data/redis/db:/data/db
    ports:
      - "6379:6379"    

注意  我创建了一个简单的节点hello world应用程序进行测试,您将需要更新command:以匹配package.json

中的内容

在启动应用程序之前,请运行以下命令来清理所有旧的容器/网络:

    docker-compose kill
    docker-compose down
    docker network prune
    docker volume prune

要启动服务,请运行:

docker-compose build

可能会导致过度杀伤,但请确保您没有使用旧的容器

docker-compose up --force-recreate

本地测试输出

    mongo_1       | Wed Jul  4 21:50:35.835 [initandlisten] waiting 
    for connections on port 27017

    mongo_1       | Wed Jul  4 21:50:35.835 [websvr] admin web console 
    waiting for connections on port 28017

    app_1         | app listening on port 3000!
    admin_1       | admin app listening on port 1123!
    adminmongo_1  | AdminMongo app listening on port 4455!

    redis_1       | 1:M 04 Jul 21:50:26.523 * Running mode=standalone, 
    port=6379.

答案 1 :(得分:0)

该问题表明您没有使用node_modulesDockerfile内安装npm install或使用COPY . /src这样的语句覆盖了主机上的那些内容。

由于您使用的模块是本机模块,因此需要在docker映像内进行编译,而不仅仅是从其他地方复制,否则除非主机操作系统版本和Image OS版本相同,否则它们将不兼容。