Setting up a LAMP development stack in Docker
The goods
My environment is Fedora Linux, Microsoft build of VScode, docker engine installed using docker repos.
In the root of your project, make the following files
compose.yaml
services:
web:
build:
context: .
dockerfile: php.dockerfile
container_name: php-apache
ports:
- "127.0.0.1:80:80" #this line maps your pc port to the container port
depends_on:
- db #this line links this container to the db container
volumes:
- ./html:/var/www/html #this line maps the content of ./html in your pc to the /var/www/html of the container
networks:
- php_network
extra_hosts:
- "host.docker.internal:host-gateway"
environment: # xdebug env var's required even though set in conf.d/docker-php-ext-xdeb
XDEBUG_MODE: develop,debug
XDEBUG_CONFIG: client_host=host.docker.internal
start_with_request=yes
db:
container_name: db
image: mariadb #check the mysql version you need for your project
environment:
MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: yes #insecure
volumes:
- ./mariadb_data:/var/lib/mysql #this line maps the content of ./mysql_data in your pc to the /var/lib/mysql of the container
ports:
- "3306:3306"
networks:
- php_network
phpmyadmin:
container_name: php-admin
image: phpmyadmin/phpmyadmin
ports:
- "127.0.0.1:8080:80" #this line maps your pc port to the container port
depends_on:
- db #this line links this container to the db container
environment:
PMA_HOST: db
networks:
- php_network
networks:
php_network:
name: php_network
php.dockerfile:
FROM php:apache
RUN docker-php-ext-install mysqli
RUN apt-get update && apt-get install -y libxml2-dev && docker-php-ext-install soap
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug
COPY conf.d/docker-php-ext-xdebug.ini /usr/local/etc/php/conf.d/
VSCode
Install Extensions
If you don't have these extensions installed yet, grab them:
- "PHP Debug" by Xdebug
- "Docker" by Microsoft
- "PHP Intelephense" by Ben Mewburn
Configure PHP Executable Path in VScode
- Press CTRL+SHIFT+P to open the Command Palette.
- Search for
Preference: Open User Settings (JSON)
and select it. - Press CTRL+F and search for the line
php.validate.executablePath
. - Set the value to
"./php"
. - Create a file called
php
in the root of your project with the following content:
php
#!/bin/bash
#make this file executable with chmod +x
#this file is used to provide vscode with a path to the php executable
docker exec -it php-apache php $@
- Make the file executable by running
chmod +x php
.
Set up a Debugging Profile for VScode
Press CTRL+SHIFT+D to open the Run and Debug tab. Select the settings gear or click "Add Configuration" and add the following configuration:
launch.json
{
"configurations": [
{
"name": "Listen on Docker for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/html": "${workspaceFolder}/html"
}
}
]
}
Potential Gotchas
Networking Considerations
- External requests coming into the PHP container go through localhost. You can access your app via
localhost
from the host machine. - However, if a request is made internally between PHP files or to a database, it will need to be sent to the internal IP of the PHP-Apache container.
- To find the IPs of all containers, open the Docker tab in VScode, right-click on the php_network under Networks, and select Inspect. A file with the following content will open.
...
"Containers": {
"6233e066b7e6009a2d0798b28ef49551c4d33868fdf9fa88d23f312eb14e1310": {
"Name": "php-apache",
"EndpointID": "87436db8d9d1d2272ad6b607fa7fa8e1775dd1957ef66f4ac8a323e306f27d0d",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
},
"a2eec094259970f19db6cf8c79da036d4d3dc7c4a02d43e606b12eb7a42c7a70": {
"Name": "php-admin",
"EndpointID": "5ba4e4b6294a453c9a22c0b3a469b9521d01fb2a8e2c9365798b0960ee676e1f",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
},
"b0697f319c99af352817141618592c4e20df9e8b909954e7dafd266a98d8ff8f": {
"Name": "db",
"EndpointID": "6ea7e802f147e1b3c738f0c1603847c43282dbd4064ad55491b37db44124cbed",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
...
Editing php.ini
To edit the php.ini
file, follow these steps:
- Open up the Docker tab in VScode.
- Right-click on the container and select Attach Shell.
- The console along the bottom will now be attached to the container.
In this attached shell, navigate to the location of the php.ini
file and make your edits as needed.
Viewing Server Information with phpinfo()
Create a new file called phpinfo.php
in your project's HTML directory and add the following code:
<?php phpinfo();
Then, navigate to http://localhost/phpinfo.php in your web browser to view the PHP server information.
That's it! If you have anything to add, or have a question, feel free to leave a comment.