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

VS Code fails to connect to a PHP code in a running container. #3024

Closed
rspiewak47 opened this issue May 19, 2020 · 17 comments
Closed

VS Code fails to connect to a PHP code in a running container. #3024

rspiewak47 opened this issue May 19, 2020 · 17 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug containers Issue in vscode-remote containers

Comments

@rspiewak47
Copy link

  • VSCode Version: 1.45.1
  • OS Version: Ubuntu Linux 18.04

Steps to Reproduce:

  1. Construct a set of containers following the example in:
    https://www.cloudreach.com/en/resources/blog/containerize-this-how-to-use-php-apache-mysql-within-docker-containers/
  2. Open the folder containing the PHP code in VS Code
  3. Run the set of containers (3 separate containers for Apache, PHP code and MySQL) using the docker-compose file.
  4. Attempt to connect to the PHP container from VS Code using RemoteContainers Attach to Running Container.

A second VS Code window opens, and then displays a dialog box indicating "An error occurred attaching to the container."

There is no more specific indication in the log:

remoteContainers.0519.log

@kieferrm kieferrm transferred this issue from microsoft/vscode May 20, 2020
@chrmarti
Copy link
Contributor

chrmarti commented Jun 3, 2020

This works for me on Mac. Did maybe the container stop?

@chrmarti chrmarti self-assigned this Jun 3, 2020
@chrmarti chrmarti added info-needed Issue requires more information from poster ssh Issue in vscode-remote SSH labels Jun 3, 2020
@rspiewak47
Copy link
Author

rspiewak47 commented Jun 3, 2020

The container is running. One symptom of the issue is that VS Code opens a second window for this. What I would expect to see is that the current window, open to code which is running in the container, be connected for debugging. This would be similar to what happens with Visual Studio when you attach the debugger to an existing running process.
As shown in the attached log, the last events are:
[2020-05-04T18:51:04.560Z] [PID 26287] [17425 ms] Start: Run in container: for pid in cd /proc && ls -d [0-9]*; do { echo $pid ; readlink -f /proc/$pid/cwd ; xargs -0 < /proc/$pid/environ ; xargs -0 < /proc/$pid/cmdline ; } ; echo ; done 2>/dev/null
[2020-05-04T18:51:04.561Z] [PID 26287] [17426 ms] Stdin closed!
[2020-05-04T18:51:04.567Z] [PID 26287] [17432 ms] Shell server terminated (code: 2, signal: null)

@chrmarti
Copy link
Contributor

chrmarti commented Jun 3, 2020

Tried to reproduce this here without luck. Could you try running that last command inside the container to see if that somehow fails?

for pid in `cd /proc && ls -d [0-9]*`; do { echo $pid ; readlink -f /proc/$pid/cwd ; xargs -0 < /proc/$pid/environ ; xargs -0 < /proc/$pid/cmdline ; } ; echo ; done 2>/dev/null

@rspiewak47
Copy link
Author

In that container, I can't run bash, only sh.
So I ran this one step at a time:

cd /proc

/proc # ls -d [0-9]*
1 102 6 65 7 72 8
/proc # echo 1; readlink -f /proc/1/cwd
1
/var/www/html
/proc # echo 102; readlink -f /proc/102/cwd
102
/proc
/proc # echo 6; readlink -f /proc/6/cwd
6
/proc # echo 65; readlink -f /proc/65/cwd
65
/root/.vscode-server/bin/5763d909d5f12fe19f215cbfdd29a91c0fa9208a
/proc # echo 7; readlink -f /proc/7/cwd
7
/proc # echo 72; readlink -f /proc/72/cwd
72
/root/.vscode-server/bin/5763d909d5f12fe19f215cbfdd29a91c0fa9208a
/proc # echo 8; readlink -f /proc/8/cwd
8
/proc #

@rspiewak47
Copy link
Author

/proc # xargs -0 < /proc/1/cmdline
php-fpm: master process (/usr/local/etc/php-fpm.conf)
/proc # xargs -0 < /proc/102/cmdline
sh
/proc # xargs -0 < /proc/6/cmdline
php-fpm: pool www
/proc # xargs -0 < /proc/65/cmdline
sh /root/.vscode-server/bin/5763d909d5f12fe19f215cbfdd29a91c0fa9208a/server.sh --disable-user-env-probe --use-host-proxy --port 0
/proc # xargs -0 < /proc/7/cmdline
php-fpm: pool www
/proc # xargs -0 < /proc/72/cmdline
/root/.vscode-server/bin/5763d909d5f12fe19f215cbfdd29a91c0fa9208a/node /root/.vscode-server/bin/5763d909d5f12fe19f215cbfdd29a91c0fa9208a/out/vs/server/main.js --disable-user-env-probe --use-host-proxy --port 0
/proc # xargs -0 < /proc/8/cmdline
php-fpm: pool www
/proc #

