Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
links:
- contendor_mysql
links: - contendor_mysql
  links: 
      - contendor_mysql

And it looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$conn = new mysqli('contenedor_mysql', 'test', 'testp');
$conn = new mysqli('contenedor_mysql', 'test', 'testp');
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo docker-compose up -d
sudo docker-compose up -d
sudo docker-compose up -d

Here is the code on GitHub.

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

0

Leave a Comment