Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if process executes inside a Windows Container

It's simple. I would like to detect with code if my process is running inside a windows container. There are examples but they are all for linux based containers.

I'm looking for something unique and explicit to docker that can be used to make a safe conclusion whether a process is executing inside a container hosted windows operating system and not otherwise.

My preferred language is PowerShell but if someone points out the how to detect, I'll port it to PowerShell.

like image 725
Alex Sarafian Avatar asked Mar 24 '17 15:03

Alex Sarafian


People also ask

Can you see the processes running inside a container from the outside?

Yes this is quite normal, the pid inside the container, or, atleast the MAIN pid will always be 1. But since docker uses the kernel on the host, and not its own, you will see it in ps command on the host.

How do I see what's running in my docker container?

Listing Containers. In order to list the Docker containers, we can use the “docker ps” or “docker container ls” command. This command provides a variety of ways to list and filter all containers on a particular Docker engine.


1 Answers

New readers can skip ahead to the part marked with "Update" which contains the accepted solution.

A quick check with whoami on the command prompt showed that the combination of domain and username that is used inside a container seems to be rather unusual. So I used this code to solve the problem:

function Test-IsInsideContainer {
  if( ($env:UserName -eq "ContainerAdministrator") -and ($env:UserDomain -eq "User Manager") ) {
    $true
  }
  else {
    $false
  }
}

Update: Another option is to check if the service cexecsvc exist. An internet search did not yield much information about this service, but its name (Container Execution Agent) suggests that it only exists inside of containers (which I verified with some quick test on Win10 and a Server2016 Docker-host). So maybe this code meets your requirements (I am a newbie in Powershell):

function Test-IsInsideContainer {
  $foundService = Get-Service -Name cexecsvc -ErrorAction SilentlyContinue
  if( $foundService -eq $null ) {
    $false
  }
  else {
    $true
  }
}
like image 65
frank koch Avatar answered Oct 14 '22 05:10

frank koch