set PHP path from host to docker container

2019-07-05 10:44发布

i know this is rather a stupid question, but i have the following problem. i use Docker above a year and a editor to change my programm which is hostet as a volume.

i dont have installed php because it only runs inside of the containers, like almost all other of my server programms (like sql, apache). now i installed visual studio code and it cannot find the path to php to use intellisense.

i know that i can set an environment path inside my docker-compose or Dockerfile to set an environment for my container. but the container is, if its run, isolated to the outside, except for commands like docker cp.

is it possible to set a path from my host machine to the container machine, so that visual studio code can find PHP inside of the container and use it for intellisense? or do i have to install php on my host machine? but this would destroy the usage of the Docker containers in my opinion.

for example in visual studio code config settings.json

"php.validate.executablePath": DOCKERCONTAINER/usr/bin/php

1条回答
你好瞎i
2楼-- · 2019-07-05 11:14

The trick is to create a Bash file that calls to our PHP container.

At first, start a PHP7 container and keep it running by using this docker-compose.yml

version: "3"
services:  
  python:
    image: php:7.2
    container_name: php7-vscode
    restart: always #this option will keep your container always running, auto start after turn on your host machine
    stdin_open: true
    networks:
      - proxy
networks:
  proxy:
    external: true

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

Chmod to make it executable

sudo chmod +x php

This file will contain this script that use our running container to process php

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

1000:1000 is our user id and our user group on our host machine. We have to run as our current user on host machine so that the container won't modify our file's owner.

That's it. Now you can type

php -v

to see the result.

查看更多
登录 后发表回答