我正在尝试容器化一个nodejs应用程序。该应用程序在我手动安装并运行redis的节点容器上运行良好,但是当我尝试使用docker-compose文件在容器中运行该应用程序时,出现错误:
“错误错误:Redis与localhost:6379的连接失败-连接ECONNREFUSED 127.0.0.1:6379”。
当我尝试执行docker-compose up时,我将在下面发布docker-compose.yml和dockerfile以及控制台日志。
addons
FROM node:8-jessie
WORKDIR /var/api-console
COPY package*.json ./
RUN npm install
COPY . . /var/api-console/
RUN apt-get update
RUN apt-get install python
EXPOSE 3000
version: '3'
services:
redis:
image: redis
ports:
- "6379:6379"
command:
redis-server
networks:
- webnet
app:
build: ./
volumes:
- ./:/var/api-console
ports:
- 3000:3000
command:
node app.js
networks:
- webnet
networks:
webnet:
答案 0 :(得分:1)
日志中的这一行说明了连接失败
Redis与localhost:6379的连接失败
节点应用程序期望redis位于本地主机上,而不是。
您可以通过环境变量将redis容器的地址提供给您的节点应用。
还要在应用程序服务中添加一个depends_on,以使其等待redis服务。下面修改后的撰写文件
version: '3'
services:
redis:
image: redis
ports:
- "6379:6379"
command:
redis-server
networks:
- webnet
app:
build: ./
volumes:
- ./:/var/api-console
ports:
- 3000:3000
depends_on:
- redis
environment:
redis_server_addr: redis
#The dependent service address is set in environment variable which you can use in your app to connect
command:
node app.js
networks:
- webnet
networks: