gpt4 book ai didi

node.js - nodejs 应用程序在 docker-compose up 时以代码 0 退出

转载 作者:搜寻专家 更新时间:2023-10-31 23:50:38 24 4
gpt4 key购买 nike

我在运行 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"]

管理员的 docker 文件

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 的 docker 文件

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 docker 文件

改变:

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"

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

中的内容

还有在开始您的应用程序之前,请运行这些命令来清理所有旧容器/网络:

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

启动服务运行:

docker-compose 构建

可能矫枉过正,但要确保你没有使用旧容器

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.

关于node.js - nodejs 应用程序在 docker-compose up 时以代码 0 退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51121848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com