在WSL里安装docker



  • 在WSL里安装docker

    # Update the apt package list.
    sudo apt-get update -y
    
    # Install Docker's package dependencies.
    sudo apt-get install -y \
        apt-transport-https \
        ca-certificates \
        curl \
        software-properties-common
    
    # Download and add Docker's official public PGP key.
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
    # Verify the fingerprint.
    sudo apt-key fingerprint 0EBFCD88
    
    # Add the `stable` channel's Docker upstream repository.
    #
    # If you want to live on the edge, you can change "stable" below to "test" or
    # "nightly". I highly recommend sticking with stable!
    sudo add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) \
       stable"
    
    # Update the apt package list (for the new apt repo).
    sudo apt-get update -y
    
    # Install the latest version of Docker CE.
    sudo apt-get install -y docker-ce
    
    # Allow your user to access the Docker CLI without needing root access.
    sudo usermod -aG docker $USER
    

    在安装完成后关闭bash,用管理员权限启动bash,继续执行

    sudo service docker start
    

    会出现

    $ sudo service docker restart
     * Starting Docker: docker                                                                                       [ OK ]
    

    执行docker version(可能需要sudo),能看到信息就OK了,比如我这里:

    ~$ docker version
    Client: Docker Engine - Community
     Version:           19.03.8
     API version:       1.40
     Go version:        go1.12.17
     Git commit:        afacb8b7f0
     Built:             Wed Mar 11 01:25:46 2020
     OS/Arch:           linux/amd64
     Experimental:      false
    
    Server: Docker Engine - Community
     Engine:
      Version:          19.03.8
      API version:      1.40 (minimum version 1.12)
      Go version:       go1.12.17
      Git commit:       afacb8b7f0
      Built:            Wed Mar 11 01:24:19 2020
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.2.13
      GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
     runc:
      Version:          1.0.0-rc10
      GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
     docker-init:
      Version:          0.18.0
      GitCommit:        fec3683
    

    如果没有用管理员权限启动bash,会提示:

    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
    

    这时可以尝试查看docker服务状态

    $ sudo service docker status
     * Docker is not running
    

    切记用管理员身份启动bash,启动成功后应该是这样

    $ sudo service docker status
     * Docker is running
    

    参考资料:
    [1] https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly