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

Windows环境下Dockerfile中WORKDIR在docker-compose run时失效求助

Dockerfile WORKDIR 配置在Windows 10失效,Mac下正常运行的问题

问题概述

Dockerfile中定义的WORKDIR /opt/project/phpstorm-stubs在Mac环境下可正常生效,但在Windows 10中执行docker-compose run命令时,容器默认工作目录并未切换到该路径,导致脚本文件找不到。

项目文件内容

docker-compose.yml

version: '3'
services:
  php_under_test:
    build: tests/DockerImages/${PHP_VERSION}
    volumes:
      - .:/opt/project/phpstorm-stubs
  test_runner:
    build: tests/DockerImages/testRunner
    volumes:
      - .:/opt/project/phpstorm-stubs

tests/DockerImages/testRunner/Dockerfile

FROM php:8.1-apache

RUN echo 'memory_limit = 1024M' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini
COPY --from=composer /usr/bin/composer /usr/bin/composer

RUN apt-get update && apt-get -y install git zip unzip

WORKDIR /opt/project/phpstorm-stubs

复现步骤

在docker-compose.yml所在目录的cmd.exe中执行:

docker-compose -f docker-compose.yml run test_runner /usr/local/bin/php tests/Tools/generate-stub-map

报错信息:

C:\Projects\phpstorm-stubs>docker-compose -f docker-compose.yml run test_runner /usr/local/bin/php tests/Tools/generate-stub-map
WARNING: The PHP_VERSION variable is not set. Defaulting to a blank string.
Creating phpstorm-stubs_test_runner_run ... done
Could not open input file: tests/Tools/generate-stub-map
ERROR: 1

临时有效方法(带疑问)

添加-w参数指定工作目录后可正常运行:

docker-compose -f docker-compose.yml run -w /opt/project/phpstorm-stubs test_runner /usr/local/bin/php tests/Tools/generate-stub-map

运行输出:

C:\Projects\phpstorm-stubs>docker-compose -f docker-compose.yml run -w /opt/project/phpstorm-stubs test_runner /usr/local/bin/php tests/Tools/generate-stub-map
WARNING: The PHP_VERSION variable is not set. Defaulting to a blank string.
Creating phpstorm-stubs_test_runner_run ... done
Parsing "/opt/project/phpstorm-stubs/tests/Tools/../../aerospike/aerospike.php"
Parsing "/opt/project/phpstorm-stubs/tests/Tools/../../aerospike/Bytes.php"
etc...

尝试过的无效方法

根据文档提示-w已过时,尝试用--project-directory替代,执行命令:

docker-compose -f docker-compose.yml --project-directory /opt/project/phpstorm-stubs run test_runner /usr/local/bin/php tests/Tools/generate-stub-map

报错信息:

C:\Projects\phpstorm-stubs>docker-compose -f docker-compose.yml --project-directory /opt/project/phpstorm-stubs run test_runner /usr/local/bin/php tests/Tools/generate-stub-map
WARNING: The PHP_VERSION variable is not set. Defaulting to a blank string.
ERROR: build path C:\opt\project\phpstorm-stubs\tests\DockerImages either does not exist, is not accessible, or is not a valid URL.

Docker版本信息

Client:
 Cloud integration: v1.0.24
 Version:           20.10.17
 API version:       1.41
 Go version:        go1.17.11
 Git commit:        100c701
 Built:             Mon Jun  6 23:09:02 2022
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.10.1 (82475)
 Engine:
  Version:          20.10.17
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.11
  Git commit:       a89b842
  Built:            Mon Jun  6 23:01:23 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.6
  GitCommit:        10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
 runc:
  Version:          1.1.2
  GitCommit:        v1.1.2-0-ga916309
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

解决方案

方案1:在docker-compose.yml中显式指定working_dir

修改test_runner服务配置,添加working_dir字段强制指定容器工作目录,覆盖Dockerfile的配置(或解决Windows下的生效问题):

version: '3'
services:
  php_under_test:
    build: tests/DockerImages/${PHP_VERSION}
    volumes:
      - .:/opt/project/phpstorm-stubs
  test_runner:
    build: tests/DockerImages/testRunner
    volumes:
      - .:/opt/project/phpstorm-stubs
    working_dir: /opt/project/phpstorm-stubs  # 新增该行

修改后直接执行原命令即可正常运行:

docker-compose -f docker-compose.yml run test_runner /usr/local/bin/php tests/Tools/generate-stub-map

方案2:检查Docker Desktop文件共享权限

Windows下Docker Desktop可能因磁盘共享权限问题导致路径映射异常,按以下步骤检查:

  1. 打开Docker Desktop设置面板
  2. 进入Resources > File Sharing
  3. 勾选项目所在的磁盘(如C:)
  4. 点击Apply & Restart重启Docker服务

方案3:使用脚本绝对路径执行

如果不想修改配置文件,可直接指定脚本的绝对路径运行:

docker-compose -f docker-compose.yml run test_runner /usr/local/bin/php /opt/project/phpstorm-stubs/tests/Tools/generate-stub-map

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 09:54:22