"Connection closed" accessing neo4j in Docker from node.js in Docker on Mac
Answer a question
I'm running a neo4j database as well as an node.js api in a Docker container. On startup, the api tries to import a lot of data into neo4j, accessing it through neo4-driver. I can also run those imports manually from inside the Docker container or outside (locally).
This used to work fine. It works fine on Windows, it works fine for other people using a Macbook, and sometimes it also works for me, but then the connection mysteriously drops and I only get ECONNREFUSED errors anymore, until I restart the container. Neither neo4j nor Docker give any indication that anything is wrong. Nothing in the logs, they don't seem to have crashed, they're just inaccessible. The neo4j browser interface on port 7474 is also gone, though it used to be there.
The fact that this sometimes works is the baffling part. But it never works for long, before I get the errors again. When it drops, I often (not always) first get an EPIPE error.
I used to run Docker/docker desktop 3.1 and 10.20.2, now upgraded to 3.6.0 and 10.20.8. Neo4j version 3. The api runs on Alpine.
I use docker-compose -p 7687:7687 -f ./docker-compose.yml up to start Docker, and my docker-compose.yml file looks like this:
version: '3'
services:
neo4j:
build:
context: ./db/
ports:
- "7474:7474"
- "7687:7687"
api:
build:
context: ./api/
volumes:
- ./api/:/code/
depends_on:
- neo4j
ports:
- "3000:3000"
I execute queries to neo4j with:
const session = driver.session();
session.writeTransaction((tx) => tx.run(query, params))
where driver was previously initialized with
const driver = neo4j.driver(neo4jUrl, neo4j.auth.basic(user, passwd), {});
The most common error is this:
Neo4jError: connect ECONNREFUSED 172.20.0.2:7687
at captureStacktrace (/code/node_modules/neo4j-driver/lib/v1/result.js:199:15)
at new Result (/code/node_modules/neo4j-driver/lib/v1/result.js:65:19)
at _newRunResult (/code/node_modules/neo4j-driver/lib/v1/transaction.js:354:10)
at Object.run (/code/node_modules/neo4j-driver/lib/v1/transaction.js:238:14)
at Transaction.run (/code/node_modules/neo4j-driver/lib/v1/transaction.js:104:26)
...
at TransactionExecutor._safeExecuteTransactionWork (/code/node_modules/neo4j-driver/lib/v1/internal/transaction-executor.js:134:22)
at TransactionExecutor._executeTransactionInsidePromise (/code/node_modules/neo4j-driver/lib/v1/internal/transaction-executor.js:122:32)
at Timeout._onTimeout (/code/node_modules/neo4j-driver/lib/v1/internal/transaction-executor.js:98:18)
at listOnTimeout (internal/timers.js:557:17) {
code: 'ServiceUnavailable'
I suspect it might have something to do with Docker differences between Windows and Mac, but I'm not sure. It has also worked fine with other Mac users, and occasionally does work for me, just not for long. So what could cause this connection to suddenly drop?
Answers
This issue seems more related to your environment than containers.
Could you check :
-
Computers resources available : RAM and CPU
You can limit CPU/RAM usage of a container with docker-compose --compatibility
https://docs.docker.com/compose/compose-file/compose-file-v3/#deploy
services: app: ... deploy: resources: limits: memory: 2g cpus: '1.4' -
OS version : Containers share kernel with your OS, an old kernel (or brand new) may have effects on your containers.
-
Docker version : This process runs your containers, you've upgraded it however.
-
Network : I've had and issue where IP used by my container was in conflict with an existing one, your can force an ip for your container.
https://docs.docker.com/compose/compose-file/compose-file-v3/#ipam
services: app: networks: - backend networks: backend: ipam: driver: default config: - subnet: "10.8.0.0/24"
EDIT 1
EPIPE error could be the main issue in fact.
Could you try to increase resource limits in docker desktop ?
Docker Desktop -> Settings -> Resources -> Advanced
EDIT 2
It is normal that you are not able to find neo4j hostname from your host.
- Neo4j run inside a container
neo4jhostname is a docker mechanism working for other containers in the same docker network.- outside docker, try to connect to
localhostinstead or127.0.0.1 - regarding your compose file, ports are already exposed.
更多推荐
所有评论(0)