Can I store Xdebug inside separate docker container?

Viewed 358

I am new to Docker and docker-compose. I have met the point that it's better to put each process to separate containers. So I put stuff like Composer, npm, artisan, Redis, pma to separate containers.

Now I am trying to integrate Xdebug and use it in PhpStorm. Can't understand how to put it separately. And should I actually? And do I understand this paradigm right?

My docker-compose.xml:

version: '3'

networks:
  laravel:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8088:80"
    volumes:
      - ../src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - laravel

  mysql:
    image: mysql:5.7.22
    container_name: mysql
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_DATABASE: test
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - laravel

  php:
    build:
      context: ./php
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ../src:/var/www/html
    ports:
      - "9000:9000"
    networks:
      - laravel

  pma:
    image: phpmyadmin/phpmyadmin
    container_name: pma
    environment:
      PMA_HOST: mysql
      PMA_PORT: "3306"
    ports:
      - "7760:80"
    links:
      - mysql
    networks:
      - laravel

  redis:
    image: redis:5-alpine
    container_name: redis
    command: redis-server --bind 0.0.0.0 --requirepass secret@123
    networks:
      - laravel
    ports:
      - "6379:6379"
    restart: unless-stopped
    volumes:
      - ./redis:/data

  queues:
    build:
      context: ./queues
      dockerfile: Dockerfile
    container_name: queues
    command: php /var/www/html/artisan queue:listen
    depends_on:
      - nginx
      - redis
    volumes:
      - ../src:/var/www/html
    networks:
      - laravel

  laravel-echo-server:
    image: oanhnn/laravel-echo-server
    container_name: laravel-echo-server
    depends_on:
      - redis
    environment:
      LARAVEL_ECHO_SERVER_AUTH_HOST: http://localhost:8088
      LARAVEL_ECHO_SERVER_DEBUG: 'true'
      LARAVEL_ECHO_SERVER_DB: redis
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PREFIX: laravel_
      REDIS_PASSWORD: secret@123
      REDIS_DB: 0
    networks:
      - laravel
    ports:
      - 6001:6001
    restart: unless-stopped

  artisan:
    build:
      context: ./php
      dockerfile: Dockerfile
    container_name: artisan
    volumes:
      - ../src:/var/www/html
    depends_on:
      - php
      - mysql
    working_dir: /var/www/html
    entrypoint: ['php', '/var/www/html/artisan']
    networks:
      - laravel

  xdebug:
    build:
      context: ./xDebug
      dockerfile: Dockerfile
    container_name: xdebug
    depends_on:
      - php
    working_dir: /var/www/html
    networks:
      - laravel

  composer:
    image: composer:latest
    container_name: composer
    volumes:
      - ../src:/var/www/html
    working_dir: /var/www/html
    networks:
      - laravel

  npm:
    image: node:latest
    container_name: npm
    volumes:
      - ../src:/var/www/html
    working_dir: /var/www/html
    entrypoint: ['npm']
    networks:
      - laravel

  git:
    image: alpine/git
    container_name: git
    volumes:
      - ../src:/var/www/html
    working_dir: /var/www/html
    networks:
      - laravel
1 Answers

No, you should include Xdebug inside the container you're trying to debug (be it Laravel or any other PHP application).

You shouldn't actually break down each and every part of every component from your application into different containers, but instead set containers following a separation of concerns concept inside your application, usually based on their role inside the application architecture.

Example (each bullet is a container):

  • Application server with HTTP routes for APIs
  • Artisan queue
  • Static content server with Angular/React/etc (this one could also be bundled with the first)
  • Redis database
  • MySQL database

And with each one, install everything you need to run/debug/profile the application inside (i.e. mysql+mysql-cli, or PHP+composer+Xdebug).

You can read more in this answer.

Related