Tuesday, July 10, 2018

nodejs app exited with code 0 when docker-compose up

Leave a Comment

I encounter the error below when running 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 for app

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 for admin

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  

dockerfile for nginx

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;         }     } } 

Updated:

Error received after docker-compose build

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 Answers

Answers 1

Can you try:

NGINX dockerfile

Change:

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

to

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

Remove EXPOSE ports no need to expose ports.

Compose File

Update compose file to:

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"     

NOTE I created a simple node hello world app to test, you will need to update the command: to match what you have in package.json

Also Before you start your app run these commands to clean up any old containers/networks:

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

To start the services Run:

docker-compose build

prob overkill but makes sure you're not using old containers

docker-compose up --force-recreate

Local Test Output

    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. 

Answers 2

The issue indicates that you didn't install node_modules inside the Dockerfile using npm install or you overwrote those from the host using COPY . /src kind of statement.

Since the module you are using is a native module it needs to be compiled inside the docker image and not just copied from some place else as the same will not be compatible until unless the Host OS version and Image OS version are the same

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment