Reading time: 3 minutes
Continuing with the series of Docker Compose tutorials, based on the previous tutorial “Associate a Docker file to have a custom image (Apache + PHP + Python) within Docker Compose”, we will now create a XAMPP or LAMPP service that includes Apache2, PHP, and MySQL. This will be achieved by leveraging Docker containers and linking them together using Docker Compose.
Using the base code, download the Github code
version: "3" # Indicate the Docker Compose version we are using # Then define the services we are going to implement services: # Specify the desired service name my_apache_service: # Indicate the folder where we save the Dockerfile build: context: ./Dockerfile # Specify the Dockerfile for this container dockerfile: apache_file # We remove this line as it 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 share it with other containers - 80 ports: # Port of our machine: port inside Docker (always 80 for Apache or 443 for SSL) - 80:80
Let’s add the MySQL container using mysql:5.7
mysql_container: image: mysql:5.7 container_name: mysql-container environment: MYSQL_DATABASE: db # You can use any username you want; in this example, we use "test" MYSQL_USER: test # Password for the MySQL user MYSQL_PASSWORD: testp # Root password MYSQL_ROOT_PASSWORD: root volumes: # Mount a volume for MySQL to persist the database data - ./mysql:/var/lib/mysql expose: - 3306 ports: - 3306:3306
We create the container, exposing the default MySQL port 3306.
We also create a volume called “mysql” where we can add our initial database if we have one. This container is capable of loading it automatically.
Now let’s join the two containers within the same Docker Compose file, starting with MySQL.
version: "3" services: mysql_container: 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 my_apache_service: build: context: ./Dockerfile dockerfile: apache_file container_name: apache-container volumes: - ./www/:/var/www/html expose: - 80 ports: - 80:80 links: - mysql_container
Once the two containers are joined, we need to establish a link between the Apache container and MySQL. To do this, we add:
links: - mysql_container
The final configuration looks as follows:
version: "3" services: mysql_container: 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 my_apache_service: build: context: ./DockerfileDockerfile dockerfile: apache_file container_name: apache-container volumes: - ./www/:/var/www/html expose: - 80 ports: - 80:80 links: - mysql_container
NOTE: We have now created a link to the MySQL container, which we named "mysql_container". This means that when we establish a connection in PHP with MySQL, we will need to use "mysql_container" instead of "localhost" (which is the host specified in our connection). In other words, the connection would be as follows:
$conn = new mysqli('mysql_container', 'test', 'testp');
Now everything is ready for deployment. The folder structure looks like this:
And now we can deploy our Dockerized XAMPP.
sudo docker-compose up -d
Download the code HERE from GitHub.
If you are looking for second-hand books, I recommend www.quierolibros.com.