skip to Main Content

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?

2

Answers


  1. 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 
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search