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).
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
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.
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:
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:
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.