Run MySQL and phpMyAdmin with Docker Compose

Introduction

This page explains how to run MySQL and phpMyAdmin with Docker.

MySQL is a popular open-source relational database management system (RDBMS) that is commonly used in web applications. It stores data in tables and uses Structured Query Language (SQL) to access and manipulate that data.

phpMyAdmin is a free and open-source web-based tool that is used to manage MySQL databases. It provides a user-friendly interface that allows users to create and manage databases, users, and privileges, as well as to execute SQL statements and manage data.

Let's use Docker to run both MySQL and phpMyAdmin by creating a docker-compose.yml file. First line of docker-compose.yml, let's specify the version :

version: '3'

Network

phpMyAdmin must reach the MySQL database. To avoid complex configurations, we will create a network that will be common to MySQL and phpMyAdmin: The mysql-phpmyadmin network uses the bridge to be reachable from the host machine:

networks:
    mysql-phpmyadmin:
        name: mysql-phpmyadmin
        # use the bridge driver
        driver: bridge

Volume

Here I want the database to be stored in the user's folder. To do this, let's create a volume mysqldata and specify that this volume is in host machine folder.

volumes:
  mysqldata:
    driver: local
    driver_opts:
      type: 'none'
      o: 'bind'
      device: '${HOME}/server/mysql-phpmyadmin/data'

MySQL

Now let's configure the MySQL container.

Here is the MySQL configuration:

services:
  mysql:
    image: mysql:8.0
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: database_name
      MYSQL_USER: user_name
      MYSQL_PASSWORD: user_password
    ports:
      - "6033:3306"
    volumes:
      - mysqldata:/var/lib/mysql
    networks:
      mysql-phpmyadmin:
        aliases:
          - mysql

phpMyAdmin

In the same way, let's now configure the container for phpMyAdmin:

Here is the configuration of phpMyAdmin :

    phpmyadmin:
        image: phpmyadmin:5.2.0
        container_name: phpmyadmin
        links:
            - mysql
        environment:
            PMA_HOST: mysql
            PMA_PORT: 3306
        ports:
            - 8081:80
        networks:
        mysql-phpmyadmin:
            aliases:
                - phpmyadmin

Docker Compose file

Here is the entire docker-compose.yml file:

version: '3'

networks:
    mysql-phpmyadmin:
        name: mysql-phpmyadmin
        # use the bridge driver
        driver: bridge

volumes:
    mysqldata:
        driver: local
        driver_opts:
            type: 'none'
            o: 'bind'
            device: '${HOME}/server/mysql-phpmyadmin/data'

services:
    mysql:
        image: mysql:8.0
        container_name: mysql
        environment:
            MYSQL_ROOT_PASSWORD: root_password
            MYSQL_DATABASE: database_name
            MYSQL_USER: user_name
            MYSQL_PASSWORD: user_password
        ports:
            - "6033:3306"
        volumes:
            - mysqldata:/var/lib/mysql
        networks:
            mysql-phpmyadmin:
                aliases:
                - mysql

    phpmyadmin:
        image: phpmyadmin:5.2.0
        container_name: phpmyadmin
        links:
            - mysql
        environment:
            PMA_HOST: mysql
            PMA_PORT: 3306
        ports:
            - 8081:80
        networks:
            mysql-phpmyadmin:
                aliases:
                - phpmyadmin

Launch the containers

To launch the containers, you just have to type the following command in the folder of the docker-compose.yml file:

docker compose up -d

The -d option allows to launch the container in the background. If everything goes well, the networks and the volume should be created, then the containers:

$ docker compose up -d
[+] Running 3/3
 ⠿ Network mysql-phpmyadmin  Created                                                              0.1s
 ⠿ Container mysql           Started                                                              0.6s
 ⠿ Container phpmyadmin      Started

The following command stops the containers:

docker compose down

phpMyAdmin

In your favorite browser, go to `http://localhost:8081/``

The phpMyAdmin home page should display:

phpMyAdmin exécuté depuis un container Docker avec Docker Compose

You have two options to connect to it:

These are obviously the identifiers specified in the docker-compose.yml file.

In the first case, you will be administrator of the MySQL database. In the second case, you will only be able to administrate the database_name database

MySQL in command line

From the host machine, you can also connect to the MySQL database from the command line:

To connect as root (password root_password)

mysql --host=127.0.0.1 --port=6033 -u root -p

As an administrator, you will be able to manage all databases:

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| database_name      |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0,01 sec)

Or as a user:

mysql --host=127.0.0.1 --port=6033 -u user_name -p

You will only have access to the databases to which your user has privileges:

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| database_name      |
| information_schema |
| performance_schema |
+--------------------+
3 rows in set (0,00 sec)

Download

You can download the complete file below. Note that you have to rename it docker-compose.yml to start the containers :

mysql-phpmyadmin-docker-compose.yml

See also


Last update : 03/17/2024