By default, Docker Compose uses port 8080 for phpMyAdmin, but this can be customized in the docker-compose.yml
file.
Key takeaways:
Docker Compose simplifies the management of multi-container applications by defining and running multiple containers through a single YAML configuration file, ensuring consistency and streamlining complex setups.
Docker Compose reduces manual configuration overhead and enhances efficiency for environments such as development, testing, or production by enabling seamless interaction between containers like MySQL and phpMyAdmin.
Docker Compose simplifies managing multi-container Docker applications. It allows us to define and run multiple Docker containers using a single YAML configuration file. This streamlines the setup of complex environments, ensuring consistent configuration and enabling seamless interaction between containers. Whether for development, testing, or production, Docker Compose enhances efficiency and reduces manual configuration overhead.
We’ll execute Docker Compose for MySQL along with phpMyAdmin. A dedicated configuration file is essential to facilitate the integration of phpMyAdmin and MySQL within Docker Compose. Below is the specified compose file we’ll be utilizing for this setup:
version: '3'services:mysql:image: mysql:latestcontainer_name: dev_mysqlrestart: alwaysports:# <Port exposed> : < MySQL Port running inside container>- "3306:3306"volumes:- ./data/db:/var/lib/mysqlenvironment:MYSQL_DATABASE: "db"MYSQL_USER: "user"MYSQL_PASSWORD: "password"MYSQL_ROOT_PASSWORD: "root"phpmyadmin:image: phpmyadmin/phpmyadmincontainer_name: "dev_phpmyadmin"restart: alwaysenvironment:PMA_HOST: db # Use the service name 'db' as the hostnamePMA_PORT: 3306PMA_USER: userPMA_PASSWORD: passwordports:- "8080:80"
Certainly! Here’s an explanation for each line in the docker-compose.yml
file:
The following line specifies the version of the Docker Compose file format being used. In this case, version 3.1:
version: '3.1'
In this section, we will define services that we will use to deploy the images of phpMyAdmin
and mysql
:
services:
The service below defines a service which we named mysql
using the mysql
Docker image with the latest version. It ensures that the container restarts automatically, sets environment variables for MySQL configuration (root password, database name, user, and password), and establishes a volume (data) for persistent storage of MySQL data in the /var/lib/mysql
directory.
mysql:image: mysql:latestcontainer_name: dev_mysqlrestart: alwaysports:# <Port exposed> : < MySQL Port running inside container>- "3306:3306"volumes:- ./data/db:/var/lib/mysqlenvironment:MYSQL_DATABASE: "db"MYSQL_USER: "user"MYSQL_PASSWORD: "password"MYSQL_ROOT_PASSWORD: "root"
This service defines a service named phpmyadmin
using the phpMyAdmin Docker image. It ensures the container restarts automatically, sets environment variables for phpMyAdmin configuration (host, user, and password), and maps port 8080
on the host to port 80
on the container. The environment variable (PMA_HOST
, PMA_PORT
, PMA_USER
and PMA_PASSWORD
) uses the service name db
as the hostname, establishing a connection between phpmyadmin
and the mysql
service.
phpmyadmin:image: phpmyadmin/phpmyadmincontainer_name: "dev_phpmyadmin"restart: alwaysenvironment:PMA_HOST: db # Use the service name 'db' as the hostnamePMA_PORT: 3306PMA_USER: userPMA_PASSWORD: passwordports:- "8080:80"
Below is the executable that will run PhpMyAdmin and MySQL in Docker Compose.
After clicking the “RUN” button, the environment will begin configuration. Subsequently, you can execute the following command to initiate the execution of both containers:
docker-compose up -d
This command will start the Docker Compose-defined services and containers based on the configurations specified in the docker-compose.yml
file.
version: '3.3' services: db: image: mysql:latest container_name: mysql-db restart: always environment: MYSQL_ROOT_PASSWORD: examplepassword MYSQL_DATABASE: exampledb MYSQL_USER: exampleuser MYSQL_PASSWORD: examplepassword ports: - "3306:3306" volumes: - db_data:/var/lib/mysql phpmyadmin: image: phpmyadmin/phpmyadmin:latest container_name: phpmyadmin restart: always environment: PMA_HOST: db PMA_USER: exampleuser PMA_PASSWORD: examplepassword ports: - "8080:80" depends_on: - db volumes: db_data:
Once the containers are established, you can navigate to the provided link to observe the connection between phpMyAdmin and MySQL.
Haven’t found what you were looking for? Contact Us
Free Resources