dokploy icon indicating copy to clipboard operation
dokploy copied to clipboard

`application.readLogs` with `search` returns HTTP 500 when there are no matches, and only searches inside the last `tail` lines

Open gcentegit opened this issue 1 month ago • 1 comments

Describe the bug

application.readLogs (same pattern in the other readLogs routers) builds this command in packages/server/src/services/docker.ts → getContainerLogs:

docker container logs --timestamps --tail ${tail} ${sinceFlag} ${target} 2>&1 | grep -iF '${search}'

This has two problems:

  1. No matches → HTTP 500 instead of an empty result. grep exits with code 1 when it finds nothing (code 1 means "no lines selected", not an error). execAsync rejects on any non-zero exit code, and the catch only recovers when stdout is non-empty, so an empty search result is re-thrown and the API answers 500 Internal Server Error.
  2. search only looks inside the last tail lines. --tail is applied by docker logs before the pipe to grep, so a line that exists in the container log but is older than the last tail lines (default 100) is never found — and, because of (1), the user gets a 500 instead of "nothing found". In practice, searching for anything that happened at container start-up fails as soon as the app has logged ~100 lines since.

To Reproduce

Dokploy v0.30.8, application deployed as a swarm service / container. Example container log (abridged, timestamps as returned by Dokploy):

2026-10-07T04:39:19Z Prisma schema loaded from db/prisma/schema.prisma
2026-10-07T04:39:19Z Applying migration `20261007000000_example`
2026-10-07T04:39:19Z All migrations have been successfully applied.
2026-10-07T04:39:22Z [PrismaService] Prisma client connected
... ~100 more lines (route mapping, requests, cron logs) ...
2026-10-07T05:00:00Z [ImportProcessor] Scheduled import triggered
2026-10-07T05:00:03Z [ImportService] Scheduled import finished

Calls to GET /api/application.readLogs (via the API / MCP), all on the same application:

# Parameters Expected Actual
1 tail: 5, since: "10m" (no search, nothing logged in the period) "" ✅ ""
2 tail: 50, search: "textthatdoesnotexist" "" ❌ 500
3 tail: 5, since: "24h", search: "Prisma" (lines exist, but ~100 lines up) the 4 "Prisma" lines ❌ 500
4 tail: 2000, since: "24h", search: "Prisma" the 4 "Prisma" lines ✅ 4 lines
5 tail: 5, search: "Import" (matches are among the last 5 lines) 2 lines ✅ 2 lines

Cases 2 and 3 are the bug; 4 is the current workaround (a large tail).

Expected behavior

  • A search with no matches returns an empty string (HTTP 200), like case 1.
  • search filters the log first and tail limits the number of matching lines returned, so tail: 100, search: "migration" means "the last 100 lines that contain migration".

Suggested fix

const command = search
  ? `${baseCommand.replace(`--tail ${tail} `, "")} 2>&1 | grep -iF '${escapedSearch}' | tail -n ${tail}`
  : `${baseCommand} 2>&1`;

and treat grep's "no match" as an empty result, e.g. append || true to the grep stage, or in the catch return "" when the error's exit code === 1 and stderr is empty.

(If scanning the whole log is a concern for very large logs, keep a generous upper bound such as --tail 10000 on docker logs and apply the user's tail after grep.)

Environment

  • Dokploy: v0.30.8 (latest at the time of writing)
  • Deployment: single server, swarm service
  • Affected endpoints: application.readLogs and the equivalent readLogs in the other routers (compose, postgres, redis, mysql, mariadb, mongo, libsql) that call getContainerLogs with search.

Question

Could you confirm whether this is something you plan to fix, or let us know the recommended way to search a container's start-up logs through the API in the meantime? Today our workaround is to always pass tail: 2000 together with search.

gcentegit avatar Oct 08 '26 11:10 gcentegit

DOK-844

linear-code[bot] avatar Oct 08 '26 11:10 linear-code[bot]