Skip to content

fix: preserve special characters in Compose-generated .env values - #4962

Open
narcisonunez wants to merge 1 commit into
canaryfrom
fix/compose-env-file-special-chars
Open

fix: preserve special characters in Compose-generated .env values#4962
narcisonunez wants to merge 1 commit into
canaryfrom
fix/compose-env-file-special-chars

Conversation

@narcisonunez

@narcisonunez narcisonunez commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

What is this PR about?

Problem

Values containing $, #, quotes, or backslashes were getting corrupted when Dokploy wrote them to the .env file that Docker Compose reads for variable interpolation. Example: PASSWORD=pa$$word in the UI became pa$word inside the container, because Docker Compose interpolates $ in unquoted .env values ($$ collapses to $).

Root cause

getCreateEnvFileCommand in packages/server/src/utils/builders/compose.ts wrote every resolved KEY=value pair unquoted into the .env file. Docker Compose's own .env parser then applied its normal interpolation rules to that unquoted content.

Fix

Added prepareEnvironmentVariablesForFile (packages/server/src/utils/docker/utils.ts), which wraps each value in double quotes and escapes \, ", and $. Used only in compose.ts, the one .env file Compose actually parses.

builders/utils.ts (createEnvFileCommand, used by the Dockerfile builder's createEnvFile option) intentionally keeps the original unquoted output, that .env lands in the Docker build context for the app's own build tooling (Next.js/Vite/etc. via a generic dotenv parser), which doesn't interpolate $ and would corrupt Compose-style escaping instead.

Testing

Verified against real docker compose/dotenv. Two new regression tests:

  • apps/dokploy/__test__/compose/env-file-literals.test.ts
  • apps/dokploy/__test__/deploy/env-file-literals-dockerfile.test.ts

Checklist

Before submitting this PR, please make sure that:

  • You created a dedicated branch based on the canary branch.
  • You have read the suggestions in the CONTRIBUTING.md file https://github.com/Dokploy/dokploy/blob/canary/CONTRIBUTING.md#pull-request
  • You have tested this PR in your local instance. If you have not tested it yet, please do so before submitting. This helps avoid wasting maintainers' time reviewing code that has not been verified by you.

Issues related (if applicable)

closes #4694

Screenshots (if applicable)

Greptile Summary

The PR changes Compose-generated .env serialization so resolved values containing Docker Compose-sensitive characters survive interpolation unchanged.

  • Adds a dedicated serializer that double-quotes values and escapes backslashes, quotes, and dollar signs.
  • Uses the serializer only for Compose-generated .env files while retaining the existing Dockerfile build-context behavior.
  • Adds regression coverage for Docker Compose and generic dotenv consumers.

Confidence Score: 5/5

The PR appears safe to merge, with the changed Compose serialization covered against the relevant special-character cases.

The serializer resolves values through the existing environment pipeline, applies the escaping required by double-quoted Compose .env values, and leaves the distinct Dockerfile and stack serialization paths unchanged.

Reviews (1): Last reviewed commit: "fix: escape env values written to the Co..." | Re-trigger Greptile

Context used:

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Critical issue with environment variables

1 participant