serving the solutions day and night

Pages

Saturday, April 11, 2020

Docker Commands

Verify the application exist inside the windows container,  connect to the cmd of the "frontendapiwin" container using the docker exec command
1. C:\> docker exec -it 64a61d823b28  cmd
           - "64a61d823b28 " - docker container id
           - It should take us inside the container.
     Run the powershell inside the "frontendapiwin" the container 
     1.1 C:\inetpub\wwwroot> powershell 
            Using Powershell to manage application web configuration
           1.1.1 PS C:\inetpub\wwwroot> gc .\web.config   or  gc web.config 
                       - Gets the content (gc) of the item at the specified location (C:\inetpub\wwwroot\web.config)
         > [xml]$web = gc .\web.config
                         -  The [xml] casts the variable as an XML object.
                         - <?xml>
                            <configuration>
                                    <appsettings>
                                             <add key="DBCon" value="db value."/>
                                             ...
                                     </appsettings>
                                    ...
                             </configuration>
         > $web.configuration.appSettings.SelectNodes("add[@key='DBCon']")
                           - display DBCon key value
                           -  key                 value
                              -----                 -------
                               DBCon        db value.
         > $key=$web.configuration.appSettings.SelectNodes("add[@key='DBCon']")
                           - assign to key
         > $key
                           - another way display DBCon key value
         > $key.SetAttribute("value","db value changed.")
                           - change DBCon value
         > $web.save(".\web.config")
                           - save file
         > exit  => exit from powershell
           > exit  = exit from container


Verify the application exist inside the windows container,  connect to the powershell of the "frontendapiwin" container using the docker exec command
2. C:\> docker exec -it 64a61d823b28  powershell
ls env:\
Invoke-WebRequest http://localhost
Invoke-WebRequest http://localhost/test.html
Invoke-WebRequest http://localhost/test.html -usebasicparsing
curl -V http://localhost/login.aspx 

Verify the application exist inside the linux container,  connect to the bash of the "frontendapiunix" container using the docker exec command
3. C:\> docker exec -it 64a61d823b28  bash or docker exec -it sqlserver2017withoutmount bash
        -  Let’s verify the database and log files exist inside the container
        -  Go to “/var/opt/mssql/data” folder and verify that our demo.mdf and demo_log.ldf exists.
root@3974dc57923e:/# ls
      bin  boot  dev  etc  home  install.sh  lib  lib64  ...  srv  sys  tmp  usr  var
root@3974dc57923e:/# cd var/opt/mssql/data
root@3974dc57923e:/var/opt/mssql/data# ls
       master.mdf  mastlog.ldf ... demo.mdf  demo_log.ldf
root@3974dc57923e:/var/opt/mssql/data# exit

Test Path
Test-Path -Path "C:\source\source.txt" => True
Test-Path -Path "C:\destination\source.txt" => False
Test-Path -Path "C:\source" -PathType Container => True
Test-Path -Path "C:\source1" -PathType Container => False

Copy Items
Copy-Item "C:\source\source.txt" -Destination "C:\destination"
- source.txt copied to "C:\destination" folder, source\source.txt file not deleted
Copy-Item "C:\source\source.txt" -Destination "C:\destination\source.txt"
- source.txt copied to "C:\destination\destionation.txt, source file not deleted
Copy-Item "C:\source\source.txt", "C:\destination\source.txt"
Copy-Item "C:\source\26?0.txt", "C:\destination"
- 2600.txt, 2611.txt 2622.txt file copied
Copy-Item "C:\source\*","C:\source2\*","C:\source3\*" -Destination "C:\destination"
- copied multiple folder files into destination folder
Get-ChildItem "C:\source\" | Copy-Item -Destination "C:\destination" -Recurse
- copied sub folders also using pipeline (|)
Copy-Item -Path "C:\source\*" -Destination "C:\destination" -Recurse
- copied sub folders also

File Count
@(Get-ChildItem -Path C:\source).Count => 100
(Get-ChildItem -Path C:\source -Recurse).Count => 110 , count subfolder files also

Get File List 
Get-ChildItem -Path C:\source\ OR gci -Path C:\source\
Directory: C:\source Mode LastWriteTime Length Name -a---- 18/01/2020 1:53 AM 5 2610.txt -a---- 18/01/2020 2:64 AM 5 2620.txt -a---- 18/01/2020 3:15 AM 5 2630.txt -a---- 18/01/2020 4:26 AM 5 2640.txt
-a---- 18/01/2020 7:29 AM 5 soruce.txt

