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

0
2
Asked By CuriousCoder42 On

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?

2 Answers

Answered By CmdLineChampion On

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!

Answered By ScriptingSage On

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!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.