Skip to content

Commit ec8e645

Browse files
committed
fix: only pin --project-directory when compose has mounts
--project-directory was added unconditionally in 87b9149 to fix relative bind mount resolution for git-based compose deploys with a nested composePath (Dokploy#5181). It also moves where build.context and the generated .env resolve, breaking any compose file in a subdirectory that has context: . alongside its Dockerfile (Dokploy#5230), or interpolates env vars (Dokploy#5242). Only pin --project-directory when the compose actually has mounts configured; otherwise build.context and .env resolve against the compose file's own directory like plain docker compose. Also pass --env-file explicitly pointing at the generated .env next to the compose file, so it's found even when --project-directory is pinned (credit: tonnenpinguin, PR Dokploy#5235). Fixes Dokploy#5230 Fixes Dokploy#5242
1 parent e8f4a17 commit ec8e645

3 files changed

Lines changed: 77 additions & 3 deletions

File tree

‎apps/dokploy/__test__/compose/compose-project-directory.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,69 @@ describe("compose createCommand --project-directory", () => {
5555
);
5656
expect(cmd).toContain("-f docker-compose.yml");
5757
});
58+
59+
it("resolves build.context against the compose file's own directory when there are no mounts", () => {
60+
const cmd = createCommand({
61+
...base,
62+
composePath: "./backend/docker-compose.yml",
63+
} as any);
64+
65+
expect(cmd).not.toContain("--project-directory");
66+
expect(cmd).toContain("-f ./backend/docker-compose.yml");
67+
});
68+
});
69+
70+
describe("compose createCommand --env-file", () => {
71+
it("points --env-file at the generated .env next to a nested compose file", () => {
72+
const cmd = createCommand(
73+
{
74+
...base,
75+
composePath: "./deploy/docker-compose.yml",
76+
createEnvFile: true,
77+
} as any,
78+
"/etc/dokploy/compose/compose-app/code",
79+
);
80+
81+
expect(cmd).toContain("--env-file deploy/.env");
82+
expect(cmd).toContain(
83+
"--project-directory /etc/dokploy/compose/compose-app/code",
84+
);
85+
});
86+
87+
it("omits --env-file when createEnvFile is disabled", () => {
88+
const cmd = createCommand(
89+
{ ...base, composePath: "./deploy/docker-compose.yml" } as any,
90+
"/etc/dokploy/compose/compose-app/code",
91+
);
92+
93+
expect(cmd).not.toContain("--env-file");
94+
});
95+
96+
it("uses the code-root .env for raw sourceType", () => {
97+
const cmd = createCommand(
98+
{
99+
...base,
100+
sourceType: "raw",
101+
composePath: "docker-compose.yml",
102+
createEnvFile: true,
103+
} as any,
104+
"/etc/dokploy/compose/compose-app/code",
105+
);
106+
107+
expect(cmd).toContain("--env-file .env");
108+
});
109+
110+
it("does not add --env-file to stack deploy (unsupported flag)", () => {
111+
const cmd = createCommand(
112+
{
113+
...base,
114+
composeType: "stack",
115+
composePath: "./deploy/docker-compose.yml",
116+
createEnvFile: true,
117+
} as any,
118+
"/etc/dokploy/compose/compose-app/code",
119+
);
120+
121+
expect(cmd).not.toContain("--env-file");
122+
});
58123
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,10 @@ export const composeRouter = createTRPCRouter({
551551
const compose = await findComposeById(input.composeId);
552552
const { COMPOSE_PATH } = paths(!!compose.serverId);
553553
const projectPath = join(COMPOSE_PATH, compose.appName, "code");
554-
const command = createCommand(compose, projectPath);
554+
const command = createCommand(
555+
compose,
556+
compose.mounts.length > 0 ? projectPath : undefined,
557+
);
555558
return `docker ${command}`;
556559
}),
557560
refreshToken: protectedProcedure

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export const getBuildComposeCommand = async (rawCompose: ComposeNested) => {
2222
const { COMPOSE_PATH } = paths(!!compose.serverId);
2323
const { sourceType, appName, mounts, composeType, domains } = compose;
2424
const projectPath = join(COMPOSE_PATH, compose.appName, "code");
25-
const command = createCommand(compose, projectPath);
25+
const command = createCommand(
26+
compose,
27+
mounts.length > 0 ? projectPath : undefined,
28+
);
2629
const envCommand = compose.createEnvFile
2730
? getCreateEnvFileCommand(compose)
2831
: "";
@@ -133,7 +136,10 @@ export const createCommand = (compose: ComposeNested, projectPath?: string) => {
133136
const projectDirectoryFlag = projectPath
134137
? `--project-directory ${quote([projectPath])} `
135138
: "";
136-
command = `compose -p ${quote([appName])} ${projectDirectoryFlag}-f ${quote([path])} up -d --build --remove-orphans`;
139+
const envFileFlag = compose.createEnvFile
140+
? `--env-file ${quote([join(dirname(compose.composePath || "docker-compose.yml"), ".env")])} `
141+
: "";
142+
command = `compose -p ${quote([appName])} ${projectDirectoryFlag}${envFileFlag}-f ${quote([path])} up -d --build --remove-orphans`;
137143
} else if (composeType === "stack") {
138144
command = `stack deploy -c ${quote([path])} ${quote([appName])} --prune --with-registry-auth`;
139145
}

0 commit comments

Comments
 (0)