Windows 10 Docker : /usr/bin/env: 'php\r': No such file or directory
Developers often assume that docker-compose provides a seamless abstraction layer, allowing them to deploy identical environments across Linux, macOS, and Windows without modification. In reality, the underlying host operating system can inject subtle incompatibilities into the build process or runtime environment if not handled correctly. A classic example of this friction occurs when moving a Symfony application from Ubuntu to Windows 10, resulting in the error: /usr/bin/env: 'php\r': No such file or directory.
This error message indicates that the shell is attempting to execute php, but the command it received includes a carriage return character (\r). In Unix-like environments used by Docker containers, line endings are strictly defined as \n (LF). Windows, however, uses \r\n (CRLF) for line endings. When files created or edited on Windows retain these CRLF endings and are copied into the container without conversion, scripts that parse commands using source or env fail to recognize the file path correctly. The trailing \r is interpreted by the shell as part of the filename, causing the system to look for a binary named php\r instead of php.
Diagnosing the Root Cause
The issue typically stems from how volumes are mounted or how files are prepared inside the container before execution. In the provided docker-compose.yml, the engine service mounts the local source code into /home/docker and attempts to run console commands via bin/console.
engine:
build: ./docker/engine/
volumes:
- ".:/home/docker:rw"
- "./docker/engine/php.ini:/usr/local/etc/php/conf.d/custom.ini:ro"
links:
- "db:db"
working_dir: "/home/docker"
If the project files reside directly on a Windows filesystem, the Composer autoloader or shell scripts generated during the bin/console execution may inherit CRLF line endings. When the Dockerfile executes commands like composer install or when the entrypoint script invokes PHP, the shell interprets the path php\r. The containerized Linux environment cannot find an executable named php\r, triggering the "No such file or directory" exception.
Resolving Line Ending Incompatibility
The most direct solution is to ensure that all files entering the Docker build context and runtime volumes use Unix-style line endings. You can achieve this by configuring your local editor to convert CRLF to LF before committing code, but for existing containers running on Windows, a more robust approach involves converting the files inside the container at startup or utilizing the winpty tool if necessary.
However, the most effective fix is often handled within the Dockerfile itself or via a post-build script. If you control the build context, ensure your local repository uses LF line endings. If you are working with an existing setup on Windows where you cannot change the host files immediately, you can add a conversion step in the Dockerfile using dos2unix:
# Example Dockerfile adjustment for engine service
RUN apt-get update && apt-get install -y dos2unix
# ... other build steps ...
COPY --chown=www-data:www-data . /home/docker/
# Convert all files in the working directory to Unix line endings
RUN find /home/docker -type f ! -path '*/vendor/*' -exec dos2unix {} +
Alternatively, you can mount a docker-compose.yml file specifically configured for Windows that includes a command to convert line endings upon entry:
engine:
# ... existing config ...
command: sh -c "find /home/docker -type f ! -path '*/vendor/*' -exec dos2unix {} + && exec bin/console doctrine:schema:create"
Alternative Workflows for Windows Development
Another common pitfall is mixing the native Windows Docker Desktop environment with legacy setups. If you are using Docker Desktop on Windows, the container runtime is actually WSL 2 (Windows Subsystem for Linux). While this provides a Linux kernel, the file system layer underneath can still behave differently regarding path separators if not properly configured.
Ensure that your docker-compose.yml does not inadvertently expose Windows-specific paths or configurations to the container. The links directive shown in the source material is also deprecated in modern Docker Compose versions (v2+), though it may still function in v1 environments like the one implied by version: '2'. Replacing links with explicit network configuration is a best practice that avoids potential DNS resolution issues that can sometimes compound path errors.
# Modernized networking example
networks:
app_network:
driver: bridge
services:
engine:
# ... build config ...
networks:
- app_network
db:
networks:
- app_network
By enforcing LF line endings and updating deprecated directives, you eliminate the carriage return interference. This ensures that /usr/bin/env receives a clean path to php, allowing your Symfony application to initialize correctly regardless of whether the host machine is Ubuntu or Windows 10.