docker-compose构建无法在WSL2中运行

时间:2019-11-28 15:33:21

标签: docker docker-compose ubuntu-18.04 windows-subsystem-for-linux

我已经在Windows上使用WSL2设置了Ubuntu 18.04,并完成了docker-ce和docker-compose的所有安装。我验证了我的安装。

$ sudo service docker start

以上命令成功启动了docker守护进程,但是当我尝试通过以下命令构建docker镜像时,

{path that contains yml files}$ docker-compose build

失败,并显示以下错误。

  

服务'comp_app'生成失败:获取https://registry-1.docker.io/v2/:拨打tcp:在172.25.240.1:53上查找注册表-1.docker.io:读取udp 172.25.254.229:49124->172.25.240.1:53 :I / O超时

我尝试杀死所有停下的容器,晃晃图像。甚至也重新启动了我的WSL。似乎没有一个工作。

1 个答案:

答案 0 :(得分:0)

在WSL2下,您必须使用Windows上运行的docker-desktop,因为WSL2不支持真正的初始化系统。 为此:

  1. 为Windows安装Docker桌面
  2. 调整Windows的docker设置以启用端口2375上的不安全访问
  3. 在WSL2上安装docker客户端(请参阅下面的Ubuntu 18.04外壳程序功能)
  4. 在.bashrc或.zshrc文件中添加“ export DOCKER_HOST = tcp:// localhost:2375”
  5. 重新启动外壳以获取新的环境变量
function install_docker_on_linux_or_wsl2 () {
    sudo apt update
    sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
    sudo apt update
    apt-cache policy docker-ce
    # Note: on WSL2, this will only install the docker client, as there is no init system
    # you need export DOCKER_HOST=tcp://localhost:2375 in your .bashrc or .zshrc file when running on WSL2
    # docker desktop must be running, and the option to run unsecure on port 2375 (docker options) must be selected
    sudo apt install -y docker-ce   
}

之后,执行所有docker操作,例如码头工人图像应该工作。