Create a Docker Compose Container with Apache + PHP + MySQL (XAMPP with Docker)

Tiempo de lectura: 4 minutos

Reading time: 4 minutes

Continuing with the series of tutorials on Docker Compose and based on the latest Associating a Docker file to have a customized image (Apache + PHP + Python) within Docker Compose, we are going to create an XAMPP or LAMPP service that includes Apache2, PHP, and MySQL, leveraging Docker containers and connecting them using Docker Compose.

Using the base code, download code from Github

version: "3"
#Indicate the Docker Compose version we are using
#Then specify the services we are going to implement
services:
#Specify the desired name
  mi_servicio_apache:
    #Indicate the folder where we save the Docker File
    build: 
      context: ./Dockerfile
      #Specify the Dockerfile for this container
      dockerfile: apache_file
    #Remove this line, as it is replaced by the dockerfile
    #image: php:7.0-apache
    container_name: apache-container
    volumes:
      #Folder where we store the web files: Docker internal folder
      - ./www/:/var/www/html
    expose:
      #Port we want to expose to share it with other containers
      - 80
    ports:
      # Port on our machine: port within Docker (always 80 for Apache or 443 for SSL)
      - 80:80

We are going to add the official MySQL container mysql:5.7

contendor_mysql:
    image: mysql:5.7
    container_name: mysql-container
    environment:
        MYSQL_DATABASE: db
          # You can use any username you want, in the 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 preserve database data
      - ./mysql:/var/lib/mysql
    expose:
      - 3306
    ports:
      - 3306:3306

Create the container exposing the default MySQL port 3306.

Create a volume named MySQL where we can add our initial database if we have one. This container is capable of loading it automatically.

Now let’s combine the two containers in the same Docker Compose file, starting with MySQL.

version: "3"
#Indicate the Docker Compose version we are using
#Then specify the services we are going to implement
services:
  contendor_mysql:
    image: mysql:5.7
    container_name: mysql-container
    environment:
        MYSQL_DATABASE: db
          # You can use any username you want, in the 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 preserve database data
      - ./mysql:/var/lib/mysql
    expose:
      - 3306
    ports:
      - 3306:3306
#Specify the desired name
  mi_servicio_apache:
    #Indicate the folder where we save the Docker File
    build: 
      context: ./Dockerfile
      #Specify the Dockerfile for this container
      dockerfile: apache_file
    #Remove this line, as it is replaced by the dockerfile
    #image: php:7.0-apache
    container_name: apache-container
    volumes:
      #Folder where we store the web files: Docker internal folder
      - ./www/:/var/www/html
    expose:
      #Port we want to expose to share it with other containers
      - 80
    ports:
      # Port on our machine: port within Docker (always 80 for Apache or 443 for SSL)
      - 80:80

Once the two containers are linked, we need to establish a link between the Apache container and MySQL. To do this, we will add:

  links: 
      - contendor_mysql

And it looks like this:

version: "3"
#Indicate the Docker Compose version we are using
#Then specify the services we are going to implement
services:
contendor_mysql:
image: mysql:5.7
container_name: mysql-container
environment:
MYSQL_DATABASE: db
# You can use any username you want, in the 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 preserve database data
- ./mysql:/var/lib/mysql
expose:
- 3306
ports:
- 3306:3306
#Specify the desired name
mi_servicio_apache:
#Indicate the folder where we save the Docker File
build:
context: ./Dockerfile
#Specify the Dockerfile for this container
dockerfile: apache_file
#Remove this line, as it is replaced by the dockerfile
#image: php:7.0-apache
container_name: apache-container
volumes:
#Folder where we store the web files: Docker internal folder
- ./www/:/var/www/html
expose:
#Port we want to expose to share it with other containers
- 80
ports:
# Port on our machine: port within Docker (always 80 for Apache or 443 for SSL)
- 80:80
links:
- contendor_mysql

NOTE: We have now created a link with the MySQL container that we named contendor_mysql. This means that when we make a connection to MySQL in PHP, we have to use contendor_mysql instead of localhost (where the host of our connection is specified). It would look like this:

$conn = new mysqli('contenedor_mysql', 'test', 'testp');

Now everything is ready for deployment.

The folder structure is as follows:

And now we can deploy our Dockerized XAMPP.

sudo docker-compose up -d

Here is the code on GitHub.

If you’re looking for second-hand books, I recommend www.quierolibros.com.

Leave a Comment