简体   繁体   中英

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

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM