0

I'm maintaining a Docker image of a database (Linux). The image runs on Linux and Windows (Linux container on Windows - lcow). But on Windows it behaves a bit different.

How can I detect if the host OS is Windows from inside the container?

Michael
  • 2,528
  • 3
  • 21
  • 54

4 Answers4

1

You cannot check the host OS from inside the container.

As workaround, I suggest to think in the opposite way and, so, to add an environmental variable inside the Dockerfile, such as:

ARG HOST
RUN echo $HOST

and then set it through the build command with --build-arg options:

docker build --build-arg HOST=windows .

Then, according to the value of the variable you can differenciate the behavior of the container or of the image you're building.

Alez
  • 1,913
  • 3
  • 18
  • 22
1

you can use a bind mount and in the container determine some characteristic of the filesystem (e.g. type, content, etc.) to guess the platform

docker run ... --mount type=bind,source=/some-dir,target=/target ...
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

Docker exposes certain environment variables to each container that can provide information about the host. You can try checking the OSTYPE environment variable, which might provide information about the host operating system.

Here's an example command that you could use within a Linux container to check the host's OSTYPE:

echo $OSTYPE 
0

A Linux container on Windows is a Hyper-V virtual machine. And it seems to have Hyper-V devices.

For example bash container under Windows host provides this directory:

/sys/devices/platform/HYPER_V_GEN_COUN:00

This directory is not present on the bash container on a Linux host.

But I'm not sure if this is always the case for any image. There are several hyper-v directories under /sys that might help determining the host OS.

Michael
  • 2,528
  • 3
  • 21
  • 54