Simple examples of using Docker. Creation of containers with PHP 7/8 and XDebug. It is made from:
- Alpine Linux image: https://hub.docker.com/_/alpine (Dockerfile.70)
- Official PHP image: https://hub.docker.com/_/php (Dockerfile.81)
These Dockerfiles are based on the official images, but they have been modified to install XDebug and other extensions.
mkdir ~/proyectos && cd ~/proyectos
git clone https://github.com/jesuserro/docker.git
📦home
┣ 📂jesus
┃ ┗ 📂proyectos
┃ ┃ ┗ 📂docker
┃ ┃ ┗ 📂project-1
┃ ┃ ┗ 📂ofertas
┃ ┃ ┗ 📂nges
cd ~/proyectos/docker
# Run/Launch container for php 7.0
docker compose up
docker-compose docker-compose.dev.yml up
# Run/Launch all containers for php 7.0 and php 8
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up
- http://localhost:8070
- http://localhost:8081
- http://localhost:8070/ofertas/public/admin/index.php
- http://localhost:8081/nges/public/
# Create new image called "my-php-image70" from existing "Dockerfile.70"
docker build -t my-php-image70 -f Dockerfile.70 .
docker build -t my-php-image81 -f Dockerfile.81 .
# Run new container instance called "my-php-container70" based on existing image "my-php-image70"
docker run -d -p 8070:80 --name my-php-container70 my-php-image70 -v /home/jesus/proyectos:/var/www/html
docker run -d -p 8081:80 --name my-php-container81 my-php-image81 -v /home/jesus/proyectos:/var/www/html
# Start terminal session inside the "my-php-container70" container
docker exec -it my-php-container70 /bin/bash
# Show container logs
docker logs my-php-container70
# Stopping a container
docker stop my-php-container70
# Removing all docker containers
docker container rm -f $(docker container ls -aq)
# Removing all docker images
docker rmi $(docker images -q)
# Removing all in 1 sentence
docker container rm -f $(docker container ls -aq) && docker rmi $(docker images -q)
# Full remove
docker system prune -a
# Borrado total de la caché
docker builder prune -a
In your WSL2 terminal, run this command to get your IP address:
ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
# Returning: 172.20.28.159
So put it at the xdebug.ini
file:
xdebug.remote_host = 172.20.28.159
No need IP definition, so put it at the xdebug.ini
file:
xdebug.remote_host=host.docker.internal
and in the docker-compose.dev.yml
file, beware of these environment configurations:
environment:
XDEBUG_MODE: develop,debug
XDEBUG_CONFIG:
client_host=host.docker.internal
start_with_request=yes
It might be breaking the breakpoint debug.
If the IP is correct, you should see the Call Stack requests in the Debug Pane at VSCODE.
Now, in the launch.json
in your VSCODE project, add this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug on Docker",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/html/docker/": "${workspaceFolder}/"
},
"log": true,
"ignore": [
"**/vendor/**/*.php"
]
}
]
}
At Debug Console we'll see:
Listening on { address: '::', family: 'IPv6', port: 9003 }
# Getting executable from the container
docker cp php70-apache:/usr/local/bin/php ./var/bin/php
# In VSCODE settings.json
"php.executablePath": "./var/bin/php"
# Executable from your local LAMP
which php
# In VSCODE settings.json
"php.executablePath": "/usr/bin/php"
Docker containers are useful for testing your code in different PHP versions. This video shows how to use Docker to test your code in PHP 5.3, 5.4, 5.5, 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, and 8.1.
- Migración de Zend Framework 1 a PHP 8.1
- Terraform a cloud management service for AWS, Google Cloud, Azure, and so on.
- https://github.com/PauGa9/php-symfony-mysql-nginx-docker
- https://github.com/thecodeholic/php-mvc-framework/blob/master/docker/php.ini
- https://jasonterando.medium.com/debugging-with-visual-studio-code-xdebug-and-docker-on-windows-b63a10b0dec
- https://github.com/thecodeholic/php-mvc-framework
- https://stackoverflow.com/questions/52579102/debug-php-with-vscode-and-docker