2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

docker-compose・VSCodeを使ったXdebug環境構築方法

Last updated at Posted at 2019-10-12

概要

 docker-compose・VSCodeを使ったXdebug環境構築方法を調べるとたくさんの情報が得られます。しかし、それらの情報を元に環境構築をやってみても、なかなか上手くいかずにかなりの時間悩んでいました。
 そこで、私と同じような状況の人の助けになればと思い、上手く動くようになった方法を紹介したいと思います。現在進行形でdockerの勉強をしている状況なので、Dockerfile等の中身に関してはお粗末だと思いますが、今回はXdebugの環境構築がメインなので悪しからず。また、記載してる箇所は必要最低限の部分のみにしてあります。

各種ファイルの中身紹介

Dockerfile

FROM centos:centos6.9
EXPOSE 80
RUN yum update -y && \
  yum install -y vim
# php
RUN yum install -y http://rpms.famillecollet.com/enterprise/remi-release-6.rpm && \
  yum install -y --enablerepo=remi,remi-php73 php php-devel php-mbstring php-pdo php-gd php-xml php-mcrypt php-xdebug php-pear php-xmlrpc php-pecl-apcu php-pecl-xdebug php-cli php-gd php-mysqlnd php-intl
# apache
RUN yum install -y httpd httpd-tools httpd-devel httpd-manual

docker-compose.yml

version: "3"
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./app:/home/app
    working_dir: /home/app
    ports:
      - "80:80"

php.ini

最下部に追記
remote_host に関しては mac と windows で書き方が異なるので注意
また、windows に関しては pro のみ動作確認済み

[XDebug]
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.dump_undefined = 1
xdebug.remote_enable = 1
xdebug.default_enable = 1

; mac
xdebug.remote_host=docker.for.mac.host.internal
; windows
xdebug.remote_host=docker.for.win.host.internal

xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart = On
xdebug.remote_log="/tmp/xdebug_log"

15-xdebug.ini

この部分をコメントアウトしただけ

; zend_extension=xdebug.so

launch.json(VSCode の PHP Debug の設定)

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for XDebug",
      "type": "php",
      "request": "launch",
      "port": 9000, //php.iniで設定したxdebug用のport番号
      "pathMappings": {
        // {docker上のdocument root}:{ローカルのdocument root}
        "/home/app": "${workspaceFolder}/app",
        "stopOnEntry": true,
        "log": true
      }
    }
  ]
}

注意点

 いろいろ試行錯誤するなかでポイントだと思った点は、Xdebug で使用するポート番号を Dockerfile や docker-compose.yml に記載しないことです。私は、これを記載していたためうまく動きませんでした。もし、Xdebug を実行してもブレークポイントで止まらないなど上手く動作していない場合は、この点を確かめてみてください。

*以下のように記載しないように注意

Dockerfile

EXPOSE 80 9000
# ↑ 9000 を書いてはダメ

docker-compose.yml

ports:
  - "80:80"
  - "9000:9000" # ← 9000 を書いてはダメ

以上、参考になればうれしいです。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?