docker 更換掛載點其實非常方便,倒不如說這部分就是 docker 會那麼好用的其中一個重要因素。一般來說,我們只要在 docker-compose.yml 中把 volumes 欄位設定一下,重新運行,一切就會像沒事人一樣,無痛搬家。

隨便拿一個簡單的 docker-compose 來看:

1
2
3
4
5
6
7
8
9
10
11
services:
glance:
container_name: glance
image: glanceapp/glance
restart: unless-stopped
volumes:
- ./config:/app/config
- ./assets:/app/assets
ports:
- 5678:8080
env_file: .env

我們可以看到在 volumes 跟 ports 裡面都有著 host:container 這種格式的寫法。在 volumes 中,這就表示你的容器會直接把宿主的 ./config 當成 /app/config 來取用。而在 ports 中,你的容器會把宿主的 5678 port 當成自己的 8080 port 來用。

按照這個邏輯,我們來看一下 immich 的 docker-compose:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
name: immich

services:
immich-server:
container_name: immich_server
image: ghcr.io/immich-app/immich-server:${IMMICH_VERSION:-release}
# extends:
# file: hwaccel.transcoding.yml
# service: cpu # set to one of [nvenc, quicksync, rkmpp, vaapi, vaapi-wsl] for accelerated transcoding
volumes:
# Do not edit the next line. If you want to change the media storage location on your system, edit the value of UPLOAD_LOCATION in the .env file
- ${UPLOAD_LOCATION}:/usr/src/app/upload
- /etc/localtime:/etc/localtime:ro
env_file:
- .env
ports:
- '2283:2283'
depends_on:
- redis
- database
restart: always
healthcheck:
disable: false

immich-machine-learning:
# 此處省略

redis:
# 此處省略

database:
container_name: immich_postgres
image: ghcr.io/immich-app/postgres:14-vectorchord0.3.0-pgvectors0.2.0
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_DB: ${DB_DATABASE_NAME}
POSTGRES_INITDB_ARGS: '--data-checksums'
# Uncomment the DB_STORAGE_TYPE: 'HDD' var if your database isn't stored on SSDs
# DB_STORAGE_TYPE: 'HDD'
volumes:
# Do not edit the next line. If you want to change the database storage location on your system, edit the value of DB_DATA_LOCATION in the .env file
- ${DB_DATA_LOCATION}:/var/lib/postgresql/data

restart: always

其中的 ${UPLOAD_LOCATION}:/usr/src/app/upload${DB_DATA_LOCATION}:/var/lib/postgresql/data 就是我們此次要下手的部分。

在下手之前,我們用 docker compose down 先行停機。

然而 ${UPLOAD_LOCATION} 是環境變數的寫法(為了不要然你直接修改 docker-compose 造成損壞),所以我們要在同資料夾的 .env 中編輯:

1
2
3
4
5
6
7
8
9
# 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=./library

# The location where your database files are stored. Network shares are not supported for the database
DB_DATA_LOCATION=./postgres

# 其他設定不更改,下略。

以我為例,我就把這兩個資料夾分別改成 /mnt/apacer/immich-data/library/mnt/apacer/immich-data/postgres,是我 SSH 掛載的位置。

接著透過 rsync 來移動資料:

1
2
sudo rsync -av ./library/ /mnt/storage/immich/library/
sudo rsync -av ./postgres/ /mnt/storage/immich/postgres/

至此設定完成,用 docker compose up -d 來讓 immich 上線。

因為 immich 會去讀取 UPLOAD_LOCATION 所在位置的儲存空間,重新上線之後,剩餘磁碟空間就會是我們新家的資訊咯。

以上!