docker-compose secrets without swarm mode: how to import their values?

980 Views Asked by At

There are some questions about using secrets with docker-compose without swarm mode, but when trying to follow some of them, I never managed to read the secrets inside running container.

Approach #1

docker-compose.yml:

version: "3.8"

services:
  server:
    image: alpine:latest
    secrets:
      - sec-str
    environment:
      - TE_STR=${sec-str}
    command: tail -F .

secrets:
  sec-str:
    file: ./secret.s

secret.s:

sec-str="A!Bit@complicated-String^%"

Outcome:

/ # echo $TE_STR
str

Approach #2

Only change is made here, in secret.s:

"A!Bit@complicated-String^%"

Outcome:

/ # echo $TE_STR
str

Approach #3

TE_STR=${sec-str} replaced with TE_STR=$sec-str.

Outcome:

/ # echo $TE_STR
-str

Running out of ideas for now. Any clues from you?

1

There are 1 best solutions below

2
On

Secrets are still files inside the container. You can find yours at:

/run/secrets/sec-str

If you need it as en environment variable do as follows:

environment:
   - TE_STR_FILE: /run/secrets/sec-str

This will set TE_STR to the contents of your secret.