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.

Docker Compose Container with Apache + PHP + MySQL + PHPMyAdmin + Python (XAMP with PHPMyAdmin with Docker)

Tiempo de lectura: 3 minutos

Reading time: 3 minutes

Continuing the tutorial series on Creating a Docker Compose Container with Apache + PHP + MySQL (XAMP with Docker), I’m going to show you how to create a Docker Compose file that includes Apache2, PHP, MySQL, Python, and PHPMyAdmin. Remember that you can use this code and containers for your projects, and they are based on the official image of each environment. At the end of this article, you will find a link to the code on GitHub.

To begin, let’s start with the Docker Compose from the previous tutorial where we had installed XAMP (upper link).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
version: "3"
# Specify the Docker Compose version we're using
# And then the services we're going to implement
services:
contendor_mysql:
image: mysql:5.7
container_name: mysql-container
environment:
MYSQL_DATABASE: db
# You can use any username, we're using "test" in the example
MYSQL_USER: test
# Password for the MySQL user
MYSQL_PASSWORD: testp
# Password for root
MYSQL_ROOT_PASSWORD: root
volumes:
# Mount a volume for MySQL to persist the database data
- ./mysql:/var/lib/mysql
expose:
- 3306
ports:
- 3306:3306
# Specify the desired name here
mi_servicio_apache:
# Specify the folder where we store the Dockerfile
build:
context: ./Dockerfile
# Specify the Dockerfile for this container
dockerfile: apache_file
container_name: apache-container
volumes:
# Folder where we'll 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:
# Our machine's port: Docker's port (80 for Apache or 443 for SSL)
- 80:80
links:
- contendor_mysql
version: "3" # Specify the Docker Compose version we're using # And then the services we're going to implement services: contendor_mysql: image: mysql:5.7 container_name: mysql-container environment: MYSQL_DATABASE: db # You can use any username, we're using "test" in the example MYSQL_USER: test # Password for the MySQL user MYSQL_PASSWORD: testp # Password for root MYSQL_ROOT_PASSWORD: root volumes: # Mount a volume for MySQL to persist the database data - ./mysql:/var/lib/mysql expose: - 3306 ports: - 3306:3306 # Specify the desired name here mi_servicio_apache: # Specify the folder where we store the Dockerfile build: context: ./Dockerfile # Specify the Dockerfile for this container dockerfile: apache_file container_name: apache-container volumes: # Folder where we'll 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: # Our machine's port: Docker's port (80 for Apache or 443 for SSL) - 80:80 links: - contendor_mysql
version: "3"
# Specify the Docker Compose version we're using
# And then the services we're going to implement
services:
  contendor_mysql:
    image: mysql:5.7
    container_name: mysql-container
    environment:
        MYSQL_DATABASE: db
          # You can use any username, we're using "test" in the example
        MYSQL_USER: test
          # Password for the MySQL user
        MYSQL_PASSWORD: testp
          # Password for root
        MYSQL_ROOT_PASSWORD: root
    volumes:
      # Mount a volume for MySQL to persist the database data
      - ./mysql:/var/lib/mysql
    expose:
      - 3306
    ports:
      - 3306:3306
# Specify the desired name here
  mi_servicio_apache:
    # Specify the folder where we store the Dockerfile
    build: 
      context: ./Dockerfile
      # Specify the Dockerfile for this container
      dockerfile: apache_file
    container_name: apache-container
    volumes:
      # Folder where we'll 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:
      # Our machine's port: Docker's port (80 for Apache or 443 for SSL)
      - 80:80
    links: 
      - contendor_mysql

With this folder structure:

Now let’s add the official container for PHPMyAdmin

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
miservicio_phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin-container
ports:
- 776:80
miservicio_phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin-container ports: - 776:80
 miservicio_phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin-container
    ports:
     - 776:80

This small container deploys PHPMyAdmin on port 776 of our machine.

