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?