Use git feature to 'hide' local changes of a Dockerfile which installs xdebug

Tag
Author: Ally
Published:

Summary:

Use git update-index –assume-unchanged to remove local changes of a Dockerfile from the staging area in a repository, and use (an ignored) docker-compose.override.yml to configure xdebug.

Table of Contents

  1. Docker Services
  2. Dockerfile changes
  3. Ignoring Dockerfile changes
  4. Configuring xdebug with docker-compose.override.yml
  5. Ignoring docker-compose.override.yml files

If you’re working on a team and want to make some very specific changes to your local development environment then git update-index --assume-unchanged (and some gitignore later on) might be for you.

Docker Services

docker-compose.yml:

---
version: '3.7'
services:
  php:
    container_name: my_php
    build:
      context: .
      dockerfile: Dockerfile

The team uses the following base image.

Dockerfile:

FROM php:8.1-fpm

Build and run the container:

docker-compose build php
docker-compose up -d
cat <<EOF | docker exec -i my_php bash
(php -m | grep -i 'xdebug') && echo 'xdebug is installed' || echo 'xdebug is not installed'
EOF

Output:

xdebug is not installed

No xdebug?

Dockerfile changes

I added the following to Dockerfile:

RUN pecl install xdebug-3.1.5 \
    && docker-php-ext-enable xdebug

Build and run the container:

docker-compose stop
docker-compose build php
docker-compose up -d
cat <<EOF | docker exec -i my_php bash
(php -m | grep -i 'xdebug') && echo 'xdebug is installed' || echo 'xdebug is not installed'
EOF

Output:

xdebug
Xdebug
xdebug is installed

xdebug

Ignoring Dockerfile changes

Running git status after adding the RUN command to install and enable xdebug:

$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	      modified:   Dockerfile

no changes added to commit (use "git add" and/or "git commit -a")

Then to ignore the file with git update-index:

git update-index --assume-unchanged Dockerfile

Running git status again:

$ git status
On branch main
nothing to commit, working tree clean

If you change your mind and want to commit some changes:

git update-index --no-assume-unchanged Dockerfile
$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   Dockerfile

no changes added to commit (use "git add" and/or "git commit -a")

Configuring xdebug with docker-compose.override.yml

I created a few xdebug.ini files:

99-xdebug-no-trigger-ide-key.ini
xdebug.mode = "debug"
# alternatively host.docker.internal
xdebug.client_host = "docker.for.mac.localhost"
xdebug.client_port = "9003"
xdebug.idekey = "PHPSTORM"
xdebug.max_nesting_level = 200
xdebug.start_with_request = "yes"
99-xdebug-no-trigger-no-ide-key.ini
xdebug.mode = "debug"
# alternatively host.docker.internal
xdebug.client_host = "docker.for.mac.localhost"
xdebug.client_port = "9003"
xdebug.idekey = ""
xdebug.max_nesting_level = 200
xdebug.start_with_request = "yes"
99-xdebug-trigger-ide-key.ini
xdebug.mode = "debug"
# alternatively host.docker.internal
xdebug.client_host = "docker.for.mac.localhost"
xdebug.client_port = "9003"
xdebug.idekey = "PHPSTORM"
xdebug.max_nesting_level = 200
xdebug.start_with_request = "trigger"
99-xdebug-trigger-no-ide-key.ini
xdebug.mode = "debug"
# alternatively docker.for.mac.localhost
xdebug.client_host = "docker.for.mac.localhost"
xdebug.client_port = "9003"
xdebug.idekey = ""
xdebug.max_nesting_level = 200
xdebug.start_with_request = "yes"

Create a docker-compose.override.yml so we can mount additional config files into the container.

docker-compose.override.yml:

---
version: '3.7'
services:
  php:
    volumes:
    - ./99-xdebug-trigger-ide-key.ini:/usr/local/etc/php/conf.d/99-xdebug-trigger-ide-key.ini:ro

Verify by using docker-compose config:

name: ac_website_article
services:
  php:
    build:
      context: /Users/alistaircollins/development/ac_website_article
      dockerfile: Dockerfile
    container_name: my_php
    networks:
      default: null
    volumes:
    - type: bind
      source: /Users/alistaircollins/development/ac_website_article/99-xdebug-trigger-ide-key.ini
      target: /usr/local/etc/php/conf.d/99-xdebug-trigger-ide-key.ini
      read_only: true
      bind:
        create_host_path: true
networks:
  default:
    name: ac_website_article_default

Ignoring docker-compose.override.yml files

There are a couple of methods:

Best method

If git core.excludesfile does not have a value, I suggest making ~/.gitignore and then use this path to set the core.excludesfile setting in git.

git core.excludesfile ~/.gitignore

Alternative method

Manually add docker-compose.override.yml to .git/info/exclude, or:

cat <<EOF >> .git/info/exclude
docker-compose.override.yml
EOF
Basic Cognito user pool with login/logout integration in Laravel, with users/system clients
To bottom
To top