- Docker for Desktop can run on WSL 2 without any Virtual machine!
- WSL 2 is now available for stable Windows 10 (not just Insiders) via the (stable) Windows 10 May 2020 Update.
- WSL 2 is available for Windows Home editions, too, you don't need a Pro license!
- Docker for Desktop with WSL 2 eliminates many issues inherent to running a Linux VM and using shares into that VM.
- WSL 2 supports *nix permissions on Windows file systems mounted into WSL 2 (Postgres container mounts come into mind).
- WSL 2 also supports full GPU access – CUDA and such, too, since the 2021H2 November update even in non-insiders, stable!!
- VirtualBox (in recent version) can now coexist with Hyper-V!
Therefore you may want to skip this guide and rather install WSL 2 and use Docker for Desktop with it!
for using Docker with WSL (without Hypervisor and Hyper-V VM instead): Docker on WSL (Windows 10 Home / Docker Toolbox) (Virtualbox instead Hyper-V)
Docker on WSL communicates with Docker on Windows from Docker Toolbox.
Install VirtualBox and Docker Toolbox on Windows.
Docker on Windows uses VM for Linux based docker containers. Create new docker machine (VM):
> docker-machine.exe create default
Start the default docker machine:
> docker-machine.exe start default
Find out (Docker daemon) IP of default docker-machine and use that IP for Docker client:
> docker-machine.exe ip
In case of error:
open C:\Users\<user>\.docker\machine\machines\default\config.json: The system cannot find the file specified.
Remove folder in C:\Users<user>.docker\machine\machines manually.
Then try again:
> docker-machine.exe rm default
[y]
> docker-machine.exe create default
After setting up environment variables for Docker client:
> docker info
In case of error:
could not read CA certificate "/home/build/.docker/ca.pem": open /home/build/.docker/ca.pem: no such file or directory
Remove folder in C:\Users<user>.docker\machine\machines manually.
Then try again:
> docker-machine.exe rm default
[y]
> docker-machine.exe create default
Extra fix if necessary:
> docker-machine.exe regenerate-certs default
[y]
Note: In Windows, docker client has to run as admin, otherwise this error will occur:
open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows
If already running as admin, try this fix
> docker-machine env
Copy + paste/run the printed for loop (without commenting out REM) for configuring.
Important: The @FOR
loop without the REM
!
C:\Users\<user>>docker-machine env
SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.99.100:2376
SET DOCKER_CERT_PATH=C:\Users\<user>\.docker\machine\machines\default
SET DOCKER_MACHINE_NAME=default
SET COMPOSE_CONVERT_WINDOWS_PATHS=true
REM Run this command to configure your shell:
REM @FOR /f "tokens=*" %i IN ('docker-machine env') DO @%i
From that output, copy + paste and execute the whole thing in cmd.
Then copy the @FOR
line without REM
and also paste and execute in cmd:
> @FOR /f "tokens=*" %i IN ('docker-machine env') DO @%i
> docker-machine ls
> docker-machine start default
Network/other issues with virtual machine Open VirtualBox GUI and stop + remove the default machine manually, then try again. Extra tip: Update VirtualBox + VirtualBox Extensions to latest version.
Test:
> docker run hello-world
This should download + run the hello-world docker container.
On WSL, install Docker CE(for Ubuntu (=WSL basically)).
docker info
will now show an error (can't connect to daemon).
Edit:
docker-env wrapper script that translates Windows style ENV to *nix (WSL) style export
:
https://gist.github.com/mmarchini/bc9df7b82127fea13612edf8bffdf96f
$ mkdir -p ~/.bin/
$ cd ~/.bin/
$ wget -O docker-env https://gist.githubusercontent.com/mmarchini/bc9df7b82127fea13612edf8bffdf96f/raw/33f14362f98fa168c17ad6b04a053c2066e071f0/docker-env
$ chmod +x ./docker-env
Test the script:
$ ./docker-env
.bashrc on WSL:
# Docker (Docker Toolbox on Windows)
source <(~/.bin/docker-env)
alias docker-machine="docker-machine.exe"
#alias docker="docker.exe" # Native docker client is used instead, less issues with environment variables
docker-machine.exe start default && # autostart docker-machine
# docker-compose path compatibility
export COMPOSE_CONVERT_WINDOWS_PATHS=1
Tip: Load the new bash script in existing terminal using source ~/.bashrc
to avoid restarting the terminal.
Share certs from Docker Toolbox on Windows with Docker client on WSL:
$ mkdir -p ~/.docker
$ ln -s /mnt/c/Users/<user>/.docker/machine/certs/ca.pem ~/.docker/ca.pem
$ ln -s /mnt/c/Users/<user>/.docker/machine/certs/ca-key.pem ~/.docker/ca-key.pem
$ ln -s /mnt/c/Users/<user>/.docker/machine/certs/cert.pem ~/.docker/cert.pem
$ ln -s /mnt/c/Users/<user>/.docker/machine/certs/key.pem ~/.docker/key.pem
Note: All of these files have to be made available/symlinked!
Install docker-compose(for Linux) natively on WSL.
* There is also docker-compose shipped with Docker Toolbox (alias docker-compose to docker-compose.exe) – but it may be older and not the same style as the natively installed Docker client.
Paths in docker-compose in Bash on Windows/WSL:
ERROR: for <container> Cannot start service php: oci runtime error: container_linux.go:262: starting container process caused "chdir to cwd (\"<target container path>\") set in config.json failed: no such file or directory"
Proposed solutions:
.env file with this content:
COMPOSE_CONVERT_WINDOWS_PATHS=1
export as environment variable:
export COMPOSE_CONVERT_WINDOWS_PATHS=1
or in .bashrc:
COMPOSE_CONVERT_WINDOWS_PATHS=1
But this doesn't resolve the issue for me... Edit: Apparently it works now, I installed the latest Windows 10 updates (and got a newer WSL version).
I found a different approach (from different user) that looks simpler, maybe you want to take a look:
https://gist.github.com/jwilson8767/00a46f5ca63327d5bfd802f87b702c8d