Skip to content

Commit 41d355c

Browse files
committed
fix: use dockerode API instead of shell docker ps for changePassword container lookup
Fixes Dokploy#5108. The changePassword mutations for postgres/mysql/mariadb/mongo/redis detected the running container via a shell one-liner (docker ps -q --filter status=running | head -n1) executed through execAsync/execAsyncRemote. When that shell command failed for any reason the CONTAINER_ID ended up empty and the mutation incorrectly reported the container as not running even though it was. Replaced it with getServiceContainer(), the existing dockerode-based lookup already used by mount.ts and schedules/utils.ts for the same purpose. Also fixes the postgres ALTER USER command, which never selected a database and defaulted to a database named after the user, causing a false 'database "<user>" does not exist' failure whenever the db name differs from the username.
1 parent ded23bb commit 41d355c

5 files changed

Lines changed: 50 additions & 50 deletions

File tree

‎apps/dokploy/server/api/routers/mariadb.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
findProjectById,
1212
getAccessibleServerIds,
1313
getContainerLogs,
14-
getServiceContainerCommand,
14+
getServiceContainer,
1515
getWebServerSettings,
1616
IS_CLOUD,
1717
rebuildDatabase,
@@ -410,17 +410,17 @@ export const mariadbRouter = createTRPCRouter({
410410
const maria = await findMariadbById(mariadbId);
411411
const { appName, serverId, databaseUser, databaseRootPassword } = maria;
412412

413-
const containerCmd = getServiceContainerCommand(appName);
413+
const container = await getServiceContainer(appName, serverId);
414+
if (!container) {
415+
throw new TRPCError({
416+
code: "BAD_REQUEST",
417+
message: `No running container found for ${appName}`,
418+
});
419+
}
420+
414421
const targetUser = type === "root" ? "root" : databaseUser;
415422

416-
const command = `
417-
CONTAINER_ID=$(${containerCmd})
418-
if [ -z "$CONTAINER_ID" ]; then
419-
echo "No running container found for ${appName}" >&2
420-
exit 1
421-
fi
422-
docker exec "$CONTAINER_ID" mariadb -u root -p'${databaseRootPassword}' -e "ALTER USER '${targetUser}'@'%' IDENTIFIED BY '${password}'; FLUSH PRIVILEGES;"
423-
`;
423+
const command = `docker exec ${container.Id} mariadb -u root -p'${databaseRootPassword}' -e "ALTER USER '${targetUser}'@'%' IDENTIFIED BY '${password}'; FLUSH PRIVILEGES;"`;
424424

425425
await db.transaction(async (tx) => {
426426
const setData =

‎apps/dokploy/server/api/routers/mongo.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
findProjectById,
1212
getAccessibleServerIds,
1313
getContainerLogs,
14-
getServiceContainerCommand,
14+
getServiceContainer,
1515
getWebServerSettings,
1616
IS_CLOUD,
1717
rebuildDatabase,
@@ -431,15 +431,15 @@ export const mongoRouter = createTRPCRouter({
431431
const mongo = await findMongoById(mongoId);
432432
const { appName, serverId, databaseUser, databasePassword } = mongo;
433433

434-
const containerCmd = getServiceContainerCommand(appName);
435-
const command = `
436-
CONTAINER_ID=$(${containerCmd})
437-
if [ -z "$CONTAINER_ID" ]; then
438-
echo "No running container found for ${appName}" >&2
439-
exit 1
440-
fi
441-
docker exec "$CONTAINER_ID" mongosh -u '${databaseUser}' -p '${databasePassword}' --authenticationDatabase admin --eval "db.getSiblingDB('admin').changeUserPassword('${databaseUser}', '${password}')"
442-
`;
434+
const container = await getServiceContainer(appName, serverId);
435+
if (!container) {
436+
throw new TRPCError({
437+
code: "BAD_REQUEST",
438+
message: `No running container found for ${appName}`,
439+
});
440+
}
441+
442+
const command = `docker exec ${container.Id} mongosh -u '${databaseUser}' -p '${databasePassword}' --authenticationDatabase admin --eval "db.getSiblingDB('admin').changeUserPassword('${databaseUser}', '${password}')"`;
443443

444444
await db.transaction(async (tx) => {
445445
await tx

‎apps/dokploy/server/api/routers/mysql.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
findProjectById,
1212
getAccessibleServerIds,
1313
getContainerLogs,
14-
getServiceContainerCommand,
14+
getServiceContainer,
1515
getWebServerSettings,
1616
IS_CLOUD,
1717
rebuildDatabase,
@@ -428,17 +428,17 @@ export const mysqlRouter = createTRPCRouter({
428428
const my = await findMySqlById(mysqlId);
429429
const { appName, serverId, databaseUser, databaseRootPassword } = my;
430430

431-
const containerCmd = getServiceContainerCommand(appName);
431+
const container = await getServiceContainer(appName, serverId);
432+
if (!container) {
433+
throw new TRPCError({
434+
code: "BAD_REQUEST",
435+
message: `No running container found for ${appName}`,
436+
});
437+
}
438+
432439
const targetUser = type === "root" ? "root" : databaseUser;
433440

434-
const command = `
435-
CONTAINER_ID=$(${containerCmd})
436-
if [ -z "$CONTAINER_ID" ]; then
437-
echo "No running container found for ${appName}" >&2
438-
exit 1
439-
fi
440-
docker exec "$CONTAINER_ID" mysql -u root -p'${databaseRootPassword}' -e "ALTER USER '${targetUser}'@'%' IDENTIFIED BY '${password}'; FLUSH PRIVILEGES;"
441-
`;
441+
const command = `docker exec ${container.Id} mysql -u root -p'${databaseRootPassword}' -e "ALTER USER '${targetUser}'@'%' IDENTIFIED BY '${password}'; FLUSH PRIVILEGES;"`;
442442

443443
await db.transaction(async (tx) => {
444444
const setData =

‎apps/dokploy/server/api/routers/postgres.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
getAccessibleServerIds,
1313
getContainerLogs,
1414
getMountPath,
15-
getServiceContainerCommand,
15+
getServiceContainer,
1616
getWebServerSettings,
1717
IS_CLOUD,
1818
rebuildDatabase,
@@ -437,15 +437,15 @@ export const postgresRouter = createTRPCRouter({
437437
const pg = await findPostgresById(postgresId);
438438
const { appName, serverId, databaseUser } = pg;
439439

440-
const containerCmd = getServiceContainerCommand(appName);
441-
const command = `
442-
CONTAINER_ID=$(${containerCmd})
443-
if [ -z "$CONTAINER_ID" ]; then
444-
echo "No running container found for ${appName}" >&2
445-
exit 1
446-
fi
447-
docker exec "$CONTAINER_ID" psql -U ${databaseUser} -c "ALTER USER \\"${databaseUser}\\" WITH PASSWORD '${password}';"
448-
`;
440+
const container = await getServiceContainer(appName, serverId);
441+
if (!container) {
442+
throw new TRPCError({
443+
code: "BAD_REQUEST",
444+
message: `No running container found for ${appName}`,
445+
});
446+
}
447+
448+
const command = `docker exec ${container.Id} psql -U ${databaseUser} -d postgres -c "ALTER USER \\"${databaseUser}\\" WITH PASSWORD '${password}';"`;
449449

450450
await db.transaction(async (tx) => {
451451
await tx

‎apps/dokploy/server/api/routers/redis.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
findRedisById,
1111
getAccessibleServerIds,
1212
getContainerLogs,
13-
getServiceContainerCommand,
13+
getServiceContainer,
1414
getWebServerSettings,
1515
IS_CLOUD,
1616
rebuildDatabase,
@@ -418,15 +418,15 @@ export const redisRouter = createTRPCRouter({
418418
const rd = await findRedisById(redisId);
419419
const { appName, serverId, databasePassword } = rd;
420420

421-
const containerCmd = getServiceContainerCommand(appName);
422-
const command = `
423-
CONTAINER_ID=$(${containerCmd})
424-
if [ -z "$CONTAINER_ID" ]; then
425-
echo "No running container found for ${appName}" >&2
426-
exit 1
427-
fi
428-
docker exec "$CONTAINER_ID" redis-cli -a '${databasePassword}' CONFIG SET requirepass '${password}'
429-
`;
421+
const container = await getServiceContainer(appName, serverId);
422+
if (!container) {
423+
throw new TRPCError({
424+
code: "BAD_REQUEST",
425+
message: `No running container found for ${appName}`,
426+
});
427+
}
428+
429+
const command = `docker exec ${container.Id} redis-cli -a '${databasePassword}' CONFIG SET requirepass '${password}'`;
430430

431431
await db.transaction(async (tx) => {
432432
await tx

0 commit comments

Comments
 (0)