繁体   English   中英

当docker-compose up时,nodejs app退出代码0

[英]nodejs app exited with code 0 when docker-compose up

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

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

泊坞窗,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

你能试一下吗:

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 app来测试,你需要更新command:匹配你在package.json中的内容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. 

问题表明您没有使用npm installDockerfile安装node_modules ,或者使用COPY . /src覆盖了来自主机的那些COPY . /src COPY . /src类型的声明。

由于您使用的模块是本机模块,因此需要在docker镜像内编译,而不是仅从其他地方复制,因为除非Host OS版本和Image OS版本相同,否则它们将不兼容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM