docker secrets - docker compose secrets

Docker Secrets in CI/CD: Stop Leaking Sensitive Data

How Docker Secrets Work, and Where Developers Misuse Them

Docker secrets were introduced to securely manage sensitive data like API keys, database passwords, and tokens. They work by mounting secrets as files in /run/secrets inside a container, only accessible to the containerized service. This mechanism keeps secrets out of environment variables and logs, at least in theory. The problem starts when developers bypass this mechanism. Instead of using the Docker Compose secrets feature properly, secrets are often hardcoded into Dockerfile, committed to Git, or injected through environment variables. These shortcuts make secrets vulnerable to accidental exposure through logs, container layers, or version history.

# Risky
ENV DB_PASSWORD=mysecretpassword

⚠️ Insecure example, do not use in production

Once this line is added, it’s layered into the image. Anyone with access to the image (build cache, registry, or CI/CD logs) can extract it. Even worse, this pattern often goes unnoticed during reviews because it “just works.”

Docker Compose Secrets and the Hidden Risks in Shared Configs

Docker Compose simplifies multi-container apps, but Docker Compose secrets come with hidden risks. Developers often share Docker Compose. yml or .env files across teams and environments, via Git, Slack, or shared drives.

services:
app:
image: myapp
secrets:
- db_password

secrets:
db_password:
file: ./secrets/db_password.txt

⚠️ Demonstrative example, do not include real secrets in versioned files

The problem? These Docker Compose secrets references assume local files, but in practice, these files get versioned or distributed insecurely. Secrets sneak into Git repos, show up in pull requests, or get copied into other folders. The assumption that “we all know not to commit secrets” isn’t a security policy.

Worse, environments like staging and production may reuse the same docker-compose.yml, creating a false sense of separation. One misconfigured .env file, and production secrets are injected into a test environment.

CI/CD Pipelines: Where Docker Secret Handling Breaks

In CI/CD, Docker secrets fall apart if not isolated. Secrets often leak in three places: logs, image layers, and shared runners.

Logs: CI steps print echo $SECRET_KEY to debug issues. But these logs are stored, sometimes publicly. Tools like GitHub Actions or GitLab store logs for days or weeks.

Image Layers: If a Dockerfile adds a secret during build:

RUN echo "$SECRET_KEY" > /app/config.txt

⚠️ Insecure example, this exposes secrets in image layers

That Docker secret is now part of an image layer. Even if you delete the file later, the previous layer stays in the image history.

Shared Runners: Many CI/CD platforms use shared runners. If secrets aren’t properly scoped or cleaned up, other builds can access them. Worse, secrets are sometimes passed as environment variables to all steps by default.

Securing Docker in Code, Pipelines, and Registries

To lock down Docker secrets, dev teams need layered defenses:

  • Use secret managers like AWS Secrets Manager, HashiCorp Vault, or Doppler. These tools rotate and inject secrets securely into your containers at runtime.
  • Limit scopes: Never expose production secrets in dev/test environments. Use role-based access control (RBAC) to restrict who can inject or read secrets.
  • Avoid ENV vars for secrets. Use Docker secret volumes or mount secrets as files explicitly.
  • Clean up builds: Make sure secrets aren’t baked into images or left in intermediate files.
  • Scan containers and repos: Use tools that inspect images, git history, and CI/CD configs for leaked secrets.

Best Practices to Prevent Docker Secret Exposure

  • Ephemeral secrets: Generate one-time secrets during CI runs. These expire and can’t be reused if stolen.
  • Sealed secrets: Use Kubernetes Sealed Secrets or SOPS to encrypt secrets in Git. This ensures secrets are versioned securely.
  • CI/CD policies: Enforce policies like no secrets in Dockerfile, block builds on secret detection, and log usage of Docker secrets per pipeline.
  • Pre-commit hooks: Block commits with secrets using tools like git-secrets or detect-secrets.
  • Immutable infrastructure: Don’t modify secrets in containers. Rebuild and redeploy with new Docker secrets instead.

Conclusion

When misused, Docker secrets become a liability instead of a security feature. From Docker Compose secrets leaking through YAML files to CI/CD pipelines exposing secrets in logs or layers, the risks are real and preventable.

Using tools like Xygeni, teams can detect exposed secrets early, enforce secret handling policies, and scan for security drifts across code, containers, and pipelines. Treat your Docker secret strategy as critical infrastructure. Secure it like you mean it.

sca-tools-software-composition-analysis-tools
Prioritize, remediate, and secure your software risks
7-day free trial
No credit card required

Secure your Software Development and Delivery

with Xygeni Product Suite