WSl 安装 docker 处理 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running

解决问题命令 本机测试有效

sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
sudo service docker restart

或者

运行 sudo update-alternatives --config iptables
输入1 选择 iptables-legacy
然后运行 sudo service docker start 试试效果

WSL 2 安装 docker

环境:

  • Windows 11 23H2
  • WSL 2
  • Ubuntu-22.04

安装 Docker

  1. 检查卸载老版本Docker

    sudo apt-get remove docker docker-engine docker.io containerd runc
    
  2. 更新软件包

    sudo apt-get update
    sudo apt-get upgrade
    
  3. 安装docker依赖

    sudo apt-get install ca-certificates curl gnupg lsb-release
    
  4. 添加docker密钥

    curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
    
  5. 添加阿里云docker软件源

    sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
    
  6. 安装docker

    apt-get install docker-ce docker-ce-cli containerd.i
    
  7. 配置用户组(非必须操作,此操作目的是为了以后执行docker命令时无需输入sudo密码,避免这些重复操作而已。)

    sudo usermod -aG docker $USER
    # sudo reboot
    
  8. 执行完第七步命令的话你电脑会立刻黑屏进行重启,等待重启即可。 检验docker是否安装成功

    service docker start
    sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
    service docker restart
    sudo docker pull hello-world
    sudo docker run hello-world
    
  9. docker 镜像源配置,修改或创建/etc/docker/daemon.json文件(docker pull hello-world 获取镜像失败的搞一下这个)

    sudo vim /etc/docker/daemon.json

    {
        "registry-mirrors":[
            "https://docker.13140521.xyz/",
            "https://ccr.ccs.tencentyun.com/"
        ]
    }
    

成功内容:

root@SPC:/etc/docker$ 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 docker version

#查看docker镜像 
sudo docker images

问题

输入docker container ls报异常Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running

解决问题:

sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
sudo service docker restart

以上命令解释:

sudo update-alternatives --set iptables /usr/sbin/iptables-legacy

这条命令设置 iptables 的替代版本为 /usr/sbin/iptables-legacy。在某些情况下,可能需要使用旧版本的 iptables 而非默认的版本。

sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

这条命令设置 ip6tables 的替代版本为 /usr/sbin/ip6tables-legacy。同样,有时候需要使用旧版本的 ip6tables。

如何查看iptables现在是那个版本呢?使用命令iptables --version

异常的版本iptables v1.8.7 (nf_tables);正常的版本iptables v1.8.7 (legacy)

docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

参考

[対処法 WSL2] Docker エラー:Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running
Ubuntu 22.04安装Docker(最全教程)

posted @ 2024-07-16 14:56  梨花大将  阅读(100)  评论(0编辑  收藏  举报