设置从主机到 docker 容器的 PHP 路径 [英] set PHP path from host to docker container

查看:52
本文介绍了设置从主机到 docker 容器的 PHP 路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个相当愚蠢的问题,但我有以下问题.我使用一年以上的 Docker 和一个编辑器来更改我的程序,它是作为一个卷的主机.

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.

我没有安装 php,因为它只在容器内运行,就像我的几乎所有其他服务器程序(如 sql、apache)一样.现在我安装了 Visual Studio 代码,但它找不到使用智能感知的 php 路径.

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.

我知道我可以在我的 docker-compose 或 Dockerfile 中设置环境路径来为我的容器设置环境.但是容器在运行时与外部隔离,除了像 docker cp 这样的命令.

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.

是否可以设置从我的主机到容器机器的路径,以便 Visual Studio 代码可以在容器内找到 PHP 并将其用于智能感知?还是我必须在我的主机上安装 php?但在我看来,这会破坏 Docker 容器的使用.

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.

例如在 Visual Studio 代码配置 settings.json

for example in visual studio code config settings.json

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

推荐答案

诀窍是创建一个 Bash 文件来调用我们的 PHP 容器.

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

首先,使用这个 docker-compose.yml 启动一个 PHP7 容器并使其保持运行

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

在/usr/local/bin中创建一个名为php的文件

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

chmod 使其可执行

Chmod to make it executable

sudo chmod +x php

该文件将包含使用我们正在运行的容器来处理 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 是我们主机上的用户 ID 和用户组.我们必须在主机上以当前用户身份运行,这样容器就不会修改我们文件的所有者.

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

查看结果.

这篇关于设置从主机到 docker 容器的 PHP 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