How to run Docker Compose for MySQL with phpMyAdmin

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:latest
container_name: dev_mysql
restart: always
ports:
# <Port exposed> : < MySQL Port running inside container>
- "3306:3306"
volumes:
- ./data/db:/var/lib/mysql
environment:
MYSQL_DATABASE: "db"
MYSQL_USER: "user"
MYSQL_PASSWORD: "password"
MYSQL_ROOT_PASSWORD: "root"
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: "dev_phpmyadmin"
restart: always
environment:
PMA_HOST: db # Use the service name 'db' as the hostname
PMA_PORT: 3306
PMA_USER: user
PMA_PASSWORD: password
ports:
- "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'
Version of docker compose
  • In this section, we will define services that we will use to deploy the images of phpMyAdmin and mysql:

services:
Service to deploy images
  • 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:latest
container_name: dev_mysql
restart: always
ports:
# <Port exposed> : < MySQL Port running inside container>
- "3306:3306"
volumes:
- ./data/db:/var/lib/mysql
environment:
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/phpmyadmin
container_name: "dev_phpmyadmin"
restart: always
environment:
PMA_HOST: db # Use the service name 'db' as the hostname
PMA_PORT: 3306
PMA_USER: user
PMA_PASSWORD: password
ports:
- "8080:80"

Below is the executable that will run PhpMyAdmin and MySQL in Docker Compose.

Test yourself

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
Command to start and run Docker containers defined in the docker-compose.yml file

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:
Running MySQL and phpMyAdmin in Docker Compose

Once the containers are established, you can navigate to the provided link to observe the connection between phpMyAdmin and MySQL.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What port does Docker compose use for phpMyAdmin?

By default, Docker Compose uses port 8080 for phpMyAdmin, but this can be customized in the docker-compose.yml file.


Can I use phpMyAdmin for MySQL?

Yes, phpMyAdmin can be used to manage MySQL databases via a web interface.


Which is better phpMyAdmin or MySQL?

phpMyAdmin is a web-based tool for managing MySQL databases, while MySQL is the database system itself. They serve different purposes and are often used together. The choice depends on whether you need a graphical interface (phpMyAdmin) or a database management system (MySQL).


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved