Skip to content
Accueil » Install PHPMyAdmin for MySQL under Docker with docker-compose

Install PHPMyAdmin for MySQL under Docker with docker-compose

If you are a fervent user of PHP and you use MySQL for your databases, you certainly know PHPMyAdmin, an application allowing you to manage them, avoiding using a terminal to connect to them and to do everything hand. Generally, PHPMyAdmin is automatically installed under WAMP/LAMP/XAMP, as well as on most PHP hosts (OVH, LWS, 1&1…)

However, how to proceed when you have initialized a project under Docker? This is what we are going to see together now:

Writing the docker-compose

Whether you use Docker or docker-compose, it’s the same, we’ll have to declare to the system that we want to use PHPMyadmin. There is just an official image on the docker HUB, available here: https://hub.docker.com/r/phpmyadmin/phpmyadmin/

If you don’t know how to write a docker-compose, and/or you want to know more about the subject, I strongly urge you to read this article, which will describe step by step how to configure a WEB site (PHP- Apache-Mysql) with docker-compose.

For now, let’s write a simple docker-compose.yml file that contains our MySQL container:

version: "3.9"
services:
  mysql:
    image: mysql:latest
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "super-secret-password"
      MYSQL_DATABASE: "my-wonderful-website"
    volumes:
      - dbData:/var/lib/mysqlvolumes:
  dbData:

Well, until then, nothing rocket science, we have a container which contains MySQL, as well as an associated volume, to allow the data not to be erased when this container is extinguished.

We are now going to add PHPMyAdmin, to avoid constantly connecting to the MySQL container via the terminal, which will be much more practical for managing our databases:

version: "3.9"
services:
  mysql:
    image: mysql:latest
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "super-secret-password"
      MYSQL_DATABASE: "my-wonderful-website"
    volumes:
      - dbData:/var/lib/mysql
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      PMA_HOST: mysql // same name as mysql service name
      PMA_PORT: 3306
    restart: always
    ports:
      - 8081:80
volumes:
  dbData:

And that’s all. What have we done here? We have created a service which, in order:

  • gets phpmyadmin image from docker HUB
  • contains two environment variables for its proper functioning:
    • the HOST is “mysql”, which corresponds to the service of the same name, allowing the application to be based on this database server
    • PORT is 3306
  • restarts each time the service shuts down (restart: always) for one reason or another (crash, out of RAM…)
  • is accessible from port 8081 of your PC, and is available on port 80 inside the container. It is not advisable to make PHPMyAdmin accessible from port 80 of your PC, the latter being generally reserved for your main application/website.

Access and use

We did the bare minimum to access PHPMyAdmin on our machine. It wasn’t very long, was it? If you have followed correctly, we will be able to access it from our port 8081. Let’s first launch the containers:

$ docker-compose up

Once the images are downloaded and the containers are on the way, go to http://localhost:8081

If everything worked well, you should see the PHPMyAdmin homepage appear:

The application therefore naturally asks you for the user and password to access your database, on the mysql server that you have declared in the PMA_HOST environment variable. So enter with the user “root” and his password, defined in mysql, namely the variable “MYSQL_ROOT_PASSWORD” (in our example “super-secret-password”). So you arrive on the home page of PHPMyAdmin:

Note that we can confirm the correct connection with our MySQL server, the database created at the start of the containers that we defined in our docker-compose file in the variable MYSQL_DATABASE (my-wonderful-website), is available and accessible in the left menu.

It’s all good, you can now access your databases as you wish!

Access without authentication

If you are used to working on a local environment, you may find it tedious to constantly have to enter your password on PHPMyAdmin (personally, during my developments, I often use private browsing for stories of cache, and keeps me logging out of the app).

Fortunately, it is possible in the docker-compose file to provide PHPMyAdmin with additional environment variables, which will allow you to simply skip this step. So you can do the following:

version: "3.9"
services:
  mysql:
    image: mysql:latest
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "super-secret-password"
      MYSQL_DATABASE: "my-wonderful-website"
    volumes:
      - dbData:/var/lib/mysql
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      PMA_HOST: mysql
      PMA_USER: "root"
      PMA_PASSWORD: "$MYSQL_ROOT_PASSWORD"
      PMA_PORT: 3306
    restart: always
    ports:
      - 8081:80
volumes:
  dbData:

With the two variables PMA_USER and PMA_PASSWORD configured, you can directly access your preferred database manager without having to authenticate yourself.

Convenient, isn’t it?

2 thoughts on “Install PHPMyAdmin for MySQL under Docker with docker-compose”

  1. Firstly thank you very much for the tut … But I could not log into phpmyamdin until I removed parentheses for MYSQL_ROOT_PASSWORD in docker-compose.yml … (tried any things … this worked)
    This didn’t work:
    MYSQL_ROOT_PASSWORD: “my-new-pass”
    MYSQL_DATABASE: “my-database”

    This works:
    MYSQL_ROOT_PASSWORD: my-new-pass
    MYSQL_DATABASE: my-database

    But I’m still not able to set it up for access without authentication. I’m on Windows 10, usting latest Docker version.
    Any clues why am I having such issues?

    1. Hello Andres, thank you for your comment.

      – First, I think yes, for parenthesis, it can depend on which version of docker/docker-compose you use, and being on windows can also affect the way it reads the yaml files. That’s a good news you achieved to make it work, I do not work on windows so I did not face this issue, but it will sure be useful for people who will read this article with Windows 🙂
      – Then, for using phpmyadmin without manual authentication, docker-compose file needs this:

      environment:
      PMA_HOST: "mysql"
      PMA_USER: "$MYSQL_USER"
      PMA_PASSWORD: "$MYSQL_PASSWORD"

      Maybe on windows it also needs your parenthesis to be adapted?
      I suggest you to start replacing those values with raw data, corresponding to your real values, and then tweak them with the parenthesis to try to make it work?

      I’ll try also on my side with a Windows virtual machine to test it, and I’ll keep you updated here if I get something new.

Leave a Reply

Your email address will not be published. Required fields are marked *