immich-app / immich

High performance self-hosted photo and video management solution.
https://immich.app
GNU Affero General Public License v3.0
49.55k stars 2.61k forks source link

[BUG] immich_server and microservices can't connect to DB #4535

Closed darkra66it closed 1 year ago

darkra66it commented 1 year ago

The bug

Using almost-vanilla configuration as per the wiki, I've not been able to get this deployed via the docker compose method.

When checking container status, immich_server and immich_microservices are crashing/restarting constantly, so I'm not able to access the web portal.

Diving deeper, the docker logs for immich_server and immich_microservices show connection timeouts for both the redis and postgres DB (but mostly the redis) like:

[Nest] 7 - 10/18/2023, 8:22:58 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (8)... Error: Connection terminated due to connection timeout at Connection. (/usr/src/app/node_modules/pg/lib/client.js:132:73) at Object.onceWrapper (node:events:628:28) at Connection.emit (node:events:514:28) at Socket. (/usr/src/app/node_modules/pg/lib/connection.js:63:12) at Socket.emit (node:events:514:28) at TCP. (node:net:337:12) Error: connect EHOSTUNREACH 172.18.0.6:6379 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) { errno: -113, code: 'EHOSTUNREACH', syscall: 'connect', address: '172.18.0.6', port: 6379 }

Checking logs for redis, last message "ready to accept connections"

Checking logs for postgres, last message: "database system is ready to accept connections"

The OS that Immich Server is running on

Ubuntu 18.04 ARM64

Version of Immich Server

latest

Version of Immich Mobile App

n/a

Platform with the issue

Your docker-compose.yml content

version: "3.8"

services:
  immich-server:
    container_name: immich_server
    image: ghcr.io/immich-app/immich-server:${IMMICH_VERSION:-release}
    command: ["start.sh", "immich"]
    volumes:
      - ${UPLOAD_LOCATION}:/usr/src/app/upload
      - /etc/localtime:/etc/localtime:ro
    env_file:
      - .env
    depends_on:
      - redis
      - database
      - typesense
    restart: always

  immich-microservices:
    container_name: immich_microservices
    image: ghcr.io/immich-app/immich-server:${IMMICH_VERSION:-release}
    # extends:
    #   file: hwaccel.yml
    #   service: hwaccel
    command: ["start.sh", "microservices"]
    volumes:
      - ${UPLOAD_LOCATION}:/usr/src/app/upload
      - /etc/localtime:/etc/localtime:ro
    env_file:
      - .env
    depends_on:
      - redis
      - database
      - typesense
    restart: always

  immich-machine-learning:
    container_name: immich_machine_learning
    image: ghcr.io/immich-app/immich-machine-learning:${IMMICH_VERSION:-release}
    volumes:
      - model-cache:/cache
    env_file:
      - .env
    restart: always

  immich-web:
    container_name: immich_web
    image: ghcr.io/immich-app/immich-web:${IMMICH_VERSION:-release}
    env_file:
      - .env
    restart: always

  typesense:
    container_name: immich_typesense
    image: typesense/typesense:0.24.1@sha256:9bcff2b829f12074426ca044b56160ca9d777a0c488303469143dd9f8259d4dd
    environment:
      - TYPESENSE_API_KEY=${TYPESENSE_API_KEY}
      - TYPESENSE_DATA_DIR=/data
      # remove this to get debug messages
      - GLOG_minloglevel=1
    volumes:
      - tsdata:/data
    restart: always

  redis:
    container_name: immich_redis
    image: redis:6.2-alpine@sha256:70a7a5b641117670beae0d80658430853896b5ef269ccf00d1827427e3263fa3
    restart: always

  database:
    container_name: immich_postgres
    image: postgres:14-alpine@sha256:28407a9961e76f2d285dc6991e8e48893503cc3836a4755bbc2d40bcc272a441
    env_file:
      - .env
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_USER: ${DB_USERNAME}
      POSTGRES_DB: ${DB_DATABASE_NAME}
    volumes:
      - pgdata:/var/lib/postgresql/data
    restart: always

  immich-proxy:
    container_name: immich_proxy
    image: ghcr.io/immich-app/immich-proxy:${IMMICH_VERSION:-release}
    environment:
      # Make sure these values get passed through from the env file
      - IMMICH_SERVER_URL
      - IMMICH_WEB_URL
    ports:
      - 2283:8080
    depends_on:
      - immich-server
      - immich-web
    restart: always

volumes:
  pgdata:
  model-cache:
  tsdata:

Your .env content

# You can find documentation for all the supported env variables at https://immich.app/docs/install/environment-variables

# The location where your uploaded files are stored
UPLOAD_LOCATION=/home/ubuntu/immich-app/uploads

# The Immich version to use. You can pin this to a specific version like "v1.71.0"
IMMICH_VERSION=release

