Skip to content

Instantly share code, notes, and snippets.

@ESultanik
Last active February 18, 2022 16:10
Show Gist options
  • Save ESultanik/2e8aad547e39849f4a4d15b431fe0c40 to your computer and use it in GitHub Desktop.
Save ESultanik/2e8aad547e39849f4a4d15b431fe0c40 to your computer and use it in GitHub Desktop.
Running a Windows Docker Container from macOS

Running a Windows Docker Container from macOS

Prerequisites

  1. A local install of Docker
  2. A local install of Vagrant
  3. VMWare or VirtualBox

Setting up the Windows Docker VM

https://github.com/StefanScherer/windows-docker-machine

$ git clone https://github.com/StefanScherer/windows-docker-machine
$ cd windows-docker-machine
$ vagrant up --provider vmware_desktop 2019-box

- or -

$ vagrant up --provider virtualbox 2019-box

Switch to the Windows Docker Instance

$ docker context use 2019-box

Afterward, to switch back to the original Docker Instance

$ docker context use default

Running Cygwin in a Windows Docker Container from macOS

This Dockerfile will create a container with Python 3.9, a JVM, git, and Cygwin.

FROM python:3.9

RUN New-Item -Path C:\cygwin -ItemType Directory -Force
RUN (New-Object Net.WebClient).DownloadFile('https://cygwin.com/setup-x86_64.exe', 'C:\cygwin\setup-x86_64.exe')
RUN cmd /c start /wait C:\cygwin\setup-x86_64.exe -qnNdO -R C:/cygwin -s http://cygwin.mirror.constant.com -l C:/cygwin/var/cache/setup -P bash
ENV CYGWIN_HOME=C:\\cygwin
RUN setx path '%path%;C:\cygwin\bin'

RUN python -m pip install --upgrade pip

# To install new cygwin packages like git:
RUN cmd /c start /wait C:\cygwin\setup-x86_64.exe -qnNdO -R C:/cygwin -s http://cygwin.mirror.constant.com -l C:/cygwin/var/cache/setup -P git

# If you need a JVM:
RUN Invoke-WebRequest https://api.adoptopenjdk.net/v3/installer/latest/11/ga/windows/x64/jdk/hotspot/normal/adoptopenjdk?project=jdk -OutFile C:\openjdk11.msi
RUN Start-Process -Wait -FilePath msiexec -ArgumentList /i, "C:\openjdk11.msi", "'ADDLOCAL=\"FeatureMain,FeatureEnvironment,FeatureJarFileRunWith,FeatureJavaHome\"'", "'INSTALLDIR=\"C:\Program Files\Java\jdk-11\"'", /quiet, /norestart -Verb RunAs

CMD C:\Cygwin\bin\bash.exe

Building the container

docker build -t windowspython .

Running the container

docker run -it --rm windowspython

Mounting a directory from macOS to the container

You cannot directly mount a directory to the Windows docker container; you must first mount the directory in your Windows VM created by Vagrant and then use the path in the Windows VM as the host path in your docker run command.

For example, say you mounted a local directory to the E: drive in the Windows VM you created with Vagrant. You can mount that drive to C:\edrive in your container by running:

docker run -it --rm -v "e:\\:c:\\edrive" windowspython
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment