MinIO設定をCoolify対応に整備し、bucket自動作成を追加

compose:
- MinIOにヘルスチェック(mc ready)を追加し、api/workerはhealthyを待つように変更
- 全認証情報を${VAR:-default}でパラメータ化し、Coolifyの環境変数/シークレットで上書き可能に
- MINIO_SERVER_URL / MINIO_BROWSER_REDIRECT_URL でリバースプロキシ背後の公開URLに対応
- workerの依存先をapiからpostgres+minioに修正し、起動順序のデッドロックを解消

API:
- S3StorageにensureBucket()を追加(HeadBucket→なければCreateBucket、冪等)
- runtime起動時にbucketを自動作成し、minio-init依存を必須ではなくした

.env.exampleにMinIO関連変数を追記
This commit is contained in:
amania-jailbreak
2026-08-05 10:31:03 +09:00
parent 93825ebc64
commit 62c32ad0ad
4 changed files with 105 additions and 35 deletions
+9 -3
View File
@@ -2,18 +2,24 @@ NODE_ENV=development
PORT=4000 PORT=4000
PUBLIC_BASE_URL=http://localhost:4000 PUBLIC_BASE_URL=http://localhost:4000
WEB_ORIGIN=http://localhost:3000 WEB_ORIGIN=http://localhost:3000
NEXT_PUBLIC_API_BASE_URL=http://localhost:4000
NEXT_PUBLIC_AUTH_MODE=demo NEXT_PUBLIC_AUTH_MODE=demo
# Use PostgreSQL + MinIO in Docker Compose. Leave DATABASE_URL empty for memory mode. # --- Storage (MinIO via docker compose) -------------------------------------
# Leave DATABASE_URL empty to run with an in-memory store instead of Postgres.
DATABASE_URL=postgres://altdock:altdock@localhost:5432/altdock DATABASE_URL=postgres://altdock:altdock@localhost:5432/altdock
STORAGE_MODE=s3 STORAGE_MODE=s3
S3_ENDPOINT=http://localhost:9000 S3_ENDPOINT=http://localhost:9000
S3_REGION=us-east-1 S3_REGION=us-east-1
S3_BUCKET=altdock S3_BUCKET=altdock
S3_ACCESS_KEY_ID=minioadmin S3_ACCESS_KEY_ID=minioadmin
S3_SECRET_ACCESS_KEY=minioadmin S3_SECRET_ACCESS_KEY=minioadmin123
S3_FORCE_PATH_STYLE=true S3_FORCE_PATH_STYLE=true
# MinIO root credentials, consumed by docker-compose.yml. CHANGE in production.
MINIO_ROOT_USER=minioadmin
MINIO_ROOT_PASSWORD=minioadmin123
# Only needed when MinIO is behind a reverse proxy / domain (e.g. on Coolify):
MINIO_SERVER_URL=
MINIO_BROWSER_REDIRECT_URL=
# demo works without an identity provider; production should use generic OIDC. # demo works without an identity provider; production should use generic OIDC.
AUTH_MODE=demo AUTH_MODE=demo
+8 -1
View File
@@ -5,5 +5,12 @@ import { createStorage } from "./storage";
export async function createRuntime(appConfig: AppConfig) { export async function createRuntime(appConfig: AppConfig) {
const { store, pool } = await createStore(appConfig); const { store, pool } = await createStore(appConfig);
await store.init(); await store.init();
return { store, storage: createStorage(appConfig), pool }; const storage = createStorage(appConfig);
// When using S3-compatible storage (e.g. MinIO), make sure the target bucket
// exists before serving traffic. This keeps the service self-contained and
// removes the need for a separate init container.
if (storage.ensureBucket) {
await storage.ensureBucket();
}
return { store, storage, pool };
} }
+23
View File
@@ -5,9 +5,11 @@ import { dirname, normalize, relative, resolve } from "node:path";
import { pipeline } from "node:stream/promises"; import { pipeline } from "node:stream/promises";
import { Readable } from "node:stream"; import { Readable } from "node:stream";
import { import {
CreateBucketCommand,
CompleteMultipartUploadCommand, CompleteMultipartUploadCommand,
CreateMultipartUploadCommand, CreateMultipartUploadCommand,
GetObjectCommand, GetObjectCommand,
HeadBucketCommand,
HeadObjectCommand, HeadObjectCommand,
PutObjectCommand, PutObjectCommand,
S3Client, S3Client,
@@ -35,6 +37,7 @@ export interface StorageAdapter {
downloadToFile(key: string, filePath: string): Promise<void>; downloadToFile(key: string, filePath: string): Promise<void>;
getObject(key: string): Promise<StoredObject | null>; getObject(key: string): Promise<StoredObject | null>;
headObject(key: string): Promise<{ sizeBytes: number; contentType: string } | null>; headObject(key: string): Promise<{ sizeBytes: number; contentType: string } | null>;
ensureBucket?(): Promise<void>;
} }
function contentTypeForPath(path: string) { function contentTypeForPath(path: string) {
@@ -126,6 +129,26 @@ export class S3Storage implements StorageAdapter {
}); });
} }
// Idempotently create the configured bucket so the service is self-contained
// and does not depend on an external init step (e.g. a separate minio-init
// container) before it can store objects. Safe to call on every startup.
async ensureBucket() {
const bucket = this.appConfig.S3_BUCKET;
try {
await this.client.send(new HeadBucketCommand({ Bucket: bucket }));
return;
} catch (error: any) {
const notFound = error?.$metadata?.httpStatusCode === 404 || error?.name === "NotFound" || error?.name === "NoSuchBucket";
if (!notFound) {
// Re-throw unexpected errors (auth, network) so they surface loudly.
throw error;
}
}
// us-east-1 / "auto" regions must NOT send a LocationConstraint.
await this.client.send(new CreateBucketCommand({ Bucket: bucket }));
}
async createUploadPlan(key: string, contentType: string, sizeBytes: number) { async createUploadPlan(key: string, contentType: string, sizeBytes: number) {
const partSizeBytes = 16 * 1024 * 1024; const partSizeBytes = 16 * 1024 * 1024;
const partCount = Math.ceil(sizeBytes / partSizeBytes); const partCount = Math.ceil(sizeBytes / partSizeBytes);
+65 -31
View File
@@ -1,16 +1,30 @@
# AltDock full stack. Works locally with `docker compose up` and on Coolify,
# where you override the `${VAR:-default}` values from the project's
# Environment Variables / secrets panel.
#
# Coolify notes:
# - All credentials are parameterized; set them in Coolify's env to avoid
# the insecure local defaults.
# - MinIO has a real healthcheck, so api/worker wait until it can serve.
# - The API also creates the S3 bucket itself on startup (ensureBucket),
# so the stack no longer depends on minio-init completing successfully.
# - If you expose MinIO through a domain, set MINIO_SERVER_URL (public S3 API
# origin) and MINIO_BROWSER_REDIRECT_URL (public console origin) so
# presigned upload URLs and console redirects work from the browser.
services: services:
postgres: postgres:
image: postgres:16-alpine image: postgres:16-alpine
environment: environment:
POSTGRES_DB: altdock POSTGRES_DB: ${POSTGRES_DB:-altdock}
POSTGRES_USER: altdock POSTGRES_USER: ${POSTGRES_USER:-altdock}
POSTGRES_PASSWORD: altdock POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-altdock}
ports: ports:
- "5432:5432" - "5432:5432"
volumes: volumes:
- postgres-data:/var/lib/postgresql/data - postgres-data:/var/lib/postgresql/data
healthcheck: healthcheck:
test: ["CMD-SHELL", "pg_isready -U altdock -d altdock"] test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-altdock} -d ${POSTGRES_DB:-altdock}"]
interval: 5s interval: 5s
timeout: 5s timeout: 5s
retries: 10 retries: 10
@@ -19,22 +33,37 @@ services:
image: minio/minio:latest image: minio/minio:latest
command: server /data --console-address ":9001" command: server /data --console-address ":9001"
environment: environment:
MINIO_ROOT_USER: minioadmin MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minioadmin}
MINIO_ROOT_PASSWORD: minioadmin MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minioadmin123}
# Public origins, only needed when MinIO is behind a reverse proxy/domain.
MINIO_SERVER_URL: ${MINIO_SERVER_URL:-}
MINIO_BROWSER_REDIRECT_URL: ${MINIO_BROWSER_REDIRECT_URL:-}
ports: ports:
- "9000:9000" - "9000:9000"
- "9001:9001" - "9001:9001"
volumes: volumes:
- minio-data:/data - minio-data:/data
healthcheck:
test: ["CMD", "mc", "ready", "local"]
interval: 5s
timeout: 5s
retries: 12
start_period: 10s
# Optional belt-and-suspenders: pre-creates the bucket. The API does the
# same on startup, so this can be removed without affecting functionality.
minio-init: minio-init:
image: minio/mc:latest image: minio/mc:latest
depends_on: depends_on:
- minio minio:
condition: service_healthy
entrypoint: ["/bin/sh", "-c"] entrypoint: ["/bin/sh", "-c"]
command: >- command: >-
"until mc alias set local http://minio:9000 minioadmin minioadmin; do sleep 1; done; "until mc alias set local http://minio:9000 ${MINIO_ROOT_USER:-minioadmin} ${MINIO_ROOT_PASSWORD:-minioadmin123}; do sleep 1; done;
mc mb --ignore-existing local/altdock" mc mb --ignore-existing local/${S3_BUCKET:-altdock}"
environment:
MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minioadmin}
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minioadmin123}
api: api:
image: node:22-bookworm image: node:22-bookworm
@@ -43,17 +72,19 @@ services:
environment: environment:
NODE_ENV: development NODE_ENV: development
PORT: 4000 PORT: 4000
PUBLIC_BASE_URL: http://localhost:4000 PUBLIC_BASE_URL: ${PUBLIC_BASE_URL:-http://localhost:4000}
WEB_ORIGIN: http://localhost:3000 WEB_ORIGIN: ${WEB_ORIGIN:-http://localhost:3000}
DATABASE_URL: postgres://altdock:altdock@postgres:5432/altdock DATABASE_URL: postgres://${POSTGRES_USER:-altdock}:${POSTGRES_PASSWORD:-altdock}@postgres:5432/${POSTGRES_DB:-altdock}
STORAGE_MODE: s3 STORAGE_MODE: s3
S3_ENDPOINT: http://minio:9000 # S3_ENDPOINT is the internal address; presigned URLs resolve via the
S3_REGION: us-east-1 # public MINIO_SERVER_URL when set.
S3_BUCKET: altdock S3_ENDPOINT: ${S3_ENDPOINT:-http://minio:9000}
S3_ACCESS_KEY_ID: minioadmin S3_REGION: ${S3_REGION:-us-east-1}
S3_SECRET_ACCESS_KEY: minioadmin S3_BUCKET: ${S3_BUCKET:-altdock}
S3_ACCESS_KEY_ID: ${MINIO_ROOT_USER:-minioadmin}
S3_SECRET_ACCESS_KEY: ${MINIO_ROOT_PASSWORD:-minioadmin123}
S3_FORCE_PATH_STYLE: "true" S3_FORCE_PATH_STYLE: "true"
AUTH_MODE: demo AUTH_MODE: ${AUTH_MODE:-demo}
PROCESS_INLINE: "false" PROCESS_INLINE: "false"
ports: ports:
- "4000:4000" - "4000:4000"
@@ -62,8 +93,8 @@ services:
depends_on: depends_on:
postgres: postgres:
condition: service_healthy condition: service_healthy
minio-init: minio:
condition: service_completed_successfully condition: service_healthy
worker: worker:
image: node:22-bookworm image: node:22-bookworm
@@ -71,34 +102,37 @@ services:
command: sh -c "npm install --no-audit --no-fund && npm run dev:worker" command: sh -c "npm install --no-audit --no-fund && npm run dev:worker"
environment: environment:
NODE_ENV: development NODE_ENV: development
DATABASE_URL: postgres://altdock:altdock@postgres:5432/altdock DATABASE_URL: postgres://${POSTGRES_USER:-altdock}:${POSTGRES_PASSWORD:-altdock}@postgres:5432/${POSTGRES_DB:-altdock}
STORAGE_MODE: s3 STORAGE_MODE: s3
S3_ENDPOINT: http://minio:9000 S3_ENDPOINT: ${S3_ENDPOINT:-http://minio:9000}
S3_REGION: us-east-1 S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: altdock S3_BUCKET: ${S3_BUCKET:-altdock}
S3_ACCESS_KEY_ID: minioadmin S3_ACCESS_KEY_ID: ${MINIO_ROOT_USER:-minioadmin}
S3_SECRET_ACCESS_KEY: minioadmin S3_SECRET_ACCESS_KEY: ${MINIO_ROOT_PASSWORD:-minioadmin123}
S3_FORCE_PATH_STYLE: "true" S3_FORCE_PATH_STYLE: "true"
AUTH_MODE: demo AUTH_MODE: ${AUTH_MODE:-demo}
PROCESS_INLINE: "false" PROCESS_INLINE: "false"
volumes: volumes:
- .:/workspace - .:/workspace
depends_on: depends_on:
api: postgres:
condition: service_started condition: service_healthy
minio:
condition: service_healthy
web: web:
image: node:22-bookworm image: node:22-bookworm
working_dir: /workspace working_dir: /workspace
command: sh -c "npm install --no-audit --no-fund && npm run dev -- --host 0.0.0.0" command: sh -c "npm install --no-audit --no-fund && npm run dev -- --host 0.0.0.0"
environment: environment:
NEXT_PUBLIC_API_BASE_URL: http://localhost:4000 NEXT_PUBLIC_API_BASE_URL: ${NEXT_PUBLIC_API_BASE_URL:-http://localhost:4000}
ports: ports:
- "3000:3000" - "3000:3000"
volumes: volumes:
- .:/workspace - .:/workspace
depends_on: depends_on:
- api api:
condition: service_started
volumes: volumes:
postgres-data: postgres-data: