Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Support docker-compose environments. #173

Open
oskarcalvo opened this issue Apr 6, 2020 · 10 comments
Open

[Feature] Support docker-compose environments. #173

oskarcalvo opened this issue Apr 6, 2020 · 10 comments

Comments

@oskarcalvo
Copy link

Hello.

My computer it's complete clean and I have all my environments in docker machine, would be great if we can configure a path or task to run by project, so I can tell to the plugin to run:

docker-compose exec php vendor/bin/phpcs web/modules/custom/hostal_alter_list instead of try to find the phpcs in my local machine.

@ljubadr
Copy link

ljubadr commented Mar 14, 2022

I was in similar situation trying to setup few different vscode extensions to work with specifying docker-compose exec... or docker exec ...

I ended up using Remote Containers in vscode, specifically Open an existing folder in a container to attach to already running docker container

This way pretty much every extension works out of the box

Default terminal is the one inside the container but you can select terminal with

  • Terminal: Create New Integrated Terminal will open a terminal window within a container
  • Terminal: Create New Integrated Terminal (Local) will open a terminal on your host machine

Only "downside" is that I need to install extensions in the remote container and it won't use same extensions as my host. It will show them in the extension list and you need to install them.

But if you can create list of default extensions that will be installed in every remote container by updating your global setttings.json

    "remote.containers.defaultExtensions": [
        // list of extensions that we want to install in every remote container
        ...
    ]

@luigimannoni
Copy link

luigimannoni commented Oct 24, 2022

My attempt was different, I created a new bash script file inside .vscode/ just to keep it hidden from the version control and set it as +x, then on the local settings.json I have pointed the phpcs executable to it.

In short intercepts arguments and rewrites them to mutate the local to remote container folder path.
For example, if my local project was sitting on /home/john/repo/project and the remote container runs everything off /app (and phpcs is located into /app/vendor/bin/phpcs within the container) I would substitute all paths:

#!/bin/bash
LOCAL_PATH='/home/john/repo/project/'
REMOTE_PATH='/app/'