Get-CimClass
Gets instances of Windows Management Instrumentation (WMI) classes or information about the available classes.
Get-CimClass -List
Get-WmiObject -Class Win32_Process

Combine 2 Arrays
Example 1
$language = @("Net","Java","PHP") $storage = @("SQLServer","Redis","MySQL", "Mongo", "Elastic") $lanstore = $language + $storage $lanstore Net Java PHP SQLServer Redis MySQL
Mongo
Elastic

Example 2
$lanstore = @() $length = [Math]::Max($language.Length, $storage.Length) for ($i = 0; $i -lt $length; $i++) { $lanstore +=$language[$i] $lanstore +=$storage[$i] } $lanstore Net SQLServer Java Redis PHP MySQL Mongo Elastic

Example 3
[int]$max = $language.Count if ([int]$storage.count -gt $max) { $max = $storage.Count; } $lanstore = for ( $i = 0; $i -lt $max; $i++) { Write-Verbose "$($language[$i]),$($storage[$i])" [PSCustomObject]@{ Language = $language[$i] Storage= $storage[$i] } } $lanstore Language Storage -------- ------- Net SQLServer Java Redis PHP MySQL Mongo Elastic

Get average CPU usage of a computer in last x minute
Get-counter -Counter “\Processor(_Total)\% Processor Time” -SampleInterval 1 -MaxSamples 5

Timestamp                      CounterSamples
---------                              --------------
4/11/2020 11:26:37 PM     \\txdns-km\processor(_total)\% processor time :
                          20.382507661013673

4/11/2020 11:26:38 PM     \\txdns-km\processor(_total)\% processor time :
                          16.87571819375431

4/11/2020 11:26:40 PM     \\txdns-km\processor(_total)\% processor time :
                          15.712810668962952

4/11/2020 11:26:41 PM     \\txdns-km\processor(_total)\% processor time :
                          18.759972698625806

4/11/2020 11:26:42 PM     \\txdns-km\processor(_total)\% processor time :

                          17.256676375051473

$CPUAveragePerformance = (GET-COUNTER -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 2 -MaxSamples 5 |select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average
   - average cpu withing 2 seconds of 5 intervales.

Monitor folder/files changes, using a .NET class called FileSystemWatcher
$file_system_watcher = New-Object System.IO.FileSystemWatcher
$file_system_watcher.Path = "C:\mak\" => mention the folder to monitor
$file_system_watcher.Filter = "*.*"
$file_system_watcher.IncludeSubdirectories = $true => include subdirectories true or false
$file_system_watcher.EnableRaisingEvents = $true 

- define actions after an event is detected
$write_action = { $full_path = $Event.SourceEventArgs.FullPath
$change_type = $Event.SourceEventArgs.ChangeType
$log_line = "$(Get-Date), $change_type, $full_path"
Add-content "C:\mak\file_watcher_log.txt" -value $log_line
      }   
- decide which events should be watched
- Register-ObjectEvent subscribes to events that are generated by .NET objects.
- When the subscribed event is raised, it is added to the event queue in your session.
- To get events in the event queue, use the Get-Event.
Register-ObjectEvent $file_system_watcher "Created" -Action $write_action
Register-ObjectEvent $file_system_watcher "Changed" -Action $write_action
Register-ObjectEvent $file_system_watcher "Deleted" -Action $write_action
Register-ObjectEvent $file_system_watcher "Renamed" -Action $write_action
while ($true) {sleep 5}

file_watcher_log.txt
04/11/2020 23:45:10, Changed, C:\mak\Web.config
04/11/2020 23:45:11, Changed, C:\mak\Web.config
04/11/2020 23:45:11, Created, C:\mak\file_watcher_log.txt
04/11/2020 23:45:11, Changed, C:\mak\file_watcher_log.txt

Using Explorer to View Directories in the GUI
PS C:\mak> explorer .

Build & Run
docker build -t frontendapiwin .
docker run -dit --name frontendapiwin1 -p 8000:80 frontendapiwin
docker run -p 5002:80 frontendapiwin
docker run -itdp 8000:80 frontendapiwin:dev
docker attach 82e08c546fd5 => connected to the Container.

Get-hnsnetwork | remove-hnsnetwork
Restart-service docker
Stop-Service docker
Stop-service hns
Start-service hns
Start-Service docker

Reference https://docs.microsoft.com/en-us/powershell/scripting/overview?view=powershell-7

No comments: