none
Is there a way to determine if a powershell script is running in a container? RRS feed

  • Question

  • I have some deployment scripts that need to branch logic if they are executed from inside a windows core container.  Is there a known approach to determining from within a powershell script if it is running inside a container?  I am thinking  test-path against some  object?  There are no environment variables that seem to be useful for this.

    Tuesday, February 23, 2016 4:59 PM

All replies

  • So, probably something quick and dirty, but check for the existence of docker.exe

    test-path c:\windows\system32\docker.exe

    It won't be inside a container, but on the host.

    Friday, February 26, 2016 9:30 PM
  • I’m not sure if there is any firm established way to test for this, but can think of some other potential options. If you are using PowerShell to manage the containers, and know of the container you are targeting, something like this would return a list of PowerShell process running in the container:

     ‘Invoke-Command –ContainerName testcontainer –ScriptBlock {get-process | where {$_.ProcessName -eq 'powershell'}

    The output could then be compared to the host processes to diff those belonging to the container and the host.

    Happy to help track something down if needed.

    neilp

    Saturday, February 27, 2016 12:54 AM