@rspiewak47
Copy link
Author

php-fpm: pool www
/proc # xargs -0 < /proc/1/environ
master process (/usr/local/etc/php-fpm.conf)
/proc # xargs -0 < /proc/102/environ
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=0bdfb720fc55 TERM=xterm PHPIZE_DEPS=autoconf dpkg-dev dpkg file g++ gcc libc-dev make pkgconf re2c PHP_INI_DIR=/usr/local/etc/php PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 PHP_LDFLAGS=-Wl,-O1 -Wl,--hash-style=both -pie GPG_KEYS=1729F83938DA44E27BA0F4D3DBDB397470D12172 B1B44D8F021E4E2D6021E995DC9FF8D3EE5AF27F PHP_VERSION=7.2.7 PHP_URL=https://secure.php.net/get/php-7.2.7.tar.xz/from/this/mirror PHP_ASC_URL=https://secure.php.net/get/php-7.2.7.tar.xz.asc/from/this/mirror PHP_SHA256=eb01c0153b3baf1f64b8b044013ce414b52fede222df3f509e8ff209478f31f0 PHP_MD5= HOME=/root
/proc # xargs -0 < /proc/6/environ
sh: can't open /proc/6/environ: Permission denied
/proc # xargs -0 < /proc/65/environ
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=0bdfb720fc55 PHPIZE_DEPS=autoconf dpkg-dev dpkg file g++ gcc libc-dev make pkgconf re2c PHP_INI_DIR=/usr/local/etc/php PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data--with-fpm-group=www-data --disable-cgi PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 PHP_LDFLAGS=-Wl,-O1 -Wl,--hash-style=both -pie GPG_KEYS=1729F83938DA44E27BA0F4D3DBDB397470D12172 B1B44D8F021E4E2D6021E995DC9FF8D3EE5AF27F PHP_VERSION=7.2.7 PHP_URL=https://secure.php.net/get/php-7.2.7.tar.xz/from/this/mirror PHP_ASC_URL=https://secure.php.net/get/php-7.2.7.tar.xz.asc/from/this/mirror PHP_SHA256=eb01c0153b3baf1f64b8b044013ce414b52fede222df3f509e8ff209478f31f0 PHP_MD5= SHELL=/bin/ash VSCODE_AGENT_FOLDER=/root/.vscode-server HOME=/root
/proc # xargs -0 < /proc/7/environ
sh: can't open /proc/7/environ: Permission denied
/proc # xargs -0 < /proc/72/environ
PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi PHP_INI_DIR=/usr/local/etc/php HOSTNAME=0bdfb720fc55 SHLVL=1 HOME=/root PHP_LDFLAGS=-Wl,-O1 -Wl,--hash-style=both -pie PHP_MD5= PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 PHP_VERSION=7.2.7 GPG_KEYS=1729F83938DA44E27BA0F4D3DBDB397470D12172 B1B44D8F021E4E2D6021E995DC9FF8D3EE5AF27F PHP_ASC_URL=https://secure.php.net/get/php-7.2.7.tar.xz.asc/from/this/mirror PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 PHP_URL=https://secure.php.net/get/php-7.2.7.tar.xz/from/this/mirror VSCODE_AGENT_FOLDER=/root/.vscode-server PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin SHELL=/bin/ash PWD=/root/.vscode-server/bin/5763d909d5f12fe19f215cbfdd29a91c0fa9208a PHPIZE_DEPS=autoconfdpkg-dev dpkg file g++ gcc libc-dev make pkgconf re2c PHP_SHA256=eb01c0153b3baf1f64b8b044013ce414b52fede222df3f509e8ff209478f31f0
/proc # xargs -0 < /proc/8/environ
sh: can't open /proc/8/environ: Permission denied
/proc #

@rspiewak47
Copy link
Author

So on pids 6, 7, and 8 there was a Permission denied error. Could this be the problem?

@chrmarti
Copy link
Contributor

chrmarti commented Jun 4, 2020

I think we ignore these. I can reproduce something similar with the Apache container, but not the PHP one.

Your log shows Alpine 3.7, I see 3.6 (unsupported) with the Apache and 3.8 with the PHP container when using the repository you linked to.

Could you append the Dockerfile you PHP container is using?

@rspiewak47
Copy link
Author

FROM php:7.2.7-fpm-alpine3.7
RUN apk update;
apk upgrade;
RUN docker-php-ext-install pdo_mysql
RUN apk add --update --virtual autoconf
RUN apk --no-cache add pcre-dev ${PHPIZE_DEPS}
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
RUN apk del pcre-dev ${PHPIZE_DEPS}
RUN echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_autostart=on" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.idekey = VSCODE" >> /usr/local/etc/php/conf.d/xdebug.ini