for param; do
  if [[ "$param" == *"$LOCAL_PATH"* ]]; then
    param=${param//$LOCAL_PATH/$REMOTE_PATH}
  fi

  newparams+=("$param")
done

/usr/local/bin/docker-compose exec -T php /app/vendor/bin/phpcs ${newparams[@]}

if you need to use -f or -p flags for docker-compose just add them before exec

And, voilá...
Screenshot from 2022-10-24 14-01-08
Working codesniffer! A bit of per-project overhead in configuration but things do work smoothly.

Extra thing: I had to use a different extension at the end because the current (and its fork) are both outdated and won't work with the newest phpcs versions. But same technique still applies.

@ultimike
Copy link

@luigimannoni which extension did you end up using?

@luigimannoni
Copy link

luigimannoni commented Feb 15, 2023

@ultimike I used zobo.php-intellisense, still works for the latest phpcs/phpcbf

@ultimike
Copy link

ultimike commented Mar 4, 2023

@luigimannoni I don't understand. You've configured zobo.php-intellisense to point to phpcs and phpcbf inside of a Docker container?

Stepping back a bit - with the valeryanm.vscode-phpsab extension, I have the following configuration (that doesn't work). Note how I'm pointing at a couple of .bash files for phpcs and phpcbf.

    /* PHP Sniffer & Beautifier */
    "phpsab.snifferEnable": true,
    "phpsab.executablePathCS": "/Users/michael/sites/pmd-2023-winter/pmd/phpcs.bash",
    // "phpsab.executablePathCS": "/Users/michael/sites/drupal_coder/vendor/bin/phpcs",
    "phpsab.fixerEnable": false,
    // "phpsab.executablePathCBF": "/Users/michael/sites/drupal_coder/vendor/bin/phpcbf",
    "phpsab.executablePathCS": "/Users/michael/sites/pmd-2023-winter/pmd/phpcbf.bash",
    "phpsab.standard": "/Users/michael/sites/drupal_coder/phpcs.xml",
    "phpsab.snifferMode": "onType",

I was assuming that I'd have to set something similar in the zobo.php-intellisense settings to get it to work, but I don't see anything obvious.

Can you assist?

thanks,
-mike

@luigimannoni
Copy link

@ultimike no worries, took me few tries before getting it right.

I did setup a repo for my co-workers few weeks ago, so you can follow the example as well: https://github.com/luigimannoni/vscode-phpcs-docker

The only steps you need to make it work is to change php, phpcs and phpcbf script files to point the right docker-compose file and or docker container names. Works as well with a docker exec instead using docker-compose.

Oh, and don't forget to chmod +x to make the scripts executable.

@ultimike
Copy link

ultimike commented Mar 5, 2023

@luigimannoni Thanks so much - I feel like I'm just about there. Looking at the phpcs file, I have a couple of questions:

#!/bin/bash
LOCAL_PATH=`pwd`
REMOTE_PATH='/app/'

for param; do
  if [[ "$param" == *"$LOCAL_PATH"* ]]; then
    param=${param//$LOCAL_PATH/$REMOTE_PATH}
  fi

  newparams+=("$param")
done

/usr/local/bin/docker-compose -f $LOCAL_PATH/docker/docker-compose.yml -p drupal8 exec -T php /app/vendor/bin/phpcs ${newparams[@]}
  1. I'm using DDEV, so my $REMOTE_PATH is a /var/www/html which is not a big deal to change, but wouldn't it be best if the last line was:

/usr/local/bin/docker-compose -f $LOCAL_PATH/docker/docker-compose.yml -p drupal8 exec -T php $REMOTE_PATH/vendor/bin/phpcs ${newparams[@]}

  1. Using DDEV, I believe the correct docker compose file I should be using is $LOCAL_PATH/.ddev/.ddev-docker-compose-full.yml - not sure if you have any experience with DDEV and if you can confirm my thinking.

  2. I'm struggling trying to figure out the proper name for the docker container name. I'm assuming it is the same as the name in my docker compose file? I've tried a bunch of variations all with the same result (testing from the command line using just phpcs --version to try to get some reasonable output):

$ /usr/local/bin/docker-compose -f /Users/michael/sites/pmd-2023-winter/pmd/.ddev/.ddev-docker-compose-full.yaml -p ddev-pmd exec -T php /var/www/html/vendor/bin/phpcs --version
service "php" is not running container #1

Thoughts?

thanks,
-mike

@luigimannoni
Copy link

@ultimike never had experience with it, but by the looks of it you probably need to target the -T php with the actual service name inside the docker compose file with the php binary on it. If it's called web try -T web.

As a last resort I think you can change the entire command with docker exec -T <container name> ... and target directly the php container

@ultimike
Copy link

ultimike commented Mar 8, 2023

@luigimannoni Thanks so much - I now have things 99% working 👍

phpcs and phpcbf are working without a hitch (so far).

The php bash script seems to always display the following issue in the problems tab, regardless of the file:

syntax error, unexpected double-quote mark, expecting "," or ";" [Ln 20182, Col 1]

When I try running my php.bash file from the command line, I get a similar error:

$ ./php.bash
PHP Parse error:  syntax error, unexpected double-quote mark, expecting "," or ";" in /usr/bin/php8.1 on line 20182

Parse error: syntax error, unexpected double-quote mark, expecting "," or ";" in /usr/bin/php8.1 on line 20182

Any idea what this is referring to?

Here's my php.bash file:

#!/bin/bash
LOCAL_PATH='/Users/michael/sites/pmd-2023-winter/pmd/'
REMOTE_PATH='/var/www/html/'
DDEV_PROJECT_NAME='ddev-pmd'

for param; do
  if [[ "$param" == *"$LOCAL_PATH"* ]]; then
    param=${param//$LOCAL_PATH/$REMOTE_PATH}
  fi

  newparams+=("$param")
done

/usr/local/bin/docker-compose -f $LOCAL_PATH/.ddev/.ddev-docker-compose-full.yml -p $DDEV_PROJECT_NAME exec -T web php /usr/bin/php ${newparams[@]}

thanks,
-mike

@luigimannoni
Copy link

@ultimike Not entirely sure what it is causing this as I don't recall anything similar but I had a strange error which wasn't related to any issue in my files, so I'd say it was along the same lines of your same problem. It was strictly related to the phpcs version I was using in the project and the extension no longer supporting the new phpcs as I mentioned in the first post above, changing extension solved the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants