Feature Request: IfWindows / IfLinux command

something simple in theory, but practice :face_with_spiral_eyes:

Looking for something that would allow a Dockerfile to do a specific thing on a windows base image vs a linux based image.



FROM python:${HOST_OS}

IFWINDOWS RUN powershell write-host $env:OS


FROM python:${HOST_OS}

IFWINDOWS RUN powershell write-host $env:OS

If you want to send a feature request that the developers can see more easily, open a request in the roadmap:

Here in the forum you can discuss the requests with community members.

I personally think that using IFWINDOWS and IFLINUX keywords would make a Dockerfile less clear. For example when you need to copy scripts you would need those keywords before the COPY instruction too and basically before everything. I would rather use two different Dockefile. It doesn’t matter of course, it is just my opinion and an optional feature would not force me to use it so try to ask for the freature in the roadmap.

It’s not what you ask for, but there are builtin variables to determine the OS so you don’t need to define it yourself at least.



The following ARG variables are set automatically:

  • TARGETPLATFORM - platform of the build result. Eg linux/amd64, linux/arm/v7, windows/amd64.
  • TARGETARCH - architecture component of TARGETPLATFORM
  • TARGETVARIANT - variant component of TARGETPLATFORM
  • BUILDPLATFORM - platform of the node performing the build.
  • BUILDARCH - architecture component of BUILDPLATFORM
  • BUILDVARIANT - variant component of BUILDPLATFORM
1 Like

Good to know, but not quite what I was looking for.

With my limited knowledge of Docker, I have not had any real problems with the built in commands. This feature thought came from my developing and learning docker using WSL on windows and it working well, talking to my manager and his thoughts were that we do a lot of Windows dev, and most of our servers are windows, so he asked if there was a way to re-write/modify them accordingly… The programmer brain said make it as portable as possible, but without some sort of branching structure, it kinda became a mess of environment variables and command line args.

I know, real world problems often lead to the need of new features, so it might be a completely valid and necessary feature, but it is also possible that we can find you an alternative if you can share why your attempt to solve this without the feature became a “mess of environment variables”. My first thought is crating a “Dockerfile.windows” and a “Dockerfile.linux” file and just build the two images from two different files.

Yup, was doing that. And also had .env.linux and .env.windows files defined as well.

That didn’t work as well as I wanted, as I found that I would have to add the --env-file=.env.(windows|linux) every time that I ran the docker commands (this might be something avoidable, but I am still a bit of a Docker n00b), when I was just hoping that I could docker run image

If the environment variables have to be different, how would the requested feature help? Yes, you could add conditional ENV instructions, but than you can do the same in each Dockerfile so you don’t need to when you start a container.

So I still don’t get the big picture here, but I would really like to help, because I don’t think the request would be implemented any time soon even if it is accepted. If you want, you can also create a new topic where we can focus on your goal and we can leave a link here to that topic too.

The ENV was just an example, the real meat would be the RUN commands. I was struggling to try and make a single Dockerfile that was more or less perfectly portable across Windows and Linux. The other part is that I wish Docker had the ability to do simple branching, in a single level of an IF/ELSE type block, or a SWITCH.

The point of a Dockerfile is that it’s simple. You can create multi-arch images, because you can use the built-in variables to download files for the required architecture, but you don’t have to add instructions that wouldn’t be used. If the point is having a single Dockerfile, you can use multi-stage build, name the stages as windows and linux and build the required target.

You can also run shell commands instead of each instruction in the Dockerfile. I wrote about that here:

This is basically how Docker built Linux images before using buildx as default builder which is using buildkit by default and doesn’t use Docker containers for building but directly runs (if it is still true) “runc”. I never tried to do it with Windows images.

I still feel that your issue is too specific, but again, I’m not the one who decides.