Why does `docker run --user` work in PowerShell but fail within my script?

September 21, 2025 2 answers
Asked by CuriousCoder42
I'm running a maintenance script on Fedora 42 using PowerShell 7.5.3 and Docker 28.4.0. The script mounts a folder into a Docker container, but when I execute the command to run the container, it defaults to *root:root* ownership for the created files. I attempted to run the container with the `--user` flag to use my host's UID/GID, but while the full command works well in the terminal, it fails in the script, throwing an unknown flag error for the `--user` argument. I've tried various ways to pass this argument, including hardcoding values and quoting them, but nothing seems to work. What could be causing this issue with the argument expansion?

Answers

ScriptingSage
It looks like you might have better luck using `-u` instead of `--user`. It works as a shortcut, so if you're stuck, that could be an easy fix to try!

CuriousCoder42: Thanks for that! I tried `-u`, and it worked. Turns out I had a typo earlier, which made my variable resolve to null. Now I'm hitting a new error about not finding user 1000, but I'm getting closer!

Reply to this answer
CmdLineChampion
I think your issue is how PowerShell interprets variables. Sometimes when expanded, it treats it as a single argument. Try using an array format for `--user`. Here's how: ```powershell $userIdRunArgs = $IsLinux ? @('--user', "$(id -u):$(id -g)") : @() & docker run --rm @userIdRunArgs --env ... ``` This way, the arguments will be split correctly when passed to Docker!

CuriousCoder42: You nailed it! That did the trick. Thanks for the help!

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions