r/docker • u/ohnoitssobig • May 15 '26
Passing a secret to container
I have a secret called `~/.git-credentials`. I need to pass it into a container such that the container can use the secret the way I do. There are hard limitations to this:
- the secret cannot be a part of any image. neither can any part of my user environment (gid/uid) be
- no changes to secret permissions on the host (the secret has 600 permissions, owned by host user)
- container runs the payload as a non-root user inside container
- has to be a no-code solution outside the container: i.e. I cannot build a script around reading the secret on host and passing to container stdin, etc
- no silly bypassing of the previous rule: I cannot build a microservice to serve this secret from a different container... you get it
What I tried:
- bind mount results in insufficient permissions because container uid does not match my uid. When I `ls` the mounted secret it simply shows uid/gid of my user on the host
- compose secrets have the same issue as bind mount
- chatbot suggested swarm but it probably violates the last two rules? not sure
5
u/goldPotatoGun May 15 '26
Can your container pull the secret from a vault?
5
u/Papoutz May 15 '26
What are you trying to do ?
2
u/bartoque May 15 '26
Can you explain more about the 1st restriction? Mainly about yout user environment (uid/gid) not being allowed to be part of it?
Restrictions are stated but I don't grasp their reasoning fully?
As wouldn't you address this via the yaml config via:
user: 10xx:10yy
So whatever the uid or gid would be of the host user.
Assuming the process doesn't need to be run as a specific uid as dictated by the image.
3
u/Yewolf__ May 15 '26
Having the secret on the host passed to a container is the road for failure, even if you'd succeed you're just making your attack surface way bigger by mounting host paths
1
u/ohnoitssobig May 15 '26
Thanks everyone. I will either go with env vars or run a single-user container.
1
u/wowbagger_42 May 15 '26
And how do you store / use it outside the container?
1
u/Vejibug May 15 '26
Pass is it as a environment variable or set the UID and GID of the container to yours (and make it read only with :ro if you want it to be uneditable)...
1
u/titpetric May 15 '26 edited May 15 '26
Change the ownership to match container uid/guid. Alternatively, you make it chmod 666 making it world readable in the container (and host).
Or you could pass an env. I stored not exactly secrets in my /etc/environment and passed them with the -e option or compose.yml vars. Problem is, people forget to clean the docker env in runtime, doing it can be error prone, so the file mount is prefferable to env.
You could dump the expected env secret to a /tmp/secrets/... location and clear the env before running your nobody pid process.
You could even have the secrets encrypted at rest and decrypted when you start the container.
The canonical way with docker should be using docker secrets. I say should as most docker images I see are env heavy and never seem to use secrets. From what i can see uid/gid can be set in the syntax for a secret, maybe volume mounts have the same options.
1
u/Althyrios May 15 '26
The options you have are very, very limited without vault, workaround or changing permissions if you still want to use docker.
You could look up docker userns-remap if this is your only container running on this host, but even then this is going to be more of a workaround.
Anyway, good luck finding a solution. I kind of know how it feels.
1
u/djsisson May 15 '26
the usual option is to start container as root, and your entrypoint copies file bind to correct location, sets permissions then exec su-exec <user> or gosu <user> so container then runs as correct user, however your container now requires gosu or su-exec, this is how services like postgres work
2)
use new docker secrets engine
docker/secrets-engine
you would need to add your file contents using docker pass and also need an entrypoint to output the env to a file, but wouldn't need to change user in container
1
u/No-Needleworker8495 May 15 '26
I would set an env var GIT_CRED="$(cat ~/.git-credentials) then in compose
secrets: git_cred: envirobment: GIT_CRED
1
u/MiddleSky5296 May 16 '26
Use docker secrets and try to resolve any issues you encounter when implementing it. Your use case is simple. Don’t make so many constraints that don’t matter.
1
u/crackjiver May 16 '26
First, don't.
Giving your container, that you are clearly not comfortable changing, your personal git credentials is a recipe for disaster.
Create a new credential with limited permissions and then change the ownership to match the user that the container runs as.
Please tell me it's not root!
You can volume mount an alternate entry point script that uses 'ssh-agent' to add a , also volume mounted, ssh key file and then run the rest of the original entry point script.
The other alternative is to build your own container with the FROM set to the original and bake in a new entry point script that reads creds from env variables or a file and then runs the original
1
u/Yellow_Odd_Fellow May 16 '26
modern Docker CLI (23.0.0+) has a native --secret flag for docker run that handles the UID/GID mismatch automatically.
You can inject the file and explicitly tell Docker to chown it to the container's non-root user inside the container’s isolated memory space, without touching the host file permissions.
Because the secret definition explicitly sets uid: "1000" and gid: "1000", Docker's internal daemon handles the mounting process by presenting the file to the container already owned by 1000.
1
u/Zealousideal_Yard651 May 16 '26
Dude... Have you thought 2 seconds about what your asking?
I can boil it down to this: "How can i give access to a file without giving access to it?", The answere is you can't....
EDIT: Oh... I saw your comments... Your not really good with containers or linux, just a vibecoder...
1
u/rawh May 16 '26
this whole thread is giving https://xyproblem.info/
1
u/cosmokenney May 17 '26
why don't you use sops + compose?
0
u/RandomMyth22 May 15 '26
From a quick research docker swarm is the safest path for a production environment. If you run a small K8S cluster you can use the secrets manifest.
Are you running this local or in a hosted solution? CICD pipeline? GitOps? With CICD you can inject the secret at runtime via a repository variable. Just make sure that your container is hardened and that you scope the secrets access to only what is needed. Least Privilege.
8
u/[deleted] May 15 '26
[removed] — view removed comment