Dmitrii
Dmitrii

Reputation: 3557

WORKDIR doesn't work when running docker-compose run on Windows

It looks like the WORKDIR inside my Dockerfile doesn't work. I'm trying this on Windows 10. The same works perfectly on Mac.

Here are my project files: 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

The tests/DockerImages/testRunner folder has only this 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

If I run docker-compose -f docker-compose.yml run test_runner /usr/local/bin/php tests/Tools/generate-stub-map in cmd.exe in the same directory where docker-compose.yml lies, I would get:

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

However, if I run the same using -w: docker-compose -f docker-compose.yml run -w /opt/project/phpstorm-stubs test_runner /usr/local/bin/php tests/Tools/generate-stub-map - that would work. Output:

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...

I can see here that -w is outdated and I should use --project-directory instead, but doesn't work also:

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.

I've tried to look for something related at docker/compose github issues but found none.

docker version output:

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

Any help with this is much appreciated.

Upvotes: 6

Views: 2286

Answers (3)

Vanshaj Daga
Vanshaj Daga

Reputation: 2165

You have to understand the different path of your whole project.

Location of your docker file C:\Projects\phpstorm-stubs\

Location of base of your project /opt/project/phpstorm-stubs so the exact whole path to your project base in reality is C:\Projects\phpstorm-stubs\opt\project\phpstorm-stubs and so the files it is trying to find is actually at C:\Projects\phpstorm-stubs\opt\project\phpstorm-stubs\usr\local\bin\php tests\Tools\generate-stub-map

So once you used the -f docker-compose.yml it by default thinks that your whole project is at C:\Projects\phpstorm-stubs\ but when you use it with -w /opt/project/phpstorm-stubs it knows that base of your project path is here so it knows that tests/Tools/generate-stub-map is further ahead of it.

And lastly --project-directory requires the whole path from the base of current drive

To sum up my answer you need to use command

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

Upvotes: 0

rzlvmp
rzlvmp

Reputation: 9394

Sounds like you built test_runner image once using Dockerfile of previous version without WORKDIR parameter and trying to re-run container using this outdated image.

You have to remove image manually or force rebuild it using docker-compose build (or docker-compose up --build) instead of docker-compose run

Also this link may be helpfull

Upvotes: 2

Slava Kuravsky
Slava Kuravsky

Reputation: 2814

WORKDIR from dockerfile corresponds to working_dir in compose file https://docs.docker.com/compose/compose-file/#working_dir

Try to use

  test_runner:
    build: tests/DockerImages/testRunner
    working_dir: /opt/project/phpstorm-stubs
    volumes:
      - .:/opt/project/phpstorm-stubs

Upvotes: 1

Related Questions