Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set PHP path from host to docker container

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
like image 227
Gabbax0r Avatar asked Jun 12 '17 13:06

Gabbax0r


People also ask

Can I run PHP in Docker?

You can use docker run to create a container and execute PHP. You just need to add some volumes to the container. These volumes should include the paths to your code.

What is host path and container path in Docker?

It is actually the path within the container where you would like to mount /path/from/host . For example, if you had a directory on the host, /home/edward/data , and you wanted the contents of that directory to be available in the container at /data , you would use -v /home/edward/data:/data .

Can Docker communicate with localhost?

docker run --network="host" Alternatively you can run a docker container with network settings set to host . Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0.0.1 ) will refer to the docker host.


1 Answers

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.

like image 130
Quinn Wynn Avatar answered Sep 28 '22 22:09

Quinn Wynn