« Back to home

Docker for Windows - switch between Linux and Windows containers from the command line

Posted on

If you use Docker for Windows on Win10 and use both Linux and Windows containers you've no doubt have had to switch the docker tools between Linux and Windows containers more than a few times.
To do this you have to bring up the tray icon for Docker Tools select the option to switch flavours.
Docker tray icon

Given that pretty much everything else for Docker is achieved via the command line it feels like quite a jolt to have to leave the command line to switch container flavours, often you'll forget and get an error when trying to build or pull.

Thankfully its possible to switch the docker tooling between Windows and Linux from the command line, however the syntax is not the most obvious or discoverable.
From the command line you can type
"%programfiles%\docker\docker\dockercli" -SwitchDaemon

Note: the command line parameter "SwitchDaemon" is case sensitive, if you don't supply it in the correct case you don't get any error and the daemon is not switched.

One thing that strikes you about the command line switch is that it simply flips between Windows and Linux containers, it doesn't let you specify a particular flavour which is not great.

I knocked up this PowerShell script that you can run passing in the parameter for Windows or Linux and it will switch the docker daemon if its not currently set to the flavour you specify.

param (  
    [Parameter(Mandatory=$true)][ValidateSet("windows","linux")] [string]$targetDaemon )

    $daemon = "*"+$targetDaemon.ToLower()+"*";

    #We want the server portion of the OS/Arch string
    $dockerver = Docker version | Select -Last 2
    #$dockerver = Docker version
    if ($dockerver -like $daemon)
        {
            echo "Daemon already set to:"$targetDaemon    
        }
    else
        {
        echo "Switching daemon...";
        & "c:\program files\docker\docker\dockercli" -SwitchDaemon
        echo "Daemon switched!"
        }

Gist

Disclaimer: As you can see I'm no PowerShell expert (to put it mildly!) but it Works on my Machineā„¢ ;) Undoubtedly can be improved so please let me know a better way of doing this.

It would be great if this was built into the tooling though.

Comments

comments powered by Disqus