Now let’s add this container to our docker-compose.yml file.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
version: "3"
# Specify the Docker Compose version we're using
# And then the services we're going to implement
services:
contendor_mysql:
image: mysql:5.7
container_name: mysql-container
environment:
MYSQL_DATABASE: db
# You can use any username, we're using "test" in the example
MYSQL_USER: test
# Password for the MySQL user
MYSQL_PASSWORD: testp
# Password for root
MYSQL_ROOT_PASSWORD: root
volumes:
# Mount a volume for MySQL to persist the database data
- ./mysql:/var/lib/mysql
expose:
- 3306
ports:
- 3306:3306
# Specify the desired name here
mi_servicio_apache:
# Specify the folder where we store the Dockerfile
build:
context: ./Dockerfile
# Specify the Dockerfile for this container
dockerfile: apache_file
container_name: apache-container
volumes:
# Folder where we'll 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:
# Our machine's port: Docker's port (80 for Apache or 443 for SSL)
- 80:80
links:
- contendor_mysql
contenedor_phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin-container
ports:
- 776:80
links:
- contendor_mysql:db
version: "3" # Specify the Docker Compose version we're using # And then the services we're going to implement services: contendor_mysql: image: mysql:5.7 container_name: mysql-container environment: MYSQL_DATABASE: db # You can use any username, we're using "test" in the example MYSQL_USER: test # Password for the MySQL user MYSQL_PASSWORD: testp # Password for root MYSQL_ROOT_PASSWORD: root volumes: # Mount a volume for MySQL to persist the database data - ./mysql:/var/lib/mysql expose: - 3306 ports: - 3306:3306 # Specify the desired name here mi_servicio_apache: # Specify the folder where we store the Dockerfile build: context: ./Dockerfile # Specify the Dockerfile for this container dockerfile: apache_file container_name: apache-container volumes: # Folder where we'll 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: # Our machine's port: Docker's port (80 for Apache or 443 for SSL) - 80:80 links: - contendor_mysql contenedor_phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin-container ports: - 776:80 links: - contendor_mysql:db
version: "3"
# Specify the Docker Compose version we're using
# And then the services we're going to implement
services:
  contendor_mysql:
    image: mysql:5.7
    container_name: mysql-container
    environment:
        MYSQL_DATABASE: db
          # You can use any username, we're using "test" in the example
        MYSQL_USER: test
          # Password for the MySQL user
        MYSQL_PASSWORD: testp
          # Password for root
        MYSQL_ROOT_PASSWORD: root
    volumes:
      # Mount a volume for MySQL to persist the database data
      - ./mysql:/var/lib/mysql
    expose:
      - 3306
    ports:
      - 3306:3306
# Specify the desired name here
  mi_servicio_apache:
    # Specify the folder where we store the Dockerfile
    build: 
      context: ./Dockerfile
      # Specify the Dockerfile for this container
      dockerfile: apache_file
    container_name: apache-container
    volumes:
      # Folder where we'll 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:
      # Our machine's port: Docker's port (80 for Apache or 443 for SSL)
      - 80:80
    links: 
      - contendor_mysql
contenedor_phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin-container
ports:
- 776:80
links:
- contendor_mysql:db

I have added contenedor_phpmyadmin at the end of the file and created the link with the database, referencing “db” (internal variable of the container).

NOTE: There is a typo in the code (contenedor_mysql is contendor_mysql)

To verify that it works, run the following command:

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

To verify that it works, visit http://localhost:776

To log in, you can use the user created in the Docker container:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MYSQL_DATABASE: db
# You can use any username, we're using "test" in the example
MYSQL_USER: test
# Password for the MySQL user
MYSQL_PASSWORD: testp
# Password for root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: db # You can use any username, we're using "test" in the example MYSQL_USER: test # Password for the MySQL user MYSQL_PASSWORD: testp # Password for root MYSQL_ROOT_PASSWORD: root
 MYSQL_DATABASE: db
          # You can use any username, we're using "test" in the example
        MYSQL_USER: test
          # Password for the MySQL user
        MYSQL_PASSWORD: testp
          # Password for root
        MYSQL_ROOT_PASSWORD: root

And here’s the code on GitHub for you to run.

0

Leave a Comment