UID/GID for Docker

This commit is contained in:
rmoren97 2026-02-07 15:42:23 -08:00
parent 012388c288
commit e3e02acbc5
2 changed files with 14 additions and 5 deletions

View File

@ -27,6 +27,8 @@ SMTP_FROM=MC-Manager <noreply@example.com>
# --- Docker ---
MC_SERVERS_PATH=/opt/mc-servers
DOCKER_SOCKET=/var/run/docker.sock
DOCKER_UID=1000
DOCKER_GID=1000
# --- App ---
NEXT_PUBLIC_APP_URL=http://localhost:3000

View File

@ -82,6 +82,9 @@ export async function createServerContainer(options: CreateContainerOptions): Pr
`MAX_PLAYERS=${options.maxPlayers}`,
]
const uid = process.env.DOCKER_UID || '1000'
const gid = process.env.DOCKER_GID || '1000'
// Only set itzg-specific env vars for the default image
if (isItzg) {
env.push(
@ -90,6 +93,8 @@ export async function createServerContainer(options: CreateContainerOptions): Pr
`MEMORY=${options.memoryMax}M`,
`JVM_XX_OPTS=${options.jvmArgs.join(' ')}`,
'CREATE_CONSOLE_IN_PIPE=true',
`UID=${uid}`,
`GID=${gid}`,
)
}
@ -125,19 +130,21 @@ export async function createServerContainer(options: CreateContainerOptions): Pr
}
/**
* Ensures the server data directory exists with correct ownership (uid=1000)
* for the itzg/minecraft-server container.
* Ensures the server data directory exists with correct ownership
* for the container user (configured via DOCKER_UID / DOCKER_GID).
*/
export async function ensureServerDirectory(serverPath: string): Promise<void> {
if (!existsSync(serverPath)) {
await mkdir(serverPath, { recursive: true })
}
// itzg/minecraft-server runs as uid=1000 gid=1000
const uid = parseInt(process.env.DOCKER_UID || '1000', 10)
const gid = parseInt(process.env.DOCKER_GID || '1000', 10)
try {
await chown(serverPath, 1000, 1000)
await chown(serverPath, uid, gid)
} catch (err) {
console.warn(`[Docker] Could not chown ${serverPath} to 1000:1000 — container may have permission issues:`, (err as Error).message)
console.warn(`[Docker] Could not chown ${serverPath} to ${uid}:${gid} — container may have permission issues:`, (err as Error).message)
}
}