Note: installing WSL2 requires an operating system: at least Windows 10 1903 (Build 18362).
Official installation method
If you just want to install docker on WSL and use it on windows, it is recommended to refer to the official Microsoft document to install Docker Desktop.
address https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-containers
Install Docker in Terminal of WSL
It's a tossing installation method. It's recommended for players who don't want to install a docker desktop application.
Install WSL
If you haven't installed it, you can install it according to the official tutorial.
address https://docs.microsoft.com/en-us/windows/wsl/install
Switch from WSL1 to WSL2
I installed distro = Ubuntu-20.04.
Because WSL has been installed in advance, but according to the installation requirements of docker, WSL version 2 is required.
PS C:\Users\overlord> wsl -l -v NAME STATE VERSION * Ubuntu-20.04 Stopped 1
Check with the command wsl -l -v and find that it is 1.
Before calling WSL -- Set version ubuntu-20.04 2, you must install linux-kernel update package
Windows features must be enabled:
I lost the Audio driver of HDMI after enabling the Hyper-V virtual machine monitor. Just turn it off and then enable it.
Press the shortcut key win+r to enter msinfo32 Exe to check whether it is enabled successfully:
Then enter the command WSL -- Set version ubuntu-20.04 2 in powershell. Wait for the conversion to complete.
PS C:\Users\overlord> wsl --set-version Ubuntu-20.04 2 Conversion in progress, this may take several minutes... Related to WSL 2 For information on the main differences, please visit https://aka.ms/wsl2 Conversion complete. PS C:\Users\overlord> wsl -l -v NAME STATE VERSION * Ubuntu-20.04 Stopped 2
Use the wsl -l -v command to check whether the wsl version has been converted.
Install settings and run Docker
Open the Terminal command line interface of WSL and install docker.
~ sudo apt update ~ sudo apt install docker.io -y
Use the docker --version command to check whether the installation is successful.
~ docker --version Docker version 20.10.7, build 20.10.7-0ubuntu5~20.04.2
However, the docker container cannot be actually run at this time, because the daemon service that docker depends on is not running in the background.
First, add sudo permission to the daemon service initiator dockerd.
~ sudo visudo
After sudo visudo, you will enter the interface of nano text editor. The edited file is / etc/sudoers. Add content at the end of another line:
# Docker daemon specification $USER ALL=(ALL) NOPASSWD: /usr/bin/dockerd
$USER replace with the USER name you signed in to WSL.
Add a startup item to automatically start the dockerd program for each login to the WSL subsystem.
According to the WSL terminal you use, this startup file will be added in ~ / bashrc or ~ / In zshrc, which terminal is logged in is added to the startup item file of which terminal.
# Start Docker daemon automatically when logging in if not running. RUNNING=`ps aux | grep dockerd | grep -v grep` if [ -z "$RUNNING" ]; then sudo dockerd > /dev/null 2>&1 & disown fi
Add the current user group to the docker group.
~ sudo usermod -aG docker $USER
After the configuration is completed, restart the Terminal of WSL and run the docker run Hello World test.
~ docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
If this information is displayed, the configuration is successful.
Install and run docker compose
Install V2 directly and use docker compose without short horizontal lines and spaces.
~ mkdir -p ~/.docker/cli-plugins/ ~ curl -SL https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
Try using an agent when github cannot be downloaded.
After downloading, give docker compose V2 running permission.
~ chmod +x ~/.docker/cli-plugins/docker-compose
Check whether it works normally
~ docker compose version Docker Compose version v2.2.3
Create and edit docker compose YML configuration file.
~ nano docker-compose.yml
Fill in the following contents in the editor and exit saving.
version: "3.9" services: hello: image: "hello-world"
Try running docker compose.
~ docker compose run hello Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
If shown above, docker compose is installed successfully.
reference material
https://medium.com/geekculture/run-docker-in-windows-10-11-wsl-without-docker-desktop-a2a7eb90556d
https://docs.docker.com/compose/cli-command/
https://docs.microsoft.com/en-us/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package
https://dev.to/bowmanjd/install-docker-on-windows-wsl-without-docker-desktop-34m9