How to set PHP Path to Docker container

Luis Coutinho
Dec 7, 2020

--

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

--

--

Luis Coutinho
Luis Coutinho

Written by Luis Coutinho

CTO at C-MORE | Beyond the Obvious

Responses (2)