6

I have the following instructions in Dockerfile

ENV DB_CONN_STRING="Data Source=DbServer;Initial Catalog=Db;User ID=sa;Password=p@ssw0rd"
ENTRYPOINT []
CMD ["powershell", "c:\\RunAll.ps1 -NewConnString", "$DB_CONN_STRING"]

When I run this command

docker run --rm -d -e DB_CONN_STRING="Test" test

DB_CONN_STRING is always empty inside RunAll.ps1. How can I pass ENV to CMD?

When I use CMD without parameters

CMD ["powershell", "c:\\RunAll.ps1"]

all works correctly.

RunAll.ps1 code:

param(
    [string]$NewConnString = "Data Source=DbServer;Initial Catalog=db;User ID=sa;Password=p@ssw0rd"
)

New-Item "C:\start_log.txt" -type file -force -value $NewConnString
.\ChangeConnString.ps1 -NewConnString $NewConnString
New-Item "C:\end_log.txt" -type file -force -value $NewConnString

# Run IIS container entry point
.\ServiceMonitor.exe w3svc

I tried several approaches, exec and shell command styles, $DB_CONN_STRING, ${DB_CONN_STRING} and $(DB_CONN_STRING) styles.

Tried suggestions from these posts:

Nothing works for me.

Here is an example from Docker log:

[16:06:34.505][WindowsDockerDaemon][Info   ] time="2017-03-27T16:06:34.505376100+02:00" level=debug msg="HCSShim::Container::CreateProcess id=0909937ce1130047b124acd7a5eb57664e05c6af7cbb446aa7c8015a44393232 config={\"ApplicationName\":\"\",\"CommandLine\":\"powershell c:\\\\RunAll.ps1 -NewConnString $DB_CONN_STRING\",\"User\":\"\",\"WorkingDirectory\":\"C:\\\\\",\"Environment\":{\"DB_CONN_STRING\":\"Test\"},\"EmulateConsole\":false,\"CreateStdInPipe\":true,\"CreateStdOutPipe\":true,\"CreateStdErrPipe\":true,\"ConsoleSize\":[0,0]}

Docker version 17.03.0-ce, build 60ccb22

4

1 回答 1

3

将 ENV 传递给(在这种情况下)ENTRYPOINT 命令的正确语法将是

ENTRYPOINT powershell c:\RunAll.ps1 -NewConnString %DB_CONN_STRING%

因此,您需要使用shell语法和 windows cmd.exe 参数格式。ENTRYPOINT 的Exec语法对我不起作用。并且要将带有空格的长字符串作为参数传递,您将需要使用双引号,例如

docker.exe run -d --rm -e DB_CONN_STRING="'Data Source=DB2;Initial Catalog=Db;User ID=sa;Password=p@ssw0rd'"
于 2017-03-27T16:26:17.053 回答