Search code examples
spring-bootdockerdocker-volumekubernetes-secretsdocker-secrets

How can I natively load docker secrets mounted as a volume in spring boot


How can my spring boot application running inside a container access docker secrets mounted as a volume inside that same container?

Commonly suggested methods I DO NOT want to use:

  • Echo the secret into an environment variable - this is insecure.
  • Pass the secret in as a command line argument - this is messy with multiple secrets and hurts the local dev experience
  • Manually read the secret from a file by implementing my own property loader - Spring must have a native way to do this

Solution

  • Spring boot 2.4 introduced this feature as part of Volume mounted config trees using the spring.config.import property.
    To read docker secrets mounted as a volume from the default location set:

    spring.config.import = configtree:/run/secrets/
    

    The property name will be derived from the secret filename and the value from the file contents. This can then be accessed like any other spring property.