Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions apps/dokploy/__test__/compose/volume/volume-services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,40 @@ test("Add suffix to volumes declared directly in services", () => {
);
});

const composeFileReadOnly = `
version: "3.8"

services:
db:
image: postgres:latest
volumes:
- db_data:/var/lib/postgresql/data:ro
- config/nginx:/etc/nginx/conf.d:z
`;

test("Preserve access mode when adding suffix to named volumes", () => {
const composeData = parse(composeFileReadOnly) as ComposeSpecification;

const suffix = generateRandomHash();

if (!composeData.services) {
return;
}

const updatedComposeData = addSuffixToVolumesInServices(
composeData.services,
suffix,
);
const actualComposeData = { ...composeData, services: updatedComposeData };

expect(actualComposeData.services?.db?.volumes).toContain(
`db_data-${suffix}:/var/lib/postgresql/data:ro`,
);
expect(actualComposeData.services?.db?.volumes).toContain(
`config-${suffix}/nginx:/etc/nginx/conf.d:z`,
);
});

const composeFileTypeVolume = `
version: "3.8"

Expand Down
9 changes: 6 additions & 3 deletions packages/server/src/utils/docker/compose/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const addSuffixToVolumesInServices = (
if (_.has(newServiceConfig, "volumes")) {
newServiceConfig.volumes = _.map(newServiceConfig.volumes, (volume) => {
if (_.isString(volume)) {
const [volumeName, path] = volume.split(":");
const [volumeName, path, ...modeParts] = volume.split(":");

// skip bind mounts and variables (e.g. $PWD)
if (
Expand All @@ -38,15 +38,18 @@ export const addSuffixToVolumesInServices = (
return volume;
}

// Preserve the access mode (e.g. :ro, :z, :Z) if present
const mode = modeParts.length > 0 ? `:${modeParts.join(":")}` : "";

// Handle volume paths with subdirectories
const parts = volumeName.split("/");
if (parts.length > 1) {
const baseName = parts[0];
const rest = parts.slice(1).join("/");
return `${baseName}-${suffix}/${rest}:${path}`;
return `${baseName}-${suffix}/${rest}:${path}${mode}`;
}

return `${volumeName}-${suffix}:${path}`;
return `${volumeName}-${suffix}:${path}${mode}`;
}
if (_.isObject(volume) && volume.type === "volume" && volume.source) {
return {
Expand Down