Docker secrets by lifetime: build mounts vs Compose runtime secrets

Docker secrets by lifetime: build mounts vs Compose runtime secrets

calendar_today ago • schedule2 min read
— Originally published at chriseugenerodriguez.com

Prerequisites

  • Docker BuildKit enabled (supported by modern Docker versions).
  • Docker Compose (v1.27+ or v2) for Compose secrets support.
  • A build process that may need private dependencies or a runtime service that reads credentials from the filesystem.

Implementation decision

  • If the credential is only required during a RUN step, pass it with docker build --secret and consume it with RUN --mount=type=secret (or use --ssh and RUN --mount=type=ssh for SSH keys). This scope keeps the secret available only to that build instruction and reduces the chance it will be recorded in image layers.

  • If the credential is only required when the container runs, define it as a Compose secret and mount it into the container at /run/secrets/. Give the secret only to the services that need it.

  • If both build and runtime need the same credential, do not try to reuse one mechanism for both. Use a build mount during image construction and a Compose secret at runtime; treat those as separate items in your CI and deployment pipeline.

Expected result

  • The build process can access private resources without baking credentials into the image.
  • The runtime service receives a file-backed secret readable at /run/secrets/ and does not have those secrets stored as image ENV or in image history.

Limitation

  • These Docker features stop Docker from embedding secrets into image layers or image configuration, but they cannot control what your application does with a secret once it’s inside a running container. You still need to prevent logging, unintended copies, or publishing of secret contents.

(Verified code example inserted by the server.)

Working example

export function chooseDockerSecretMechanism({ needsDuringBuild, needsDuringRuntime, credentialType = 'file' } = {}) {
  if (needsDuringBuild && needsDuringRuntime) {
    return {
      recommendation: 'split',
      reason: 'Use a build secret or SSH mount during image construction, and a Compose secret at runtime.',
    };
  }

  if (needsDuringBuild) {
    if (credentialType === 'ssh') {
      return {
        recommendation: 'build-ssh-mount',
        reason: 'SSH credentials used during a build should use --ssh and RUN --mount=type=ssh.',
      };
    }

    return {
      recommendation: 'build-secret',
      reason: 'Temporary build-time credentials should use --secret and RUN --mount=type=secret.',
    };
  }

  if (needsDuringRuntime) {
    return {
      recommendation: 'compose-secret',
      reason: 'A running container should read a file-backed secret mounted by Docker Compose at /run/secrets/<name>.',
    };
  }

  return {
    recommendation: 'plain-config',
    reason: 'If the value is not sensitive and is not needed for build or runtime secret access, use ordinary configuration instead.',
  };
}

export function classifyDockerfileLine(line) {
  const text = String(line).trim();

  if (/\bRUN\b.*--mount=type=secret\b/.test(text)) {
    return 'build-secret-mount';
  }
  if (/\bRUN\b.*--mount=type=ssh\b/.test(text)) {
    return 'build-ssh-mount';
  }
  if (/^\s*(ENV|ARG)\b/i.test(text) && /(TOKEN|PASSWORD|SECRET|KEY)/i.test(text)) {
    return 'persistent-secret-risk';
  }
  return 'other';
}

Read the full article: https://chriseugenerodriguez.com/blog/docker-build-vs-compose-secrets

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

I deleted 44.6 GB and df moved by 5

BashSnippets - Nov 10

How to Reduce Your AWS Bill by 50%

rogo032 - Jan 27

How to Debug Failed n8n Workflows: Step-by-Step Guide

floxolab - Nov 2

Your Kubernetes Requests Are a Guess. groundcover Wants an Agent to Keep Fixing Them.

Tom Smithverified - Nov 3

My 30-day log cleanup kept a file that was 30 and a half days old

BashSnippets - Oct 8
chevron_left
735 Points • 34 Badges
Santa Monica, CA • chriseugenerodriguez.com
27Posts
0Comments
6Connections
10 years fe

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!