简体   繁体   English

设置从主机到docker容器的PHP路径

[英]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. 我使用一年以上的Docker和一个编辑器来改变我的程序,它是作为卷的hostet。

i dont have installed php because it only runs inside of the containers, like almost all other of my server programms (like sql, apache). 我没有安装php,因为它只在容器内运行,就像几乎所有其他服务器程序(如sql,apache)一样。 now i installed visual studio code and it cannot find the path to php to use intellisense. 现在我安装了visual studio代码,它无法找到使用intellisense的php路径。

i know that i can set an environment path inside my docker-compose or Dockerfile to set an environment for my container. 我知道我可以在docker-compose或Dockerfile中设置一个环境路径来为我的容器设置一个环境。 but the container is, if its run, isolated to the outside, except for commands like docker cp. 但是,如果它的运行,容器是隔离到外面的,除了像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? 是否可以设置从主机到容器机器的路径,以便visual studio代码可以在容器内部找到PHP并将其用于智能感知? or do i have to install php on my host machine? 或者我必须在我的主机上安装php? but this would destroy the usage of the Docker containers in my opinion. 但在我看来,这会破坏Docker容器的使用。

for example in visual studio code config settings.json 例如在visual studio code config settings.json

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

The trick is to create a Bash file that calls to our PHP container. 诀窍是创建一个调用我们的PHP容器的Bash文件。

At first, start a PHP7 container and keep it running by using this docker-compose.yml 首先,启动一个PHP7容器并使用此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 在/ usr / local / bin中创建一个名为php的文件

Chmod to make it executable Chmod使其可执行

sudo chmod +x php

This file will contain this script that use our running container to process php 该文件将包含使用我们的运行容器来处理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. 1000:1000是我们的用户ID和我们主机上的用户组。 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. 看到结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用来自 Docker 容器的 PHP sendmail 通过主机 Postfix 发送 - Use PHP sendmail from Docker container to send through host Postfix 如何在 docker 容器中设置 php 时在 vscode 中设置 php 可执行路径 php.validate.executablePath? - How to set php executable path php.validate.executablePath in vscode when php is set inside docker container? Docker - 无法从Mac Host访问容器 - Docker - cannot access container from Mac Host 如何将 docker 容器上的 php 脚本与网络主机连接,同时在网络主机上使用 mysql Z05B6053C41A2130AFD68BDAB3E 容器 - How connect a php script on docker container with network host, with a mysql docker container also on network host 使用docker-machine从主机输入docker容器 - Enter docker container from host using docker-machine Docker:docker-compose 将文件从容器复制到主机 - Docker: docker-compose copy files from container to host 使用php5-fpm的docker容器连接主机nginx和msql - Connect host nginx and msql with a docker container of php5-fpm nginx 容器正在从主机运行 php - nginx container is running php from host 从docker容器中的laravel应用程序连接到主机中的Postgresql? - Connect from laravel app in docker container to Postgresql in host machine? 从另一个Docker容器(同一主机,Apache)调用Docker容器中的API - Calling an API in a Docker container from a different Docker container (same host, Apache)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM