我正在通过docker compose运行一个基于Node和Redis的React应用:
version: "3"
services:
webapp:
build: ./
ports:
- "127.0.0.1:3000:9090"
depends_on:
- redis
command:
npm run start
nginx:
build: ./nginx
ports:
- "80:80"
environment:
- NGINX_HOST=127.0.0.1
- NGINX_PORT=80
command:
service nginx start
redis:
image: redis
ports:
- "6379:6379"
volumes:
- ./data:/data
Docker文件:
FROM node:alpine
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 9090
RUN npm run build_prod
Server.js:
const redisClient = RedisClient.createClient(6379,'redis');
当我运行 docker-compose up --build 时,我遇到了 Redis 连接被拒绝的错误。
redis_1 | 1:M 15 Nov 13:55:19.865 * Ready to accept connections
webapp_1 |
webapp_1 | > web.globalmap.fatmap.com@0.0.1 start /usr/src/app
webapp_1 | > node ./build/server.js
webapp_1 |
webapp_1 | events.js:193
webapp_1 | throw er; // Unhandled 'error' event
webapp_1 | ^
webapp_1 |
webapp_1 | Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
webapp_1 | at Object._errnoException (util.js:1031:13)
webapp_1 | at _exceptionWithHostPort (util.js:1052:20)
webapp_1 | at TCPConnectWrap.afterConnect [as oncomplete]
我想知道如何正确地将两个Docker容器链接起来。