DIY Photo Hosting with Immich
For a while now, I’ve been trying to share some family photos with family and friends. None of the services I already pay for quite did what I wanted. After a quick search, I found Immich which basically does everything that I wanted.
Since I now have a spare N100 Mini PC, I decided to just set this up as a NAS/Immich server.
PC specs:
- 500GB OS SSD
- 4TB SSD (ZFS)
- N100
- 16GB RAM
- Debian Trixie (13)
At the start of this exercise, the PC just has the plain OS + Docker installed and ZFS mounted at /tank.
Self-hosting considerations
The installation instructions are pretty clear: the recommendation is Docker Compose.
For a small-time site with limited users and importance, that makes perfect sense, so let’s skip Kubernetes and just things the easy way for once.
Following the guide while logged in as geoff gave me a server that was fast and had all the features I wanted around album management along with new features like face recognition that work entirely within the bounds of the Mini PC without helping Meta train its AI.
After five minutes, I was “sold” (it’s free - AGPL).
For many users, just following the steps in the guide will be enough and gives a server that works fine when accessed over the LAN or VPN but since I want to share photos securely without giving VPN access, forwarding a port, or dealing with other people’s passwords, I decided to make a few improvements:
- Docker Compose - as recommended in the manual
- Systemd service to match the way I normally manage my Linux services
- ZFS storage (to access the 4TB SSD)
- Cloudflare tunnel for secure access to the Immich server on the Mini PC - this also takes care of the TLS service
- Google single sign-on (OAuth/OIDC)
- Secure access over the lan with Traefik (TLS)
There’s one important caveat with Cloudflare and that’s that there is a 100MB file upload limit. I do have a couple of long videos where I decided to record my trip across the HZMB but for the most part, uploads are just normal photos so under 10MB.
I can keep Cloudflare and just do large uploads over the LAN or VPN if needed. Fixing this while staying on Cloudflare would require an business/enterprise plan, which is fair enough really.
ZFS setup
The first thing to do was setup the ZFS resources. I took some advice from Claude and used a different block size for the library vs database to tune SSD wear, and to give Immich files a dedicated home since there wasn’t already a suitable dataset created:
sudo zfs create -o compression=lz4 -o atime=off -o xattr=sa tank/immich
sudo zfs create -o recordsize=1M tank/immich/library
sudo zfs create -o recordsize=16K tank/immich/postgres
Cross-check:
zfs list -r -o name,mountpoint,recordsize,compression tank/immich
NAME MOUNTPOINT RECSIZE COMPRESS
tank/immich /tank/immich 128K lz4
tank/immich/library /tank/immich/library 1M lz4
tank/immich/postgres /tank/immich/postgres 16K lz4
One nice perk as a ZFS newbie is not having to mess with /etc/fstab to activate the storage - it just pops up automatically:
geoff@nas:~$ tree /tank
/tank
├── borg
├── images
├── immich
│ ├── library
│ └── postgres
└── share
Docker compose setup
The instructions recommend to try the new docker compose builder which on the whole works quite nicely.
The caveat is that the .env file override variables described in the manual won’t work with this generated file, since it deliberately doesn’t reference them.
This is fair enough - required values can be inserted directly into docker-compose.yml and the .env file done away with.
My final docker-compose.yml looks something like this:
name: immich
services:
immich-server:
container_name: immich_server
image: ghcr.io/immich-app/immich-server:v3
volumes:
- /tank/immich/library:/data
- /etc/localtime:/etc/localtime:ro
environment:
TZ: Australia/Sydney
DB_PASSWORD: CHANGEME
ports:
- 2283:2283
depends_on:
- redis
- database
restart: always
healthcheck:
disable: false
devices:
- /dev/dri:/dev/dri
immich-machine-learning:
container_name: immich_machine_learning
image: ghcr.io/immich-app/immich-machine-learning:v3-openvino
volumes:
- model-cache:/cache
environment:
MACHINE_LEARNING_MODEL_REVISION: v2
restart: always
healthcheck:
disable: false
devices:
- /dev/dri:/dev/dri
redis:
container_name: immich_redis
image: docker.io/valkey/valkey:9@sha256:3add1a336a920ea54770a001ab6feb3da5fceea96a40373a3a2cd469b815f0b7
healthcheck:
test: redis-cli ping | grep -q PONG || exit 1
restart: always
database:
container_name: immich_postgres
image: ghcr.io/immich-app/postgres:14-vectorchord0.4.3-pgvectors0.2.0@sha256:bcf63357191b76a916ae5eb93464d65c07511da41e3bf7a8416db519b40b1c23
environment:
POSTGRES_PASSWORD: CHANGEME
POSTGRES_USER: postgres
POSTGRES_DB: immich
POSTGRES_INITDB_ARGS: --data-checksums
DB_STORAGE_TYPE: SSD
volumes:
- /tank/immich/postgres:/var/lib/postgresql/data
shm_size: 128mb
restart: always
healthcheck:
disable: false
volumes:
model-cache:
Key features:
- No
.envfile needed! - Database password should be set to something secure before first starting Immich (not
CHANGEME!). It needs to be configured in the environment for bothdatabaseandimmich-serveras above, and noteDB_PASSWORDvsPOSTGRES_PASSWORD. If you accidentally started postgres without setting a password, it will be the defaultpostgresand you would have to fix this up yourself - Postgres and image library will be on ZFS
- Machine learning cache will be on a docker managed permanent volume, not
tmpfsor a bind mounted volume (check:docker volume inspect immich_model-cache):- This is the recommended approach in the FAQ
- Models and caches can be rebuild if needed
- Excluding them from the bind mounts and corresponding backups should reduce space needed considerably
The compose file just needs a directory to run from on the system. I settled on /opt/immich:
mkdir /opt/immich
With the above file pasted into /opt/immich/docker-compose.yml, I created a systemd service file at /etc/systemd/system/immich.service:
# /etc/systemd/system/immich.service
[Unit]
Description=Immich photo server (docker compose)
Requires=docker.service zfs-mount.service
After=docker.service zfs-mount.service network-online.target
Wants=network-online.target
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/opt/immich
# Refuse to start if the ZFS datasets aren't mounted, so Immich never
# writes into empty directories on the root disk.
ExecStartPre=/usr/bin/mountpoint -q /tank/immich/library
ExecStartPre=/usr/bin/mountpoint -q /tank/immich/postgres
ExecStart=/usr/bin/docker compose up -d --remove-orphans
ExecStop=/usr/bin/docker compose down
ExecReload=/usr/bin/docker compose up -d --remove-orphans
TimeoutStartSec=5min
[Install]
WantedBy=multi-user.target
Which I can enable and start:
systemctl daemon-reload
systemctl enable immich
systemctl start immich
After a minute or so, you should be able to reach the Immich setup screen by navigating to port 2283.
Run through the setup wizard and create yourself an admin user with a password, upload some photos, etc, then reboot the server and check everything comes back up properly.
✅ Server running locally, using ZFS
Secure LAN access with Traefik (TLS)
I already figured out how to do Traefik for my podman services, so I will adapt my homelab ansible deployment script to install a TLS certificate for immich local access over the LAN.
Ansible generated a traefik drop-in config for TLS termination very easily:
# ansible managed
#
# HTTP
#
http:
routers:
immich-http:
# HostSNI only for TCP - we get the real headers for http routes
# as TLS already terminated and http call parsed
rule: "Host(`immich.infrastructure.asio`)"
tls: {}
entryPoints:
- default.https
service: immich-2283
services:
immich-2283:
loadBalancer:
servers:
- url: "http://localhost:2283"
✅ Server available over the LAN, using TLS and my private CA
Database (PostgreSQL)
Before proceeding, check access to Postgres works with the password set in docker-compose.yml, to prove it can be accessed easily for backups and has been correctly secured. This is a defense-in-depth measure since PostgreSQL can’t be accessed from outside the compose network. Make sure to run the test from the immich-server container, since the database one is already authenticated:
# get a shell in the `immich-server` container
docker compose exec immich-server bash
# should prompt for a password and accept the one from `docker-compose.yml`, rejecting the
# default password which is `postgres`
psql -U postgres -d immich -h database -p 5432
Cloudflare Tunnel
This step assumes you already have a Cloudflare account and a domain you can plug new hosts into.
No point to duplicate the Cloudflare manual here, so lets just list the main steps:
- Create a tunnel to the immich server (Networking -> Tunnels):
- Route should be from whatever hostname you chose to published application at
http://localhost:2283 - No point to tunnel via Traefik - that’s only useful to protect data in-flight over the LAN and I configured it to terminate TLS anyway
- Route should be from whatever hostname you chose to published application at
✅ Server available over the internet via Cloudflare tunnel, with real TLS certificate
Note that Cloudflare is exposing the login page from Immich as-is, so we rely on the security of the app to ensure nothing silly is happening like a magic URL that bypasses all authentication. Since there are many Immich user and it’s established software, this isn’t any more concerning than exposing any other app to the Internet.
Single sign-on (OAuth/OIDC)
This is the magic that will allow anyone with a Gmail account to login to the server via a 3-way flow between Google, Immich and Cloudflare.
It’s the same authentication method I help enterprises with all the time, so it should be easy right? Read the gory details of how this all works if you like (recommended). The key point here is that if someone tries to login to immich, they are not sending their Google password to my little server, instead, 3 way trust is setup and a Google user who chooses to login will have Cloudflare send an authenticated token to Immich with just the details that were shared at the time of login. The important one is email address, which is used to map an OIDC principal to an Immich user.
There are 3 distinct systems that all need to be setup at the same time. If it doesn’t work after a few minutes, it’s normally easiest to delete all trust relationships and start over.
First off, check the docs:
These were very helpful to get setup but note that these cover the Cloudflare to Immich authentication flow. You still need to setup an Identity provider (Google) as well - the guide assumes this is already setup.
To recap:
Immich:
- Login to Immich as your admin user with username + password
- Administration page -> Authentication Settings -> OAuth
- Turn ON OAuth
Cloudflare:
- Zero Trust -> Access Control -> Applications
- Create new application (I had to agree to $0 plan for this)
- Choose
SaaS applicationsandOpenID Connect (OIDC), thenContinue with SaaS applications
On the next screen:
- Type the name
Immichand hit return to add it to the list - Remove
groupsfromscopeslist - Add each of the redirect URLs you plan on using, as illustrated in the Guide for Cloudflare, adjusted for the URLs you are serving from
- You have to paste one URL at a time, and click the flat
Add URLbutton to add more - Click
Reset Secretso you can copy the client secret, then click the copy button before it vanishes - The rest of this job just involves copy-pasting between Cloudflare and Immich admin screens:
- Client Secret -> Immich
- Issuer -> Immich (Issuer URL)
- Client Id -> Immich
- Save the Cloudflare application
- With the details now loaded in Immich:
- Auto Register -> off
- Mobile Redirect -> on, and paste the redirect URL from the Guide for Cloudflare
- Save
The last piece of the puzzle is to enable google authentication. This is done via the GCP cloud console. You should follow the official Google guide above for setting up OIDC on GCP, but once again, the main steps are:
- Create a new project
- Search for
Google Auth Platform(orOAuth) - Choose
Clientsfrom side bar - Create a new Client:
- Type:
Web App - Name:
cloudflare(or whatever you like) - Authorized Javascript origins: This is the public URL you setup previously
- Now leave this screen open and head back to Cloudflare
- Type:
On Cloudflare:
- Zero Trust -> Integrations -> Identity Providers -> Add an identity provider
- Choose
Google(to allow any google account) - Copy the callback URL (cloudflareaccess.com), now return to GCP
On GCP:
- Paste the callback URL into Authorized Redirect URIs
- Click
Create - Copy-paste the credentials on the next screen back into Cloudflare:
- Client ID
- Client secret
- Click OK in GCP, then click Save in Cloudflare
- A couple of minutes later, try the
testbutton in Cloudflare, it should sayYour connection works!if it’s working. If not, give it another 5 minutes before starting troubleshooting
Customise the login page details shown to users if needed:
- Zero Trust -> Reusable Components -> Custom pages
- Access login page
With the integration to google confirmed working, I just have to return to my SaaS application settings and select Google as the identity provider. After clicking save, I can try to login to the service by clicking the Login with OAuth button on the login screen. Initially this will give a message (from Cloudflare): That account does not have access which is because I did not yet setup an Access Policy.
I did this deliberately to prove the policy is working as intended. For my small site, whitelisting each allowed google account is fine.
With the policy updated, I can login. To add additional users, I need to both whitelist the email address in cloudflare and create the corresponding user with the identical email address and appropriate level of access in Immich.
✅ Server accepts single sign-on login from Google
After a little more testing, username and password access should be disabled in Immich settings. The break-glass to regain control if OAuth breaks for some reason is to use the immich CLI to regain access, eg:
docker exec -it immich_server immich-admin enable-password-login
docker exec -it immich_server immich-admin reset-admin-password # if you've forgotten it
# ...fix things, then:
docker exec -it immich_server immich-admin disable-password-login
Final production steps
- Disable password login in Immich UI
- Alter bind in
docker-compose.ymlto prevent access bypassing Traefik TLS (change ports line to127.0.0.1:2283:2283) and restart - Publish the app in GCP (or else others can’t login with Google):
- In the GCP console, open Google Auth Platform → Branding. Check that the app name, user support email and developer contact email are filled in. If they’re missing, publishing will fail or the button can stay greyed out.
- In Australia and possibly worldwide, you are also required ot provide a privacy policy and terms of service
- Go to Google Auth Platform → Audience. Check that User type is External.
- Under Publishing status, click Publish app and confirm.
And there we have it - our $0 photo-sharing server. Photos are private once uploaded but can be shared via Albums.
Todo
- Backups!
- Bind to 127.0.0.1 in docker-compose to prevent ability to bypass TLS over the LAN
- Enable hardware transcoding in settings
Post comment