Skip to content

Commit 87b9149

Browse files
committed
fix: resolve relative bind mounts against code dir for git-based compose deploys
Compose deployments using a git provider (GitHub/GitLab/Gitea/Bitbucket/ custom Git) clone the repo verbatim, so composePath can point into a subdirectory (e.g. deploy/docker-compose.yml). Docker Compose resolves relative bind mounts (../files) against the compose file's own directory, not against the code/ dir where file mounts are actually written, so the mount silently binds an empty directory instead of the configured content. Pin --project-directory to code/ when building the docker compose command so relative mounts always resolve the same way raw deployments already do. Fixes Dokploy#5181
1 parent cebd380 commit 87b9149

3 files changed

Lines changed: 70 additions & 5 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { createCommand } from "@dokploy/server/utils/builders/compose";
2+
import { describe, expect, it } from "vitest";
3+
4+
const base = {
5+
composeType: "docker-compose" as const,
6+
appName: "compose-app",
7+
sourceType: "github" as const,
8+
command: "",
9+
};
10+
11+
describe("compose createCommand --project-directory", () => {
12+
it("pins --project-directory to the code dir when composePath is nested", () => {
13+
const cmd = createCommand(
14+
{ ...base, composePath: "./deploy/docker-compose.yml" } as any,
15+
"/etc/dokploy/compose/compose-app/code",
16+
);
17+
18+
expect(cmd).toContain(
19+
"--project-directory /etc/dokploy/compose/compose-app/code",
20+
);
21+
expect(cmd).toContain("-f ./deploy/docker-compose.yml");
22+
});
23+
24+
it("omits --project-directory when no projectPath is passed", () => {
25+
const cmd = createCommand({
26+
...base,
27+
composePath: "./deploy/docker-compose.yml",
28+
} as any);
29+
30+
expect(cmd).not.toContain("--project-directory");
31+
});
32+
33+
it("does not add --project-directory to stack deploy (unsupported flag)", () => {
34+
const cmd = createCommand(
35+
{
36+
...base,
37+
composeType: "stack",
38+
composePath: "./deploy/docker-compose.yml",
39+
} as any,
40+
"/etc/dokploy/compose/compose-app/code",
41+
);
42+
43+
expect(cmd).not.toContain("--project-directory");
44+
expect(cmd.startsWith("stack deploy")).toBe(true);
45+
});
46+
47+
it("keeps raw sourceType resolving from the code dir (root docker-compose.yml)", () => {
48+
const cmd = createCommand(
49+
{ ...base, sourceType: "raw", composePath: "docker-compose.yml" } as any,
50+
"/etc/dokploy/compose/compose-app/code",
51+
);
52+
53+
expect(cmd).toContain(
54+
"--project-directory /etc/dokploy/compose/compose-app/code",
55+
);
56+
expect(cmd).toContain("-f docker-compose.yml");
57+
});
58+
});

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { join } from "node:path";
12
import {
23
addDomainToCompose,
34
clearOldDeployments,
@@ -32,6 +33,7 @@ import {
3233
updateCompose,
3334
updateDeploymentStatus,
3435
} from "@dokploy/server";
36+
import { paths } from "@dokploy/server/constants";
3537
import { db } from "@dokploy/server/db";
3638
import { canEditDeployGitSource } from "@dokploy/server/services/git-provider";
3739
import {
@@ -547,7 +549,9 @@ export const composeRouter = createTRPCRouter({
547549
service: ["create"],
548550
});
549551
const compose = await findComposeById(input.composeId);
550-
const command = createCommand(compose);
552+
const { COMPOSE_PATH } = paths(!!compose.serverId);
553+
const projectPath = join(COMPOSE_PATH, compose.appName, "code");
554+
const command = createCommand(compose, projectPath);
551555
return `docker ${command}`;
552556
}),
553557
refreshToken: protectedProcedure

‎packages/server/src/utils/builders/compose.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export const getBuildComposeCommand = async (rawCompose: ComposeNested) => {
2121
const compose = await withResolvedVaultRefs(rawCompose);
2222
const { COMPOSE_PATH } = paths(!!compose.serverId);
2323
const { sourceType, appName, mounts, composeType, domains } = compose;
24-
const command = createCommand(compose);
24+
const projectPath = join(COMPOSE_PATH, compose.appName, "code");
25+
const command = createCommand(compose, projectPath);
2526
const envCommand = compose.createEnvFile
2627
? getCreateEnvFileCommand(compose)
2728
: "";
28-
const projectPath = join(COMPOSE_PATH, compose.appName, "code");
2929
const exportEnvCommand = getExportEnvCommand(compose);
3030

3131
const newCompose = await writeDomainsToCompose(compose, domains);
@@ -119,7 +119,7 @@ const sanitizeCommand = (command: string) => {
119119
return restCommand.join(" ");
120120
};
121121

122-
export const createCommand = (compose: ComposeNested) => {
122+
export const createCommand = (compose: ComposeNested, projectPath?: string) => {
123123
const { composeType, appName, sourceType } = compose;
124124
if (compose.command) {
125125
return `${sanitizeCommand(compose.command)}`;
@@ -130,7 +130,10 @@ export const createCommand = (compose: ComposeNested) => {
130130
let command = "";
131131

132132
if (composeType === "docker-compose") {
133-
command = `compose -p ${quote([appName])} -f ${quote([path])} up -d --build --remove-orphans`;
133+
const projectDirectoryFlag = projectPath
134+
? `--project-directory ${quote([projectPath])} `
135+
: "";
136+
command = `compose -p ${quote([appName])} ${projectDirectoryFlag}-f ${quote([path])} up -d --build --remove-orphans`;
134137
} else if (composeType === "stack") {
135138
command = `stack deploy -c ${quote([path])} ${quote([appName])} --prune --with-registry-auth`;
136139
}

0 commit comments

Comments
 (0)