# Connection secrets for postgres and typesense. You should change these to random passwords
TYPESENSE_API_KEY=some-random-text
DB_PASSWORD=postgres

# The values below this line do not need to be changed
###################################################################################
DB_HOSTNAME=immich_postgres
DB_USERNAME=postgres
DB_DATABASE_NAME=immich

REDIS_HOSTNAME=immich_redis

Reproduction steps

1. download sample config files
2. change upload_location in .env
3. docker compose up -d
4. docker compose down and then up again

Additional information

No response

jrasm91 commented 1 year ago

So you have any networking things going on? If your run docker compose up database redis do they come up correctly?

darkra66it commented 1 year ago

So you have any networking things going on? If your run docker compose up database redis do they come up correctly?

I'm new to Docker / Docker networking so not sure where to start troubleshooting.

ubuntu@immich-ubuntu-arm64:~/immich-app$ sudo docker compose up -d [+] Running 9/9 ✔ Network immich-app_default Created 0.0s ✔ Container immich_web Started 0.9s ✔ Container immich_redis Started 0.8s ✔ Container immich_typesense Started 1.0s ✔ Container immich_machine_learning Started 0.8s ✔ Container immich_postgres Started 0.4s ✔ Container immich_microservices Started 1.5s ✔ Container immich_server Started 1.4s ✔ Container immich_proxy Started 1.8s

From searching around, my guess is that there is something blocking the inter-container traffic?

immich_server | [Nest] 7 - 10/18/2023, 10:51:04 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (9)... immich_server | Error: connect EHOSTUNREACH 172.21.0.2:5432 immich_server | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) immich_server | [Nest] 7 - 10/18/2023, 10:51:04 PM ERROR [ExceptionHandler] connect EHOSTUNREACH 172.21.0.2:5432 immich_server | Error: connect EHOSTUNREACH 172.21.0.2:5432 immich_server | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) immich_microservices | Error: connect EHOSTUNREACH 172.21.0.3:6379 immich_microservices | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) { immich_microservices | errno: -113, immich_microservices | code: 'EHOSTUNREACH', immich_microservices | syscall: 'connect', immich_microservices | address: '172.21.0.3', immich_microservices | port: 6379 immich_microservices | }

darkra66it commented 1 year ago

Ok, I've been able to resolve this -- it appears that installing docker using snapis a bad idea.

I ran sudo snap remove docker --purge to wipe it out completely. Then I installed docker via https://docs.docker.com/engine/install/ubuntu/ and rebuilt everything from scratch.

sudo docker compose up -d now works as expected, I can login and start experimenting.

Devqon commented 10 months ago

I had the same issue when installing Immich with docker-compose on my Synology NAS. Guess it is related to the firewall issues mentioned above. A restart of the NAS seems to have fixed it (only disabling and re-enabling the firewall did not work).

Can anybody explain why this happens? It also only happens with Immich, and it happened on all my tries consistently

ihavenokia commented 8 months ago

I'm running a ubuntu server, with a lot of containers working fine, but I'm getting this message when running immich stack (portainer compose or terminal). I've tried to use a db server that I use for other apps, but changing that didn't help. Any ideias what might be blocking this?

gregwym commented 1 month ago

Thanks @Devqon , it's indeed a firewall issue.

For anyone who's using Immich on Synology NAS, rather than turning off firewall entirely, you need to allow the immich_default network subnet to access other ports through the firewall. (You may use more specific ports to make it more secure.)

  1. Build the Immich docker compose project
  2. Go to Container Manger -> Network -> expand immich_default and write down the subnet.
  3. Go to Control Panel -> Security -> Firewall -> Edit Rules
  4. Add a new rule
    • Ports: All
    • Source IP: Specific IP
      • Choose Subnet
      • IP address: the immich_default subnet value without /16
      • Subnet mask: 255.255.0.0
    • Action: Allow
    • save the rules

Please see my rules as an example. image

p3rb3r commented 3 weeks ago

Thanks @Devqon , it's indeed a firewall issue.

For anyone who's using Immich on Synology NAS, rather than turning off firewall entirely, you need to allow the immich_default network subnet to access other ports through the firewall. (You may use more specific ports to make it more secure.)

1. Build the Immich docker compose project

2. Go to Container Manger -> Network -> expand `immich_default` and write down the subnet.

3. Go to Control Panel -> Security -> Firewall -> Edit Rules

4. Add a new rule

   * Ports: All
   * Source IP: Specific IP

     * Choose `Subnet`
     * IP address: the `immich_default` subnet value without `/16`
     * Subnet mask: `255.255.0.0`
   * Action: Allow
   * save the rules

Please see my rules as an example. image

Confirm as a fix. Seen people asking about this for ages well done