WSL中使用docker的一些总结

WSL上安装docker

  • 执行 apt-get update 出错

    root@WSL:/# apt-get update
    The following signatures couldn't be verified because the public key is not available: NO_PUBKEY ABF5BD827BD9BF62
    

    解决方法

    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABF5BD827BD9BF62
    
  • WSL中按照官方文档(Ref-1)安装完不能使用,一番折腾,还是无法使用。

    root@WSL:/# docker run hello-world
    docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
    
    //step-1: docker确实已安装
    root@WSL:~# docker info
    Client:
     Debug Mode: false
    
    Server:
    ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
    errors pretty printing info
    
    //step-2: 启动docker服务
    root@WSL:/# service docker start
     * Starting Docker: docker
    
    //step-3: 安装daemonize(守护进程运行命令, 使docker可以后台运行)
    root@WSL:/# apt-get install daemonize
    
    //step-4:再次启动docker服务
    root@WSL:/# service docker start
     * Starting Docker: docker                                       [ OK ]
    
    //step-5
    root@WSL:/# docker run hello-world
    docker: Cannot connect to the Docker daemon at tcp://0.0.0.0:2375. Is the docker daemon running?
    

    一年前在WSL-1上自学docker,按照一篇Docker使用笔记上使用低版本的方法跑了起来。不过正如那篇博客中所说,使用过程中真的各种折腾,一边学docker,一边还要和WSL-1中各类奇葩问题作斗争。这次不想再折腾了,直接使用WSL-2,正如官方文档(Ref-2)所言,Docker 引擎无法直接在 WSL-1 上运行, 通过各种变通措施强行运行,确实有点给自己找不痛快。

    在 WSL 版本1中,由于 Windows 和 Linux 之间的基本差异,Docker 引擎无法直接在 WSL 内运行,因此 Docker 团队开发了使用 Hyper-v Vm 和 LinuxKit 的替代解决方案。 但是,因为 WSL 2 现在运行在具有完全系统调用容量的 Linux 内核上,所以 Docker 可以在 WSL 2 中完全运行。

    若是第一次安装,请按照官方文档(Ref-3Ref-4)安装WSL, 并设置版本到WSL-2(可双向切换)。

    C:\>WSL --list --verbose
      NAME            STATE           VERSION
    * Ubuntu-20.04    Stopped         1
    
    C:\>WSL --set-version Ubuntu-20.04 2
    正在进行转换,这可能需要几分钟时间...
    有关与 WSL 2 的主要区别的信息,请访问 https://aka.ms/wsl2
    转换完成。
    
    C:\>
    

    再次安装docker并运行,直接成功,顺利的不敢相信,WSL-2太棒了!

    root@WSL:~# service docker start
     * Starting Docker: docker                                          [ OK ]
    root@WSL:~# 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/
    
    root@WSL:~#
    

docker的使用总结

  • 查看docker日志

    cat /var/log/docker.log
    
  • docker psdocker container ls 的快捷方式,可以通过加入 -a 标记,输出中列出已停止的容器

    docker ps -a
    
  • 不能删除正在运行的容器,但可以通过将 -f 标记用于 docker rm 命令来强制停止和删除容器。 这是停止和删除容器的快速方法,但仅当容器中的应用不需要执行正常关闭时才应使用

    docker container rm -f contaner_name
    
  • Docker Daemon(Ref-5Ref-6)

posted @ 2020-11-28 23:36  天琊蓝  阅读(1127)  评论(0编辑  收藏  举报