Skip to main content
Kofax

Environment Variables From Docker Containers

3025400

Question / Problem: 

How can environment variables be collected from Docker containers for troubleshooting configuration issues?

Answer / Solution: 

The Configuration Utility can generate an env file to be provided to docker containers when they are started to set configuration settings in environment variables.  The environment variables are used by Startup.ps1, the script run at the start of the container, to set the values of the config files used by KTA.  For example, Startup.ps1 calls UpdateConfigAppSettings.ps1 to set AppSettings from environment variables, and UpdateConfigBindings.ps1 makes binding changes from the values in environment variables.

To troubleshoot configuration within a container, it can be helpful to output the environment variables to a file to confirm they match the configuration that is expected.  The environment variables that are actually set by Docker, and used by KTA, are at the Process scope.  However it can be useful to also see variables set at the User and Machine scopes to check if expected variables are being set in the incorrect scope.

The attached PowerShell script will export the environment variables in the Process, User, and Machine scopes, and copy them to the same folder as the script, named with the name of the container.  The script will either prompt for the name of the container, or if the script has been renamed to end in _NameOfContainer, it will use NameOfContainer.

ExportContainerEnvVariables_Prompt.ps1

# get script name and folder
$scriptpath = $MyInvocation.MyCommand.Path
$scriptdir = Split-Path $scriptpath
$scriptname=[io.path]::GetFileNameWithoutExtension($scriptpath)


# consider last text after _ as a container name if present, otherwise prompt for it
$containername=$scriptname.Split("_") | Select-Object -Last 1
if (!($scriptname.Contains("_")) -or [string]::IsNullOrWhiteSpace($containername) -or $containername.ToLower() -eq "prompt")
{ 
    "Note: Change script name to end in _NameOfContainer to skip prompt.`n"
    $containername = Read-Host 'Name of running container (from docker ps -a) from which to export environment variables?'
} else {
    "Based on filename of script, acting on container named $containername"
}


# dump environment variables to text file
docker exec $containername powershell -command "[Environment]::GetEnvironmentVariables('Process').GetEnumerator() | Sort-Object Name | format-table Name, Value -AutoSize | Out-String -Width 1024" > ${scriptdir}\${containername}-ProcessVariables.txt
docker exec $containername powershell -command "[Environment]::GetEnvironmentVariables('User').GetEnumerator() | Sort-Object Name | format-table Name, Value -AutoSize | Out-String -Width 1024" > ${scriptdir}\${containername}-UserVariables.txt
docker exec $containername powershell -command "[Environment]::GetEnvironmentVariables('Machine').GetEnumerator() | Sort-Object Name | format-table Name, Value -AutoSize | Out-String -Width 1024" > ${scriptdir}\${containername}-MachineVariables.txt
"Done!`n"
Read-Host -Prompt "Press Enter to exit"

Applies to:  

Product Version
KTA All

 

 

  • Was this article helpful?