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

查看:488
本文介绍了设置从主机到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代码,但找不到使用intellisense的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

推荐答案

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

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天全站免登陆