[WSL] Ubuntu 22.04 installation and configuration docker

foreword

WSL is a pit!
WSL is a pit!
WSL is a pit!

The first time I installed and used Ubuntu was my first laptop, with dual systems installed, the version was 18.04 LTS, but I only had a mechanical hard disk at that time, so the Ubuntu desktop was very stuck.

Going around, I found that Microsoft's WSL2 was very useful when I was a junior. I wanted to install one to learn Linux. Of course, I chose the latest Ubuntu 22.04 LTS at that time. Recently, work needs to learn some docker knowledge. So I planned to install docker on WSL, and I stepped on some pits during the period, so let's record it here.

Install

Install using Docker repository

Update the apt package index

$ sudo apt-get update

Install the apt dependency package to get the warehouse via HTTPS

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

Add Docker's official GPG key

$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

Set up the stable repository with the following command

$ sudo add-apt-repository \
   "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
  $(lsb_release -cs) \
  stable"

After installation, the output is as shown in the figure:

Set up a stable repository

Install Docker Engine-Community

I directly install the latest version of Docker Engine-Community and containerd here,

$ sudo apt-get install docker-ce docker-ce-cli containerd.io

The output is as shown in the figure:

Install the latest version of Docker Engine-Community and containerd
The installation is basically finished here, try to run your first container by typing the following command:

sudo docker run hello-world

The output of the operation is as follows:

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/

question

The docker command must be executed with sudo

By default, a Unix socket is owned by user root and other users can only access it using sudo. The Docker daemon always runs as the root user.

In order to avoid adding sudo every time, you need to add docker permissions to the current account

Create a docker user group

$ sudo groupadd docker

Add the current user to the docker user group

$ sudo usermod -aG docker $USER

Exit the current shell and log in again

Docker boot needs to be started manually, unable to create self-start

I haven't solved this problem yet, my guess is that some components are missing in WSL. I will record the steps to solve this problem here for future reference.

Here is my wsl version:

WSL 版本: 1.0.3.0
内核版本: 5.15.79.1
WSLg 版本: 1.0.47
MSRDC 版本: 1.2.3575
Direct3D 版本: 1.606.4
DXCore 版本: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows版本: 10.0.19045.2604

Adding self-starting needs to be used systemctl. The latest version of WSL does not use systemd by default. It can be configured and used by the following steps

Open a terminal in WSL, use sudo vim /etc/wsl.confEdit (or use another familiar text editing command) and add something like this:

[boot]
systemd=true

Also install systemctl if necessary

However, even if docker.service is added to start automatically at boot, there is no way to really make docker start automatically. I have time to study the use of systemmd.

For now, after starting Ubuntu, you need to enter the following command to start the docker service:

sudo service docker start

Subsequent execution of the docker command does not require calling sudo.

Guess you like

Origin blog.csdn.net/qq_37387199/article/details/129100486