【WSL】Ubuntu 22.04 安装配置docker

前言

WSL就是个坑!
WSL就是个坑!
WSL就是个坑!

我第一次安装使用 Ubuntu 还是第一台笔记本,装了双系统,版本是18.04 LTS,但是我那个时候只有机械硬盘,因此 Ubuntu 桌面十分的卡。

兜兜转转,大三的时候发现微软的 WSL2 很好用,想装一个学习 Linux,当然选择了那个时候最新的 Ubuntu 22.04 LTS。最近工作需要,学习一些 docker 知识。于是打算在WSL上安装docker,期间踩了一些坑,在这里记录下吧。

安装

使用 Docker 仓库进行安装

更新 apt 包索引

$ sudo apt-get update

安装 apt 依赖包,用于通过HTTPS来获取仓库

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

添加 Docker 的官方 GPG 密钥

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

使用以下指令设置稳定版仓库

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

安装后,输出如图:

设置稳定版仓库

安装 Docker Engine-Community

我这里直接安装最新版本的 Docker Engine-Community 和 containerd,

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

输出如图:

扫描二维码关注公众号,回复: 15101572 查看本文章

安装最新版本的 Docker Engine-Community 和 containerd
到这里基本就安装完了,试试输入以下指令运行你的第一个容器:

sudo 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/

问题

docker指令必须用sudo执行

默认情况下, Unix 套接字 (Unix socket)由用户 root 拥有,其他用户只能使用 sudo 访问它。 Docker 守护进程始终以 root 用户身份运行。

为了避免每次都加sudo,需要给现在的账户添加docker权限

创建docker用户组

$ sudo groupadd docker

添加当前用户到docker用户组

$ sudo usermod -aG docker $USER

退出当前shell,重新登录

docker开机需要手动启动,无法创建自启动

这个问题我还没解决,我猜测是WSL缺少某些组件。我在这里记录一下解决这个问题的步骤,以后参考。

这是我的wsl版本:

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

添加自启动需要使用systemctl,最新版的WSL默认是不使用systemd的,可以通过下面的步骤配置使用

在WSL中打开终端,使用sudo vim /etc/wsl.conf编辑(或使用其他熟悉的文本编辑命令)并添加如下内容:

[boot]
systemd=true

必要时还要安装systemctl

不过就算把docker.service加入开机自启,也没法真正做到让docker自启动。我有时间要研究下systemmd的使用。

就目前来说,启动 Ubuntu 后,需要输入以下命令启动 docker 服务:

sudo service docker start

后续执行docker命令,无需再调用sudo。

猜你喜欢

转载自blog.csdn.net/qq_37387199/article/details/129100486