Containerize MySQL and phpMyAdmin using Docker Containers

By bhagwatchouhan
Containerize MySQL and phpMyAdmin using Docker Containers

We can containerize our applications using Docker to have a separate installation of the required packages with the application-specific versions independent of the underlying operating system. We can use Docker Containers to make our application portable so that we can simply move it to another system having docker. This tutorial provides all the steps to containerize MySQL and phpMyAdmin using Docker containers.

 

Prerequisites

WindowsHow To Install WSL 2 (Windows Subsystem for Linux) with Ubuntu On Windows 10 and How To Install Docker Desktop On Windows 10. Optionally you may follow How To Change Docker Data Path On Windows 10.

Ubuntu - How To Install Docker Engine on Ubuntu 20.04 LTS

macOS - How To Install Docker Desktop On Mac

 

Install MySQL and phpMyAdmin 

In this step, we will configure and install MySQL and phpMyAdmin using the official images. Create the docker-compose.yml at the root of your project directory as shown below.

# docker-compose.yml
version: "3.8"
services:
  mysql:
    image: mysql:8.0.27
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: '<root-password>'
      MYSQL_DATABASE: helloworld
      MYSQL_USER: helloworld
      MYSQL_PASSWORD: '<db-password>'
    ports:
      - "3306:3306"
    volumes:
      - ./database:/var/lib/mysql
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: pma
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8085:80

Now, run the command docker-compose build to build the application.

# Build
cd <path to project>/helloworld
docker-compose build
# Output mysql uses an image, skipping phpmyadmin uses an image, skipping

We also need to run the command docker-compose up to launch the application as shown below. It will pull the MySQL and phpMyAdmin images and take time for the first time. The subsequent launches will be faster.

# Launch Application
docker-compose up
# Output Pulling mysql (mysql:8.0.27)... 8.0.27: Pulling from library/mysql ffbb094f4f9e: Pull complete df186527fc46: Pull complete fa362a6aa7bd: Pull complete 5af7cb1a200e: Pull complete .... .... Status: Downloaded newer image for mysql:8.0.27 Pulling phpmyadmin (phpmyadmin/phpmyadmin:)... latest: Pulling from phpmyadmin/phpmyadmin
69692152171a: Pull complete 2040822db325: Pull complete .... .... 2e982de2b8e5: Pull complete
Digest: sha256:382dedf6b43bf3b6c6c90f355b4dda660beb3e011de91bb3241170e54fca6119 Status: Downloaded newer image for phpmyadmin/phpmyadmin:latest Creating mysql ... done Creating pma ... done Attaching to mysql, pma .... ....

Now, try to access phpMyAdmin from the Browser using the URL http://localhost:8085. It should show the phpMyAdmin home page as shown in Fig 1.

Containerize MySQL and phpMyAdmin using Docker - phpMyAdmin

Fig 1

Now, login to phpMyAdmin using the username as root and the root password configured in the docker-compose.yml. Also, leave the server blank. It should show the phpMyAdmin home page as shown in Fig 2.

Containerize MySQL and phpMyAdmin using Docker - phpMyAdmin

Fig 2

We can press Ctrl + C to stop the containers.

# Press Ctrl + C
Gracefully stopping... (press Ctrl+C again to force)
Stopping pma   ... done
Stopping mysql ... done

We can also use MySQL Workbench installed locally using port 3306 to access the MySQL database running in the Docker container.

 

Summary

This tutorial provided all the steps to containerize MySQL and phpMyAdmin using Docker containers.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan