Deploy MariaDB Docker Hub image on Google Cloud

Quick Tip

When working with MariaDB on Docker containers you usually generate a personalized Image that contains all your customizations and normally is used on your local Docker installation for testing. If you want to use it and share it more widely you upload your image to the Docker Hub.

I want to test if our customized MariaDB image docker.io/patomx/patomariadb can be retrieved from the Docker Hub and deployed in a Virtual Machine Instance on the Compute Engine component of Google Cloud Platform. This will allow us to further deployment, for example in a Kubernetes cluster.

Create Virtual Machine Container

Create a new Virtual Machine with the following parameters:

On Google Cloud Platform - Compute Engine - VM Instances - Create Instance

  • Name: patomariadhub
  • Type: f1-micro (1 vCPU, 0.6 GB memory)
  • Container: Deploy a container image to this VM instance.
    • Container image: docker.io/patomx/patomariadb

or if you prefer to use Cloud Shell, generate the new instance like this:

$ gcloud config set compute/zone us-central1-a

gcloud compute instances create-with-container patomariadhub --container-image docker.io/patomx/patomariadb --machine-type=f1-micro
Created [https://www.googleapis.com/compute/v1/projects/personal-20202/zones/us-central1-a/instances/patomariadhub].
NAME           ZONE           MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP  STATUS
patomariadhub  us-central1-a  f1-micro                   10.128.0.19  34.69.91.49  RUNNING

Validate Container

Connect to your virtual machine and list your container:

pato@patomariadhub ~ $ docker ps
CONTAINER ID        IMAGE                                                                COMMAND                  CREATED             STATUS              PORTS               NAMES
8faf579a4623        patomx/patomariadb                                                   "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes                            klt-patomariadhub-xwgd

Then access your container and restore the safe backup of our image:

pato@patomariadhub ~ $ docker exec -it 8faf579a4623 bash

root@patomariadhub:~# ls /root/*.sql
initial-db.sql

root@7cb4db2c49a9:/# mysql -u root -p < /root/initial-db.sql
Enter password:

Connect remotely to this container and validate all our image users are present:

$ mysql -u remoto -p -h patomariadhub
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 10.4.13-MariaDB-1:10.4.13+maria~bionic mariadb.org binary distribution

MariaDB [(none)]> use mysql
Database changed

MariaDB [mysql]> select user, host, plugin from user;
+-------------+-----------+-----------------------+
| User        | Host      | plugin                |
+-------------+-----------+-----------------------+
| mariadb.sys | localhost | mysql_native_password |
| root        | localhost | mysql_native_password |
| pato        | localhost | unix_socket           |
| remoto      | %         | mysql_native_password |
| respalda    | localhost | mysql_native_password |
| replicador  | %         | mysql_native_password |
| maxpato     | %         | mysql_native_password |
+-------------+-----------+-----------------------+
7 rows in set (0.040 sec)

OK we have successfully deployed our MariaDB image into a Google Cloud Virtual Machine Container!