Docker PowerShell Script - Tip of the Day
Like any other technology, Docker has its own pet peeves. One of the things that annoys me the most is port in use. Most of the times, we get to know about an used port when that specific container starts up and that happens towards the fag-end. Something like this:
Error:
Error response from daemon: failed to create endpoint docker-examples-solr-1 on network docker-examples_default: failed during hnsCallRawResponse: hnsCall failed in Win32: The process cannot access the file because it is being used by another process. (0x20)
Specifically, in the above case, we usually have a SOLR_PORT parameter in the .env file. This variable gets passed to the docker-compose.yml. Two different docker compose files below, one has param passed from .env file while the port number is hard-coded in another. For the current discussion, the latter is anyway out of context.
So, the value passed through the .env param is what matters here. Although the value is passed dynamically through a .env variable, it solves only a part of the problem. This is where PS comes to the rescue. We can test the port as follows:
test-netconnection localhost -port 8983
So, stick this code on top of up.ps1 file:
$envContent = Get-Content .env -Encoding UTF8
$SolrPort = $envContent | Where-Object { $_ -imatch "^SOLR_PORT=.+" }
$SolrPort= $SolrPort.Split("=")[1]
Write-Host "SOLR Port: "$SolrPort
if ((test-netconnection localhost -port $SolrPort).TcpTestSucceeded -eq $true) {
throw "Solr port in use, Specify an unused port in .env file for SOLR_PORT"
}
This way, you get an error as soon as you start the script rather than at the fag-end!
This approach should be used for all variables passing port numbers in .env file:
Comments
Post a Comment