How to configure PHP, Xdebug, Docker and PHPStorm

Updated: 15 November 2023

1. Select these two items in PHPStorm:
Run > Start Listening for PHP Debug Connections
Run > Break at first line in PHP scripts

2. Load a page in a web browser.

3. PHPStorm should display an Incoming Connection From Xdebug dialog box.

4. From the bottom of the dialog, select the local file which matches the ‘File path on server’ value and click Accept.

cron

Updated: 11 October 2023

Maintain crontab files for individual users.

Edit the current crontab. After exiting the editor, the modified crontab will be installed automatically.

crontab -e

Display the current crontab on standard output

crontab -l

Start a Docker project when host system is rebooted

@reboot sleep 60 && touch /home/chris/docker-project/reboot.log && echo "$(date): System rebooted, attempting to start." >> /home/chris/docker-project/reboot.log && cd /home/chris/docker-project && docker compose up -d

Save output to file

*   *   *   *   *   /home/chris/purge.sh > /bin/purge.log 2>&1

PHP in Docker

Updated: 14 March 2024

Run a php script

docker run -it --rm --name my-running-script \
-v "$PWD":/usr/src/myapp \
-w /usr/src/myapp \
php:7.4-cli php script.php

Run the php development server

docker run -it --rm --name php-server \
-v "$PWD":/usr/src/myapp \
-w /usr/src/myapp \
-p 8000:8000 \
php:8.2-cli php -S 0.0.0.0:8000

Docker

Updated: 14 June 2023

Build and tag an image from a Dockerfile in the same directory

docker build -t node-demo .

Start a container

docker run -i -t ubuntu /bin/bash
docker run -it node:19 /bin/bash
docker run -it --rm -d -p 8080:80 --name web nginx

Docker compose

docker-compose up --build
docker-compose up --remove-orphans -d

Stop all containers

docker stop $(docker ps -aq)

Remove all containers

docker rm $(docker ps -aq)

List images

docker images

See which containers are running

docker ps

See all containers, including non-running

docker ps -a

Remove stopped containers and images, including unused and dangling images

docker system prune -a

Delete all images

sudo docker rmi $(sudo docker images -q) --force

Run a php script inside a container

docker run --rm -v $(pwd):/app -w /app php:cli php hello.php

Get ip address of container

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container name>
sudo docker inspect <container name> | grep IPAddress

Command prompt into a container

sudo docker exec -it  bash
sudo docker exec -it wp-local-docker_phpfpm_1 /bin/sh -c "[ -e /bin/bash ] && /bin/bash || /bin/sh"
docker-compose exec --user www-data phpfpm bash

Keep a container running. From docker-compose.yml

services:
  myservice:
    command: bash -c "while true; do echo 'sleeping...' && sleep 10; done"

phpMyAdmin in Docker

Updated: 16 November 2023

phpMyAdmin is available as a Docker image. The MySQL server connection parameters are provided when the container is instantiated.

For a remote MySQL server

docker run --name myadmin \
-e PMA_HOST=https://example.com \
-e PMA_PORT=3306 \
-e PMA_USER=user \
-e PMA_PASSWORD=password \
-p 8080:80 \
phpmyadmin/phpmyadmin

Browse to phpMyAdmin at http://localhost:8080

MySQL server running in a local Docker network some-network

docker run --name myadmin \
--network=some-network \
-e PMA_HOST=db \
-e PMA_PORT=3306 \
-p 8080:80 \
phpmyadmin/phpmyadmin

--network The docker network the phpMyAdmin container will join.

phpdoc

Updated: 10 January 2024

PHPDoc reference

Generate php documentation with phpdoc tool and Docker

docker pull phpdoc/phpdoc
cd path/to/my-php-project
docker run --rm -v $(pwd):/data phpdoc/phpdoc -d src -t doc

Display cli manual

docker run --rm phpdoc/phpdoc --help