当我在Docker的同一网桥网络上运行时,我正在尝试使我的Node.js应用程序与我的Elasticsearch数据库对话。我使用docker-compose.yml文件进行配置。这是相关的位和我收到的错误消息。
version: '3'
services:
express-app:
build: /path/to/app
container_name: my_app
depends_on:
- 'elasticsearch'
environment:
- NODE_ENV=local
- ES_HOST=elasticsearch
- PORT=3000
ports:
- 3000:3000
networks:
- backend
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.0
container_name: es-master
environment:
- discovery.type=single-node
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- esdata:/usr/share/elasticsearch/data
ports:
- 9200:9200
- 9300:9300
networks:
- backend
volumes:
esdata:
driver: local
networks:
backend:
我希望服务(express-app
和elasticsearch
都可以在创建的Docker桥接网络“ my_app_backend”上以其服务名称被发现。官方Docker documentation指出“服务可以在主机名上访问。”我的意图是在地址“ {http://elasticsearch:9200”处访问服务elasticsearch。
const { Client } = require('@elastic/elasticsearch');
const hostname = process.env.ES_HOST || 'localhost';
const client = new Client({
node: `http://${hostname}:9200`,
log: 'error'
});
async function checkConnection () {
let isConnected = false;
console.log('Connecting to host', hostname);
try {
const health = await client.cluster.health({});
console.log(health);
isConnected = true;
} catch (err) {
console.log('process.env.ES_HOST', process.env.ES_HOST);
console.log('Connection Failed\n', err);
}
}
checkConnection();
module.exports.client = client;
以下消息显示Node.js应用尝试通过process.env.ES_HOST变量'elasticsearch'(在docker-compose.yml文件中定义)连接到Elasticsearch的结果,并在容器运行时执行。错误消息显示了我放入的两个控制台日志,以检查变量的值是否正确。
ECONNREFUSED之后显示的IP地址是Docker引擎替换为“ elasticsearch”的地址。当我运行输出容器的网络地址的docker network inspect my_app_backend
时,也会发现这一点。
... (redacted)
"MacAddress": "02:42:ac:14:00:03",
"IPv4Address": "172.20.0.3/16",
"IPv6Address": ""
> NODE_ENV=production node ./server/index.js
Connecting to host elasticsearch
Server listening on port 3000!
process.env.ES_HOST elasticsearch
Connection Failed
ConnectionError: connect ECONNREFUSED 172.20.0.3:9200
at onResponse (/src/app/node_modules/@elastic/elasticsearch/lib/Transport.js:214:13)
at ClientRequest.<anonymous> (/src/app/node_modules/@elastic/elasticsearch/lib/Connection.js:98:9)
at ClientRequest.emit (events.js:311:20)
at Socket.socketErrorListener (_http_client.js:426:9)
at Socket.emit (events.js:311:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
name: 'ConnectionError',
meta: {
body: null,
statusCode: null,
headers: null,
warnings: null,
meta: {
context: null,
request: [Object],
name: 'elasticsearch-js',
connection: [Object],
attempts: 3,
aborted: false
}
}
}
此外,我想指出应用程序在启动时无法连接,但是当我通过docker exec -it express-app sh
进入应用程序容器并编辑数据库连接代码并硬编码数据库的IP地址(172.20.0.3 ),然后重新启动节点应用程序,该应用程序已成功连接到数据库。
在通过docker-compose完全自动化的尝试中我做错了什么?这是Elasticsearch的怪癖还是我遗漏的很明显的东西?预先感谢您的帮助!
答案 0 :(得分:0)
在express_app中使用container_name : es-master
代替elasticsearch
作为ES_HOST
env变量
ES_HOST : es-master
答案 1 :(得分:-1)
我认为您应该链接elasticsearch容器
version: '3'
services:
express-app:
build: /path/to/app
container_name: my_app
restart: always
depends_on:
- 'elasticsearch'
environment:
- NODE_ENV=local
- ES_HOST=elasticsearch
- PORT=3000
ports:
- 3000:3000
networks:
- backend
links:
- elasticsearch
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.0
container_name: es-master
environment:
- discovery.type=single-node
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- esdata:/usr/share/elasticsearch/data
ports:
- 9200:9200
- 9300:9300
networks:
- backend
volumes:
esdata:
driver: local
networks:
backend: