DOCKER How do I set an password and username on docker run with an windows image?
Asked Answered
H

3

6

What I want

I want to set the username and password when I start the container for example:

docker run --password=mysupersafepw --user=myusername mcr.microsoft.com/windows/servercore

What I've got

At the moment the user and password is hardcoded in my dockerfile:

RUN net USER /ADD ssh Passw0rd  && net localgroup Administrators ssh /ADD

What I've tried

I already heard about environment variables, but this (in the dockerfile) doesn't work for me:

ENV user=ssh
ENV password=Passw0rd
[...]    
RUN net USER /ADD ${user} ${password}  && net localgroup Administrators ${user} /ADD

docker build results in:

Step 10/14 : RUN net USER /ADD ${user} ${password}  && net localgroup Administrators ${user} /ADD
 ---> Running in 2552caf74946
The password does not meet the password policy requirements. Check the minimum password length, password complexity and password history requirements.

More help is available by typing NET HELPMSG 2245.
Hearken answered 24/3, 2020 at 10:27 Comment(3)
Hey Pascal, were you able to achieve this flow???Mercorr
@Mercorr no, i was not. And the project for what i needed it was canceled.Hearken
ohh ok, thanks for the quick response.Mercorr
R
1

The default shell for Windows images is cmd.exe. Therefore the ARG and ENV should be dereferenced the same way as in any windows cmd: %myarg%.

So in your case dereferencing should be done like: RUN net USER /ADD %user% %password% && RUN net localgroup Administrators %user% /ADD

Also, ENV statement should be placed after FROM statement, in order to have the environment variables available inside the container.

One can also change the shell to powershell using: SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; $verbosePreference='Continue';"]

In this case dereferencing would have the syntax: $env:myenv

Rouge answered 27/3, 2021 at 11:8 Comment(0)
F
0

You are in the right track and missing a few parameters. You are correct to define the EVN user in the docker file. In order to set the value of user when running you should specify it as docker run -e user=ssh so that in your case the following would work.

docker run -e password=mysupersafepw -e user=myusername mcr.microsoft.com/windows/servercore

given that you have set them in the dockerfile correctly.

Forecast answered 24/3, 2020 at 11:15 Comment(3)
Thanks for the fast answer. But my Problem is, that the build of the dockerfile fails, not the docker run command :/Hearken
can you please edit your question with the dockerfileForecast
I edited the description. I still want to know how to do it with "docker run" but I need help in the dockerfile to make it possibleHearken
C
0

Hi I had the same issue is solved it by running a powershell script that does it for me

docker run -e password=yourpassword the rest of your options then let the script change the password that you gave in the command line.

Continuance answered 8/6, 2020 at 5:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.