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

5
  • Per docker instructions, CMD instruction does not invoke command shell and hence environment variable will not be available for extraction. docs.docker.com/engine/reference/builder/#cmd You probably shall consider to switch to "ENTRYPOINT" instead Commented Mar 27, 2017 at 14:50
  • I also tried with ENTRYPOINT. By the way, started with it: ENTRYPOINT ["powershell", "c:\\RunAll.ps1", "-NewConnString", "${DB_CONN_STRING}"] . Had the same result, then switched to CMD. Even tried like this: SHELL ["powershell"] ENTRYPOINT c:\RunAll.ps1 -NewConnString CMD ["$DB_CONN_STRING"] Commented Mar 27, 2017 at 14:58
  • Below works for me Docker file ` # escape=` FROM microsoft/windowsservercore copy hello.ps1 c:\ ENTRYPOINT powershell.exe c:\hello.ps1 ` hello.ps1 is Write-Output "Environment variable is " + (dir env:) I can build an run below and see environment variable being present docker run --rm me/me -e "test=lala" Commented Mar 27, 2017 at 16:15
  • Your solution works for me partially. If I use it as is - it doesn't work. But if I add ENV TEST="test" in Dockerfile - I'm able to see this environment variable inside container, but I can't reassign it with docker run --rm me/me -e TEST="lala" - it always shows "test". Commented Mar 28, 2017 at 8:26
  • I'm a bit novice in containers and powershell, and I use this approach to send parameters to the starting container. May be there is another way to do it. Commented Mar 28, 2017 at 8:29

1 Answer 1

3

The correct syntax for passing ENV to (in this case) ENTRYPOINT command will be

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

So, you need to use the shell syntax and windows cmd.exe parameters format. Exec syntax of ENTRYPOINT haven't worked for me. And to pass long string with spaces as parameter you will need to use double different quotes, for example

docker.exe run -d --rm -e DB_CONN_STRING="'Data Source=DB2;Initial Catalog=Db;User ID=sa;Password=p@ssw0rd'"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.