我正在通过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正确链接两个容器。
看起来redis
无法解析为正确的IP地址。
创建客户端时,尝试redis URI。
// [redis:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]] const redisClient = RedisClient.createClient('redis://redis:6379');
如果合适,提供用户名,密码和数据库编号。