mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-08 22:36:37 +08:00
Harden Docker deployment tiers
This commit is contained in:
@@ -153,7 +153,7 @@ environment:
|
|||||||
```bash
|
```bash
|
||||||
docker compose pull
|
docker compose pull
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
docker logs -f mediastation-go
|
docker compose logs -f mediastation-go
|
||||||
```
|
```
|
||||||
|
|
||||||
看到 `sqlite data migrated to postgres`,或确认网页里的用户、媒体库、设置都正常后,再处理旧 SQLite 文件。
|
看到 `sqlite data migrated to postgres`,或确认网页里的用户、媒体库、设置都正常后,再处理旧 SQLite 文件。
|
||||||
@@ -278,7 +278,6 @@ services:
|
|||||||
# Docker Hub 备用:
|
# Docker Hub 备用:
|
||||||
# image: shukbet/mediastationgo:latest
|
# image: shukbet/mediastationgo:latest
|
||||||
|
|
||||||
container_name: mediastation-go
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
init: true
|
init: true
|
||||||
depends_on:
|
depends_on:
|
||||||
@@ -330,7 +329,6 @@ services:
|
|||||||
|
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
container_name: mediastation-postgres
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB: mediastation
|
POSTGRES_DB: mediastation
|
||||||
@@ -339,7 +337,7 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- ./postgres:/var/lib/postgresql/data
|
- ./postgres:/var/lib/postgresql/data
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U mediastation -d mediastation"]
|
test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -U mediastation -d mediastation"]
|
||||||
interval: 10s
|
interval: 10s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 10
|
retries: 10
|
||||||
@@ -390,7 +388,7 @@ docker compose up -d
|
|||||||
### 查看日志
|
### 查看日志
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker logs -f mediastation-go
|
docker compose logs -f mediastation-go
|
||||||
```
|
```
|
||||||
|
|
||||||
### 备份
|
### 备份
|
||||||
@@ -429,7 +427,7 @@ docker compose down
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker ps
|
docker ps
|
||||||
docker logs --tail=100 mediastation-go
|
docker compose logs --tail=100 mediastation-go
|
||||||
```
|
```
|
||||||
|
|
||||||
确认浏览器访问的是:
|
确认浏览器访问的是:
|
||||||
|
|||||||
+4
-6
@@ -153,7 +153,7 @@ Recommended SQLite to PostgreSQL upgrade flow:
|
|||||||
```bash
|
```bash
|
||||||
docker compose pull
|
docker compose pull
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
docker logs -f mediastation-go
|
docker compose logs -f mediastation-go
|
||||||
```
|
```
|
||||||
|
|
||||||
After you see `sqlite data migrated to postgres`, or after the web UI shows your users, libraries, and settings correctly, you can stop using the old SQLite file as a migration source.
|
After you see `sqlite data migrated to postgres`, or after the web UI shows your users, libraries, and settings correctly, you can stop using the old SQLite file as a migration source.
|
||||||
@@ -278,7 +278,6 @@ services:
|
|||||||
# Docker Hub backup:
|
# Docker Hub backup:
|
||||||
# image: shukbet/mediastationgo:latest
|
# image: shukbet/mediastationgo:latest
|
||||||
|
|
||||||
container_name: mediastation-go
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
init: true
|
init: true
|
||||||
depends_on:
|
depends_on:
|
||||||
@@ -331,7 +330,6 @@ services:
|
|||||||
|
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
container_name: mediastation-postgres
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB: mediastation
|
POSTGRES_DB: mediastation
|
||||||
@@ -340,7 +338,7 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- ./postgres:/var/lib/postgresql/data
|
- ./postgres:/var/lib/postgresql/data
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U mediastation -d mediastation"]
|
test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -U mediastation -d mediastation"]
|
||||||
interval: 10s
|
interval: 10s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 10
|
retries: 10
|
||||||
@@ -391,7 +389,7 @@ docker compose up -d
|
|||||||
### Logs
|
### Logs
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker logs -f mediastation-go
|
docker compose logs -f mediastation-go
|
||||||
```
|
```
|
||||||
|
|
||||||
### Backup
|
### Backup
|
||||||
@@ -430,7 +428,7 @@ Check the container:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker ps
|
docker ps
|
||||||
docker logs --tail=100 mediastation-go
|
docker compose logs --tail=100 mediastation-go
|
||||||
```
|
```
|
||||||
|
|
||||||
Then open:
|
Then open:
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
@@ -69,6 +70,9 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("database open failed", zap.Error(err))
|
logger.Fatal("database open failed", zap.Error(err))
|
||||||
}
|
}
|
||||||
|
if err := waitForDatabase(db, logger); err != nil {
|
||||||
|
logger.Fatal("database not ready", zap.Error(err))
|
||||||
|
}
|
||||||
if err := database.AutoMigrate(db); err != nil {
|
if err := database.AutoMigrate(db); err != nil {
|
||||||
logger.Fatal("auto-migrate failed", zap.Error(err))
|
logger.Fatal("auto-migrate failed", zap.Error(err))
|
||||||
}
|
}
|
||||||
@@ -144,6 +148,28 @@ func applyCPUThreadLimit(cfg *config.Config, logger *zap.Logger) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func waitForDatabase(db interface{ DB() (*sql.DB, error) }, logger *zap.Logger) error {
|
||||||
|
sqlDB, err := db.DB()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var lastErr error
|
||||||
|
for attempt := 1; attempt <= 30; attempt++ {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
|
err = sqlDB.PingContext(ctx)
|
||||||
|
cancel()
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
lastErr = err
|
||||||
|
if logger != nil {
|
||||||
|
logger.Warn("database not ready; retrying", zap.Int("attempt", attempt), zap.Error(err))
|
||||||
|
}
|
||||||
|
time.Sleep(time.Duration(attempt) * 500 * time.Millisecond)
|
||||||
|
}
|
||||||
|
return lastErr
|
||||||
|
}
|
||||||
|
|
||||||
func buildRouter(cfg *config.Config, logger *zap.Logger, svc *service.Container) *gin.Engine {
|
func buildRouter(cfg *config.Config, logger *zap.Logger, svc *service.Container) *gin.Engine {
|
||||||
if !cfg.App.Debug {
|
if !cfg.App.Debug {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ services:
|
|||||||
|
|
||||||
opensearch:
|
opensearch:
|
||||||
image: opensearchproject/opensearch:2
|
image: opensearchproject/opensearch:2
|
||||||
container_name: mediastation-opensearch
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
discovery.type: single-node
|
discovery.type: single-node
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ services:
|
|||||||
|
|
||||||
redis:
|
redis:
|
||||||
image: redis:7-alpine
|
image: redis:7-alpine
|
||||||
container_name: mediastation-redis
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
command:
|
command:
|
||||||
- redis-server
|
- redis-server
|
||||||
|
|||||||
+1
-3
@@ -22,7 +22,6 @@ services:
|
|||||||
# 方式二:Docker Hub 备用(GHCR 拉取慢或不可用时使用)
|
# 方式二:Docker Hub 备用(GHCR 拉取慢或不可用时使用)
|
||||||
# image: shukbet/mediastationgo:latest
|
# image: shukbet/mediastationgo:latest
|
||||||
|
|
||||||
container_name: mediastation-go
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
init: true
|
init: true
|
||||||
depends_on:
|
depends_on:
|
||||||
@@ -122,7 +121,6 @@ services:
|
|||||||
|
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
container_name: mediastation-postgres
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB: mediastation
|
POSTGRES_DB: mediastation
|
||||||
@@ -133,7 +131,7 @@ services:
|
|||||||
# PostgreSQL 主数据目录。升级/重建容器时必须保留。
|
# PostgreSQL 主数据目录。升级/重建容器时必须保留。
|
||||||
- ./postgres:/var/lib/postgresql/data
|
- ./postgres:/var/lib/postgresql/data
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U mediastation -d mediastation"]
|
test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -U mediastation -d mediastation"]
|
||||||
interval: 10s
|
interval: 10s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 10
|
retries: 10
|
||||||
|
|||||||
Reference in New Issue
Block a user