How to use a Mysql instance via phpMyAdmin using docker compose in Windows

Some months ago I needed to restore a sensitive database to run some import scripts. The backup was a Mysql database. So the idea was to spin a local Mysql instance and install a GUI to navigate through the data.
That annoyed me a bit, because it was a one-time job and then I will have my machine dirty. So I thought Docker could have been a good fit.

I followed the great post wrote by Luiz Felipe F M Costa “How to install MySQL and phpMyAdmin with Docker” to get up and running the basic idea.

His solution was great and worked fine, but it required multiple docker run commands and to create a network, which I found difficult to script.
So I found that was the right job for docker compose, that allows to create multi-container app.
Now I can commit the .yml file in GIT, and everyone can replicate my setup.

So my final solution is this docker-compose.yml file. Run it using docker-compose up -d and stop it using docker-compose down.

Some notes:

  • Navigate to http://localhost:8080/ to access the phpMyAdmin web interface. The default user is “root” and use the password specified in MYSQL_ROOT_PASSWORD.
  • I used Docker Desktop on Windows, WSL2 and Linux containers
  • Change the password as fit best to you. A improved step could be to use an environment file and remove it from the .yml file. In my specific case this was not a security problem.
  • The subfolder /data/ is required, and the database data will be mounted/bound here. This means also that you can start/shutdown the server without losing your data.
  • I increased the UPLOAD_LIMIT so you can restore also big backup files (up to 2GB)

Leave a comment if you found it useful, or just to share your thoughts!

version: "3.8"
services:
    mysql:
        image: mysql:8.0.12
        container_name: w3snet-mysql
        command: --default-authentication-plugin=mysql_native_password
        environment:
            - MYSQL_ROOT_PASSWORD=yourpassword
        networks:
            - w3snet
        volumes:
            - type: bind
              source: ./data
              target: /var/lib/mysql
        ports:
            - target: 3306
              published: 3306

# PMA_HOST is the IP or domain of the MySQL server, so we can use the MySQL container name as the domain
# because the Docker network create the route as a DNS server.
# more info on -e environment variables: https://github.com/phpmyadmin/docker#environment-variables-summary

    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        container_name: w3snet-phpmyadmin
        networks:
            - w3snet
        environment:
            - PMA_HOST=w3snet-mysql
            - UPLOAD_LIMIT=2048M
        ports:
            - target: 80
              published: 8080

networks:
  w3snet:

Add a Comment

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