pythondockervisual-c++cythoncl.exe

How to compile C++ code or Cython in a MS Windows Docker image


I want to build a docker MS Windows image to compile python and cython code via command line. cython requires Microsoft Visual C++ 14.0 or greater (Link).

Example

The initial goal is to compile the example taken from cython.org within a Windows docker image.

hello.pyx

print('Hello World')

setup.py

from setuptools import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("hello.pyx"))

When executing the following command in a bash shell (linux) it compiles.

$ python setup.py build_ext --inplace`
Compiling hello.pyx because it changed.
[1/1] Cythonizing hello.pyx

MS Windows Docker image

I tried a variety of commands which I could find for installing Microsoft Visual C++, but nothing seemed to work. The following Dockerfile is my current default:

Dockerfile

FROM mcr.microsoft.com/dotnet/framework/runtime:4.8-windowsservercore-ltsc2019

SHELL ["cmd", "/S", "/C"]

# Download and build chocolatey
RUN powershell Set-ExecutionPolicy Bypass -Scope Process -Force
RUN powershell [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
RUN powershell iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

RUN powershell choco upgrade chocolatey

# Installing packages via chocolatey
RUN powershell choco install visualstudio2022buildtools -y --package-parameters "--includeRecommended"
RUN powershell choco install python -y

# Add bash shell
RUN powershell choco install git -y
RUN setx path "C:\\Program Files\\Git\\bin;%path%"

# adds gcc and gfortran
RUN powershell choco install mingw -y

ENTRYPOINT [ "cmd" ]

You can create a Docker image windows:python3.11 via the following command (on a Windows machine):

$ docker build --rm --file ./Dockerfile --tag windows:python3.11 .

Request

With this MS Windows Docker image in mind I try to compile the example and get the following result:

Microsoft Windows [Version 10.0.17763.4499]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\>pip install cython
Collecting cython
  Downloading Cython-0.29.35-py2.py3-none-any.whl (988 kB)
     ---------------------------------------- 988.4/988.4 kB 12.5 MB/s eta 0:00:00
Installing collected packages: cython
Successfully installed cython-0.29.35

C:\>python setup.py build_ext --inplace
running build_ext
building 'hello' extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

Question: How to correctly set up Microsoft Visual C++ 14.0 (docker) for cython?

error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

Regarding the MS C++ compiler, I have found not one method to access the Microsoft C compiler cl.exe. Any help would be appreciated. Commandline only.


Solution

  • Working Dockerfile for compiling cython in a MS Windows container

    When you install Visual Studio via buildtools.exe in the container, the c++ compiler is found by cython, but still, the MS windows c compiler can not simply be called via the Windows shell. I guess that you have to add the compiler location to the PATH variable.

    FROM mcr.microsoft.com/dotnet/framework/runtime:4.8-windowsservercore-ltsc2019
    ENV LANG=C.UTF-8
    
    SHELL ["cmd", "/S", "/C"]
    
    #================================#
    # Download and build chocolatey
    RUN powershell "Set-ExecutionPolicy Bypass -Scope Process -Force"
    RUN powershell "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072"
    RUN powershell "iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))"
    
    # importing chocolatey functions in the powershell
    RUN powershell "Import-Module ${env:ChocolateyInstall}\\helpers\\chocolateyProfile.psm1"
    
    #================================#
    # Installing python and cython
    RUN powershell "choco install python -y"
    RUN powershell "python -m pip install --upgrade pip build"
    RUN powershell "pip install cython"
    
    #================================#
    # Adds Visual Studio + recommend tools
    RUN powershell "Invoke-WebRequest -Outfile buildtools.exe -Uri https://aka.ms/vs/17/release/vs_BuildTools.exe"
    RUN powershell ".\buildtools.exe --quiet --wait --norestart --nocache --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"
    
    #================================#
    ENTRYPOINT [ "cmd" ]