How to set PHP Path to Docker container

At first, start a PHP container and keep it running.

docker run -itd --restart always --name php-path php:7.4

Create a file named php in /usr/local/bin folder.

sudo nano /usr/local/bin/php

With the contents below:

#!/bin/bash
docker exec -i --user=1000:1000 php-path php "$@"

Make the file executable.

sudo chmod +x /usr/local/bin/php

1000:1000 are the user and group ids on the host machine. This is necessary to prevent the container from changing the owner of the files.

That’s it. Now you can try it:

php -v