You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSL与bash环境下执行Docker build命令报“unable to prepare context”错误

Fixing Docker Build Path Errors in WSL Bash

Hey there, let's break down why your docker.exe build commands are failing and how to fix them!

The Root Cause

When you run docker.exe in WSL bash, you're actually invoking the Windows version of Docker. Windows Docker doesn't understand WSL's Linux-style paths like /tmp/docker-nginx-test—it only recognizes Windows filesystem paths (like C:\Users\... or the WSL mount path \\wsl$\...). Also, your second command has a usage mistake: docker build expects a context directory as input, not a direct path to the Dockerfile.

Solutions to Try

1. Use WSL's Native Docker (Recommended)

If you've installed Docker Engine directly in your WSL distro, skip the .exe suffix and run the standard docker command. This will natively recognize WSL's file system paths:

  • Build from the absolute path:
    docker build /tmp/docker-nginx-test
    
  • Or build from your current directory (since the Dockerfile is already here):
    docker build .
    
  • If you need to specify a custom Dockerfile name/location, use the -f flag:
    docker build -f ./MyCustomDockerfile .
    

2. Convert WSL Paths for Windows Docker

If you must use the Windows docker.exe (e.g., you only have Docker Desktop installed), convert the WSL path to a Windows-compatible path using the wslpath -w command:

  • Build from the absolute path:
    docker.exe build $(wslpath -w /tmp/docker-nginx-test)
    
  • Build from your current directory:
    docker.exe build $(wslpath -w .)
    
  • To specify a Dockerfile with Windows Docker:
    docker.exe build -f $(wslpath -w ./Dockerfile) $(wslpath -w .)
    

3. Enable WSL Integration in Docker Desktop

If you're using Docker Desktop, make sure WSL integration is turned on for your distro:

  • Open Docker Desktop settings → Resources → WSL Integration
  • Toggle on the switch for your WSL distro
  • This lets Windows Docker access WSL files, but you still need to use converted paths as shown above.

内容的提问来源于stack exchange,提问作者geoB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:37:39