5

I have to create a Windows container image with our application installed to it. Our application uses hostnames in several places during the installation. The docker build command does not have an option to pass the hostname - so we are currently stuck at a point where when we run a container with the image created, our application fails to run, as it is expecting the hostname to be the generated name during the image creation time, and not the one we pass to the docker run command.

I tried to change the hostname within the container before installing our application, using suggestions from this question - but it did not work.

I want to understand if there is a way we can change the hostname from within the Windows container.

alex
  • 6,818
  • 9
  • 52
  • 103
Sathish M
  • 51
  • 1

1 Answers1

0

The following worked for me -
rename-computer.ps1:

$ComputerName = "New Name"
   
Remove-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "Hostname" 
Remove-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "NV Hostname" 

Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Computername\Computername" -name "Computername" -value $ComputerName
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Computername\ActiveComputername" -name "Computername" -value $ComputerName
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "Hostname" -value $ComputerName
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "NV Hostname" -value  $ComputerName
Set-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -name "AltDefaultDomainName" -value $ComputerName
Set-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -name "DefaultDomainName" -value $ComputerName

Dockerfile:

COPY "rename-computer.ps1" "C:/"  
RUN powershell -command "Set-ExecutionPolicy RemoteSigned" \
 && powershell -command "C:/rename-computer.ps1 ; hostname"  \
 && powershell -command "Set-ExecutionPolicy Restricted" \
 && <your installer runs here> 

Sources -
https://serverfault.com/a/731832/845133
https://gist.github.com/timnew/2373475

jrbe228
  • 356
  • 3
  • 11
  • Note that 15 characters is the maximum length for ComputerName - https://learn.microsoft.com/en-us/troubleshoot/windows-server/identity/naming-conventions-for-computer-domain-site-ou – jrbe228 Feb 06 '23 at 00:07