@rspiewak47
Copy link
Author

That may have been the wrong one, but similar. Here's the likely one;
FROM php:7.2.7-fpm-alpine3.7
RUN apk update;
apk upgrade;
RUN docker-php-ext-install pdo_mysql
RUN apk add --update --virtual autoconf
RUN apk --no-cache add pcre-dev ${PHPIZE_DEPS}
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
RUN apk del pcre-dev ${PHPIZE_DEPS}
RUN echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_autostart=on" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.idekey = VSCODE" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_host = localhost" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_port = 9000" >> /usr/local/etc/php/conf.d/xdebug.ini

@rspiewak47
Copy link
Author

TL;DR:
This is based on this article:
https://www.cloudreach.com/en/resources/blog/ct-apache-docker-containers/

@chrmarti chrmarti added containers Issue in vscode-remote containers under-discussion Issue is under discussion for relevance, priority, approach and removed info-needed Issue requires more information from poster ssh Issue in vscode-remote SSH labels Jun 5, 2020
@chrmarti
Copy link
Contributor

chrmarti commented Jun 5, 2020

More testing shows that we currently don't run with Alpine 3.7. You could use a newer image with Alpine 3.8 or newer. (I see a long list of newer PHP FPM images on Docker Hub.)

@Chuxel Should we run on Alpine 3.7 (I'm not sure what the lowest version is that worked before) or can we update the documentation to require Alpine 3.8+?

We also need to add a version check and fail with a useful error message.

@chrmarti chrmarti added this to the June 2020 milestone Jun 5, 2020
@Chuxel
Copy link
Member

Chuxel commented Jun 5, 2020

Yeah I think that is reasonable - 3.7 is end of life (so is 3.8 actually). The distro updates very quickly. I should likely tweak the Apline 3.10 definition to be like Ubuntu in that regard with multiple variations.

@rspiewak47
Copy link
Author

rspiewak47 commented Jun 5, 2020

Thanks! I updated the alpine version, and made a few other changes.
This issue is now resolved.
I found a tip at:
docker-library/php#479 on moving the php-fpm port away from the xdebug port. I have verified that I can now connect to the code in the container and set breakpoints.

My Apache VirtualHost which proxies PHP to the php container uses 8090, and the Docker file for this container now modifies the php-fpm port to the same value.
This may be needed by anyone else trying to take the next step of being able to debug PHP code in a container which is separate from the apache container.

One remaining quibble: VS Code opened in a separate window, and I had to install the PHP debug extension into the container. I'd still like to see any of that happen behind the scenes, stay in the window I started from and have it all just work - the way that Visual Studio does in attaching to a running process.

Here is my current docker file:

FROM php:7.3.18-fpm-alpine3.11
RUN apk update;
apk upgrade;
RUN docker-php-ext-install pdo_mysql
RUN sed -i 's/9000/8090/' /usr/local/etc/php-fpm.d/zz-docker.conf
RUN apk add --no-cache autoconf
RUN apk --no-cache add pcre-dev ${PHPIZE_DEPS}
RUN pecl install xdebug-2.9.6
RUN docker-php-ext-enable xdebug
RUN apk del pcre-dev ${PHPIZE_DEPS}
RUN echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_autostart=on" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.idekey = VSCODE" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_host = localhost" >> /usr/local/etc/php/conf.d/xdebug.ini
&& echo "xdebug.remote_port = 9000" >> /usr/local/etc/php/conf.d/xdebug.ini

@chrmarti
Copy link
Contributor

chrmarti commented Jun 5, 2020

@rspiewak47 I'll track the addition of a version check (for >=3.8) here. Could you open a new issues for the remaining requests?

@chrmarti chrmarti added bug Issue identified by VS Code Team member as probable bug and removed under-discussion Issue is under discussion for relevance, priority, approach labels Jun 5, 2020
@rspiewak47
Copy link
Author

rspiewak47 commented Jun 5, 2020 via email

@chrmarti chrmarti modified the milestones: July 2020, August 2020 Aug 6, 2020
@chrmarti chrmarti modified the milestones: August 2020, September 2020 Sep 3, 2020
@chrmarti chrmarti modified the milestones: September 2020, October 2020 Oct 1, 2020
@chrmarti chrmarti modified the milestones: October 2020, November 2020 Oct 30, 2020
@chrmarti
Copy link
Contributor

chrmarti commented Nov 5, 2020

Fixed with #3280

@chrmarti chrmarti closed this as completed Nov 5, 2020
@chrmarti chrmarti removed this from the November 2020 milestone Nov 5, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Dec 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug containers Issue in vscode-remote containers
Projects
None yet
Development

No branches or pull requests

3 participants