Continuing with the series of Docker Compose tutorials and based on the last one Associate a Docker file to have a custom image (Apache + PHP + Python) within Docker Compose we are going to create a XAMPP or LAMPP service that includes Apache2, PHP and MySQL taking advantage of Docker containers and linking them together using Docker Compose.

Using the base code, download Github code
version: "3"
#Indicate the docker compose version that we use
#And then the services we are going to implement
services:
#Here we indicate the name we want
mi_servicio_apache:
#Indicate the folder where we save the Docker File
build:
context: ./Dockerfile
#Indicate the dockerfile of this container
dockerfile: apache_file
# We remove this line that is replaced by the dockerfile
#image: php:7.0-apache
container_name: apache-container
volumes:
#Folder where we will save the web files: internal Docker folder
- ./www/:/var/www/html
expose:
#Port that we want to expose to so we can share it with other containers
- 80
ports:
# Port of our machine: port inside Docker (it will always be 80 for apache or 443 for SSL)
- 80:80
Let’s add the MySQL container of mysql:5.7
contendor_mysql:
image: mysql:5.7
container_name: mysql-container
environment:
MYSQL_DATABASE: db
# You can use the user you want, in the example we use test
MYSQL_USER: test
# Password for the MySQL user
MYSQL_PASSWORD: testp
# Password for root
MYSQL_ROOT_PASSWORD: root
volumes:
# We mount a volume for MySQL so as not to lose the db data
- ./mysql:/var/lib/mysql
expose:
- 3306
ports:
- 3306:3306
We create the container exposing the MySQL default port 3306.
We also create a volume that we call MySQL, it is a folder where we are going to add our initial database if we have it, this container is capable of loading it automatically.
Now we are going to join the two containers inside the same Docker Compose file, we are going to add MySQL first.
version: "3"
services:
contendor_mysql:
image: mysql:5.7
container_name: mysql-container
environment:
MYSQL_DATABASE: db
MYSQL_USER: test
MYSQL_PASSWORD: testp
MYSQL_ROOT_PASSWORD: root
volumes:
- ./mysql:/var/lib/mysql
expose:
- 3306
ports:
- 3306:3306
mi_servicio_apache:
build:
context: ./Dockerfile
dockerfile: apache_file
# We remove this line that is replaced by the dockerfile
#image:php:7.0-apache
container_name: apache-container
volumes:
#Folder where we will save the web files: internal Docker folder
- ./www/:/var/www/html
expose:
#Port that we want to expose to so we can share it with other containers
- 80
ports:
# Port of our machine: port inside Docker (it will always be 80 for apache or 443 for SSL)
- 80:80
Once the two containers are joined, we have to make a link between the apache container and MySQL, for this we are going to add
links:
- contendor_mysql
And it remains as follows:
version: "3"
services:
contendor_mysql:
image: mysql:5.7
container_name: mysql-container
environment:
MYSQL_DATABASE: db
MYSQL_USER: test
MYSQL_PASSWORD: testp
MYSQL_ROOT_PASSWORD: root
volumes:
- ./mysql:/var/lib/mysql
expose:
- 3306
ports:
- 3306:3306
mi_servicio_apache:
build:
context: ./Dockerfile
dockerfile: apache_file
container_name: apache-container
volumes:
- ./www/:/var/www/html
expose:
- 80
ports:
- 80:80
links:
- contendor_mysql
NOTE: now we have created a link to the MySQL container which we have named mysql_container. This means that when we make a connection in PHP with MySQL we will have to put container_mysql instead of localhost (where the host of our connection is indicated) that is, it would be as follows:
$conn = new mysqli('contenedor_mysql', 'test', 'testp');
Now we have everything ready to deploy. The folder structure looks like this:

And now we can deploy our Dockerized XAMP.
sudo docker-compose up -d

Download the code HERE from GitHub.
If you are looking for Second Hand books I recommend www.quierolibros.com
