How to run Windows Container with ultralight Docker-CE CLI


This will NOT include the docker desktop, and it is COMMAND-LINE ONLY, Like in Linux.
Hyper-V, WSL, VT-d are NOT required.
Please check Limitations first.


Install

You only need 4 files in total.

First download docker engine from the official:
https://download.docker.com/win/static/stable/x86_64/
After unzipping, you will have the following 3 files:
docker.exe
dockerd.exe
docker-proxy.exe

Put all 3 files into whatever folder you want as the install folder, for example:
C:\Program Files\docker
Add the install folder to your environment variable (Optional)
PS(ADMIN)>[Environment]::SetEnvironmentVariable('Path',"$([Environment]::GetEnvironmentVariable('Path','Machine'))"+';<path to docker>','Machine')

Then, download the docker-compose from the official repo: (Optional)
https://github.com/docker/compose/releases/
you will get docker-compose-windows-x86_64.exe
Rename it to docker-compose.exe
Put it to C:\Users\<username>\.docker\cli-plugins

Run docker without Elevated privileges: (Optional)
PS(ADMIN)>net localgroup docker /add
PS(ADMIN)>net localgroup docker <your username> /add

Next, register the docker service:
If you set up the group above
PS(ADMIN)>&'<Path to docker>\dockerd.exe' --register-service -G docker
Otherwise
PS(ADMIN)>&'<Path to docker>\dockerd.exe' --register-service

Final step, enable the Containers feature for Windows.
(optional) If you want to use hyper-v isolation for your container, then you need to enable Hyper-V as well.

That’s it, restart computer, and you are ready to go.

To verify the installation:
PS(ADMIN)>docker version


Alternate Install

Download the following zip and run the install.ps1 in PowerShell

This will automatically install docker into C:\Program Files\docker


Limitations

  • You can only run Windows-based images. To run Linux-based image, you need Docker Desktop or WSL.
  • You can only run images that match your Windows build version, you can run winver to check your version.
    Except Win11 can run version 20348 and above, therefore Win11 or Server2022 are recommended.
  • Docker engine seems does not correctly clean temp data for Windows. You may need to clean it manually if space is needed.
    Default path: C:\ProgramData\docker
    Take over ownership, assign yourself permissions then remove it.
  • Windows images are huge, can easily take 10-100GB and more.


Special features

Those arguments are exclusive to Docker for Windows

--isolation <hyperv | process>, for example:
Docker run -it --rm --isolation hyperv mcr.microsoft.com/windows/nanoserver:ltsc2022 cmd
There are two options hyperv or process. Both docker run and docker build can use this argument.
In short, process mode is the way you typically use in Linux, I suggest you run containers in this mode.
Hyperv mode means you will run the container inside a VM for full separation from the host. I suggest you build containers in this mode. This mode requires hyper-v installed on the host.

--cpu_count <int>
Specify how many cores you want to assign to the container.


Docker Hub Containers for Windows

https://hub.docker.com/search?q=&operating_system=windows


Build Containers

Basically, there is no difference then the Linux version.
Here is an example of a container that run Stable-Diffusion with CUDA GPU support

Dockerfile

FROM mcr.microsoft.com/windows/server:ltsc2022

EXPOSE 7860

ENV SKIP_VENV=1
ENV VENV_DIR=-
ENV COMMANDLINE_ARGS='--update-check --listen --data-dir .\\udata'

COPY ./res C:/res

WORKDIR C:/res

RUN powershell Start-Process -wait -FilePath python-3.10.11-amd64.exe -ArgumentList '/quiet InstallAllUsers=0 InstallLauncherAllUsers=0 PrependPath=1' ; \
    Start-Process -wait -FilePath vc_redist.x64.exe -ArgumentList '/install /passive /norestart' ; \
    Move-Item -Force '.\git' 'C:\Program Files\' ; \
    Move-Item -Force '.\nvdll\*' 'C:\Windows\System32\' ; \
    Start-Process -wait -FilePath regsvr32 -ArgumentList '/s C:\Windows\System32\nvcuda.dll' ; \
    Start-Process -wait -FilePath regsvr32 -ArgumentList '/s C:\Windows\System32\nvapi64.dll' ; \
    Import-Certificate -FilePath .\cacert.sst -CertStoreLocation 'Cert:\LocalMachine\Root' ; \
    [Environment]::SetEnvironmentVariable('Path',"$([Environment]::GetEnvironmentVariable('Path','Machine'))"+';C:\Program Files\git\bin','Machine')

WORKDIR C:/
RUN git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git .\app && \
    git config --global --add safe.directory "*" && \
    cd .\app && \
    rmdir /s /q c:\res && \
    pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cu118 && \
    pip install --use-pep517 --no-cache-dir -r requirements.txt --prefer-binary && \
    set COMMANDLINE_ARGS=--exit --xformers --skip-torch-cuda-test && \
    pip cache purge && \
    webui.bat


WORKDIR C:/app
CMD webui.bat

docker-compose.yaml

version: "3.8"

networks:
  windows:

services:
  windows:
    container_name: stable_diffusion
    build:
      context: .\build
      isolation: hyperv     # Change this to "process" if you dont have hyperv
      no_cache: true
    isolation: process
    networks:
      - stable_diffusion
    cpu_count: 8
    ports:
      - "7860:7860"
    environment:
      - COMMANDLINE_ARGS=--update-check --listen --data-dir .\\udata --medvram     # See https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Command-Line-Arguments-and-Settings
    volumes:
      - '.\data:C:\app\udata'
      - '.\outputs:C:\app\outputs'
    devices:
      - class/5B45201D-F2F2-4F3B-85BB-30FF1F953599     # Passing GPU, This GUID is fixed

Resources

References

https://learn.microsoft.com/en-us/virtualization/windowscontainers/manage-docker/manage-windows-dockerfile

By Eisai