I'm trying to use Secrets with Docker Compose
Here is my Docker Compose (only the relevant parts):
version: "3.8"
services:
My-Service:
build:
context: ./..
dockerfile: ./docker/Dockerfile
image: my-app/api
container_name: my-app-api
environment:
ASPNETCORE_ENVIRONMENT: Docker
ASPNETCORE_URLS: http://+:8080
Swagger__Authentication__ClientSecret_FILE: /run/secrets/my_secret
secrets:
- my_secret
ports:
- "50002:8080"
secrets:
my_secret:
file: ./my_secret.txt
I have a my_secret.txt file containing a secret.
Then I create the image and run it
docker compose -f ./docker/compose.yaml -p my-app up -d --build
Inside the container, checking the /run/secrets/my_secret file is here and has the correct value
sh-5.1$ cat /run/secrets/my_secret
My super secret
The only problem is that my Swagger__Authentication__ClientSecret_FILE environment variable has the value of my file path instead of its content
sh-5.1$ echo $Swagger__Authentication__ClientSecret_FILE
/run/secrets/my_secret
So what am I doing wrong here? I think I'm doing exactly like the Docker documentation says, yet it doesn't work as intended. I honestly can't wrap my head around this.
Thanks for your answer(s).
Let's examine your first example (the one that works). You execute this command :
cat /run/secrets/my_secretand you get resultMy super secret.The
catcommand reads a file at the provided path and print its content. Thus, your command reads the file at/run/secrets/my_secretand print its content which isMy super secret.Now, let's examine the second one. You execute this command
echo $Swagger_Authentication_ClientSecretand get this result/run/secrets/my_secret.$Swagger_Authentication_ClientSecretis the value saved in the variableSwagger_Authentication_ClientSecret. You did set this variable in the environment of your container to be/run/secrets/my_secret. You executeechowith this value as parameter.echosimply prints the value given as parameter. Your command then print the value stored in your variable.What you want is to print the content of the file whose path is the value stored in your variable. To do so, just replace
echowithcatin your second command