After the last post, I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM.
WIN32 Support
To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class.
The sample can be found on Github and can be run easily using the following commands:
Code sample: https://github.com/vunvulear/Stuff/tree/master/Azure/Docker.Win32
How is this possible?
Starting from Windows Server 2016 (and Windows 10 Anniversary Upgrade), Windows can run containers in two different ways.
The first mechanism is using Windows Containers, that runs on top of Host OS kernel, sharing the same kernel between containers. This means that the kernel, binary libraries and many more are shared between containers. On top of this, all the containers are affected and use the same kernel.
In contrast with Windows Containers, Hyper-V Containers runs directly on top of Hyper-V. Each container uses his own kernel image. This allows us to run multiple containers, each of them using a different version of the OS. We have more flexibility, but the footprint might be a little higher.
Docker for Windows is using Hyper-V containers. This means for each container that we run, we will have our own instance of kernel, that is different from the Host version. Having our own kernel instance, give us access to all information, including the Win32 API, as long as the Docker image that we use has that Win32 API available.
COM Support
The second topic is related to COM. The short response is yes, we can make COM calls. Things are more complex at setup level. We need to be sure that that COM is register and available in our image. In the sample provided on GitHub, I register directly from C# code the COM that I use.
Registration
COM call
As we can see above, we can make WIN32 and COM calls from Docker.
The last question that I received yesterday is related to ASP.NET Web Forms and if we can run them in Docker Containers. Microsoft created a Docker image for ASP.NET applications (https://hub.docker.com/r/microsoft/aspnet/) . Inside it, we can run any kind of ASP.NET applications the MVC to Web Forms or other kind of applications. The IIS is already configured, so we don’t need to configure any bindings to it.
The sample code for ASP.NET WebForms can be found on GitHub: https://github.com/vunvulear/Stuff/tree/master/Azure/Docker.WebForms
In comparison with the previous samples we used ‘microsoft/aspnet’ Docker image. Don’t forget to specify in the browser the IP of the container, when you want to access the web application. The IP of the Docker container can be obtained using the following command:
In this post we validated that we can do the following things in a Docker Container:
WIN32 Support
To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class.
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}
...
[DllImport("kernel32")]
static extern void GetSystemInfo(ref SYSTEM_INFO pSI);
...
SYSTEM_INFO pSI = new SYSTEM_INFO();
GetSystemInfo(ref pSI);
The sample can be found on Github and can be run easily using the following commands:
docker build -f Docker.Win32.Image.txt -t win32 .
docker run win32
As we can see, we were able to make with success a call to a WIN32 API.Code sample: https://github.com/vunvulear/Stuff/tree/master/Azure/Docker.Win32
How is this possible?
Starting from Windows Server 2016 (and Windows 10 Anniversary Upgrade), Windows can run containers in two different ways.
The first mechanism is using Windows Containers, that runs on top of Host OS kernel, sharing the same kernel between containers. This means that the kernel, binary libraries and many more are shared between containers. On top of this, all the containers are affected and use the same kernel.
In contrast with Windows Containers, Hyper-V Containers runs directly on top of Hyper-V. Each container uses his own kernel image. This allows us to run multiple containers, each of them using a different version of the OS. We have more flexibility, but the footprint might be a little higher.
Docker for Windows is using Hyper-V containers. This means for each container that we run, we will have our own instance of kernel, that is different from the Host version. Having our own kernel instance, give us access to all information, including the Win32 API, as long as the Docker image that we use has that Win32 API available.
COM Support
The second topic is related to COM. The short response is yes, we can make COM calls. Things are more complex at setup level. We need to be sure that that COM is register and available in our image. In the sample provided on GitHub, I register directly from C# code the COM that I use.
Registration
public static void RegistarDlls(string filePath)
{
string fileinfo = "/s" + " " + "\"" + filePath + "\"";
Process reg = new Process();
reg.StartInfo.FileName = "regsvr32.exe";
reg.StartInfo.Arguments = fileinfo;
reg.StartInfo.UseShellExecute = false;
reg.StartInfo.CreateNoWindow = true;
reg.StartInfo.RedirectStandardOutput = true;
reg.Start();
reg.WaitForExit();
reg.Close();
}
...
RegistarDlls("Interop.NetFwTypeLib.dll");
COM call
const string guidFWPolicy2 = "{E2B3C97F-6AE1-41AC-817A-F6F92166D7DD}";
const string guidRWRule = "{2C5BC43E-3369-4C33-AB0C-BE9469677AF4}";
Type typeFWPolicy2 = Type.GetTypeFromCLSID(new Guid(guidFWPolicy2));
Type typeFWRule = Type.GetTypeFromCLSID(new Guid(guidRWRule));
INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(typeFWPolicy2);
INetFwRule newRule = (INetFwRule)Activator.CreateInstance(typeFWRule);
newRule.Name = "MabuAsTcpLocker_OutBound_Rule";
newRule.Description = "Block outbound traffic over TCP port 80";
newRule.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
newRule.RemotePorts = "8888";
newRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
newRule.Enabled = true;
newRule.Profiles = 6;
newRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
As we can see above, we can make WIN32 and COM calls from Docker.
The last question that I received yesterday is related to ASP.NET Web Forms and if we can run them in Docker Containers. Microsoft created a Docker image for ASP.NET applications (https://hub.docker.com/r/microsoft/aspnet/) . Inside it, we can run any kind of ASP.NET applications the MVC to Web Forms or other kind of applications. The IIS is already configured, so we don’t need to configure any bindings to it.
The sample code for ASP.NET WebForms can be found on GitHub: https://github.com/vunvulear/Stuff/tree/master/Azure/Docker.WebForms
In comparison with the previous samples we used ‘microsoft/aspnet’ Docker image. Don’t forget to specify in the browser the IP of the container, when you want to access the web application. The IP of the Docker container can be obtained using the following command:
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" webforms
In this post we validated that we can do the following things in a Docker Container:
- Make Win32 calls
- Use COM Interop
- Run a ASP.NET WebForms application
Comments
Post a Comment