.env to docker-compose

Convert a .env file into a docker-compose environment section.

.env Input
docker-compose environment Output
docker-compose environment output appears here

Related Tools

Documentation

What is .env to docker-compose?

This tool reads a .env file and inlines every variable directly into a docker-compose service's environment: block, under a service name you choose. It's for when the values need to live in the compose file itself — a self-contained handoff, a generated template, or a CI step where the original .env isn't available — rather than referencing an external file with env_file:.

How it works

The parser (parseEnv) walks each line, skips blank lines and lines starting with #, strips a leading export if present, and splits on the first =. Only keys matching a valid shell identifier (letters, digits, underscore, not starting with a digit) are kept. Double-quoted values have their \n and \" escapes unescaped; single-quoted values are taken literally; unquoted values have a trailing # comment stripped. Each resulting KEY=value pair is then written as a list item under services.{name}.environment, exactly as compose expects that block to look.

Features

  • Custom service name field (defaults to app)
  • Handles export KEY=value, double- and single-quoted values, and inline # comments the same way dotenv does
  • Skips blank lines and full-line comments automatically
  • Copy, download as .yml, or load sample data

Example

Input (service name: app):

DATABASE_URL=postgres://user:pass@localhost:5432/mydb
API_KEY="sk_live_abc123"
DEBUG=false
PORT=3000

Output:

services:
  app:
    environment:
      - DATABASE_URL=postgres://user:pass@localhost:5432/mydb
      - API_KEY=sk_live_abc123
      - DEBUG=false
      - PORT=3000

Common errors

"No KEY=value lines found" means every line was blank, a comment, or failed the identifier check (e.g. a key starting with a digit, or a line with no =). Values are emitted as plain, unquoted text after the = — if a value itself contains a YAML-significant character like : or #, quote it by hand in the output before committing, since the tool doesn't second-guess your values with YAML-safety quoting the way the Kubernetes Secret and GitHub Actions converters do.

Best practices

Prefer env_file: .env for local development — it keeps secrets out of the compose file and out of version control. Reach for this tool specifically when you need one portable, self-contained YAML file, and remember that inlined secrets in a compose file are exactly as sensitive as the original .env — don't commit the generated output to a public repo.

Frequently Asked Questions

Why not just use env_file: .env directly in docker-compose?

That's simpler when you're fine keeping a separate .env file alongside the compose file. This tool is for when you need the values inlined directly into the compose YAML itself — sharing a single self-contained file, generating a template, or a CI step that doesn't have the original .env available.

Are values quoted or escaped?

Values are inserted as plain KEY=value entries under environment:, matching how .env files themselves are unquoted by default. If a value contains YAML-significant characters, quote it manually in the output before committing.

What about comments and blank lines in the .env file?

Both are skipped automatically — only real KEY=value assignments (with export optionally stripped) are converted.

Does this handle quoted values in the .env file?

Yes — both "double-quoted" and 'single-quoted' values are unwrapped, with \n and \" unescaped inside double-quoted values, matching standard dotenv parsing behavior.