Второй, надо заставить монгу слушать либо на 0.0.0.0 (то есть на всех интерфейсах)
sudo lsof -i -P -n | grep LISTEN
systemd-r 568 systemd-resolve 13u IPv4 19859 0t0 TCP 127.0.0.53:53 (LISTEN)
container 627 root 12u IPv4 24719 0t0 TCP 127.0.0.1:39081 (LISTEN)
sshd 655 root 3u IPv4 24561 0t0 TCP *:22 (LISTEN)
sshd 655 root 4u IPv6 24563 0t0 TCP *:22 (LISTEN)
mongod 8733 mongodb 11u IPv4 112559 0t0 TCP *:27017 (LISTEN)
docker-pr 11010 root 4u IPv4 129502 0t0 TCP *:5000 (LISTEN)
docker-pr 11016 root 4u IPv6 129506 0t0 TCP *:5000 (LISTEN)
подключаться к ней из контейнера по IP шлюза (его можно посмотреть в выводе docker inspect )
version: '3.8'
services:
server:
container_name: backend
image: skellord/tww:backend
build: ./
restart: unless-stopped
env_file: ./.env
environment:
- MONGO_URI=mongodb://host.docker.internal:27017/tww?retryWrites=true
ports:
- '5000:5000'
extra_hosts:
- 'host.docker.internal:172.22.0.1'
info: MongooseServerSelectionError: Server selection timed out after 30000 ms
at Ad.ye.openUri (/app/build/index.js:10:209339)
at /app/build/index.js:49:24676
at /app/build/index.js:7:26681
at new Promise (<anonymous>)
at HM.exports (/app/build/index.js:7:26667)
at X._promiseOrCallback (/app/build/index.js:49:28819)
at X.connect (/app/build/index.js:49:24649)
at vNe (/app/build/index.js:141:41877)
at Object.<anonymous> (/app/build/index.js:141:42043)
at Module._compile (node:internal/modules/cjs/loader:1101:14)