Skip to content

Instantly share code, notes, and snippets.

@keyan1603
Created August 9, 2025 07:23
Show Gist options
  • Select an option

  • Save keyan1603/89835586bcb610c17d36d68f882b77d4 to your computer and use it in GitHub Desktop.

Select an option

Save keyan1603/89835586bcb610c17d36d68f882b77d4 to your computer and use it in GitHub Desktop.
Add host.docker.internal and gateway.docker.internal to hostentries using Entrypoint in Containers in Sitecore
# Path to the system hosts file
$hostsFile = "C:\Windows\System32\drivers\etc\hosts"
try {
# List of Docker special DNS names used to refer to the host
$DnsEntries = @("host.docker.internal", "gateway.docker.internal")
# Attempt to resolve each DNS entry
foreach ($Entry in $DnsEntries) {
Resolve-DnsName -Name $Entry -ErrorAction Stop # Throws error if resolution fails
}
# If no exception was thrown, DNS is properly configured
Write-Host("DNS settings are already configured.")
}
catch {
# If DNS resolution fails, retrieve the host (gateway) IP from ipconfig output
$ip = (ipconfig | where-object {$_ -match "Default Gateway"} | foreach-object{$_.Split(":")[1]}).Trim()
# Read current lines from the hosts file
$src = [System.IO.File]::ReadAllLines($hostsFile)
# Create a modifiable copy of the lines with an extra line at the end
$lines = $src += ""
# Check if the hosts file already contains both entries
if ((cat $hostsFile | Select-String -Pattern "host.docker.internal") -And (cat $hostsFile | Select-String -Pattern "gateway.docker.internal")) {
# Update existing entries with the current host IP
For ($i = 0; $i -le $lines.length; $i++) {
if ($lines[$i].Contains("host.docker.internal")) {
$lines[$i] = ("{0} host.docker.internal" -f $ip)
$lines[$i+1] = ("{0} gateway.docker.internal" -f $ip)
break
}
}
} else {
# Append new entries if not already present
$lines = $lines += "# Added by Docker for Windows"
$lines = $lines += ("{0} host.docker.internal" -f $ip)
$lines = $lines += ("{0} gateway.docker.internal" -f $ip)
$lines = $lines += "# End of section"
}
# Write the updated hosts file content back to disk
[System.IO.File]::WriteAllLines($hostsFile, [string[]]$lines)
Write-Host("New DNS settings written successfully.")
}
# Launch LogMonitor (required by Sitecore container) and start IIS service inside the container
& "C:\LogMonitor\LogMonitor.exe" "powershell" "C:\Run-W3SVCService.ps1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment