Docker and Google Cloud

This process shows how to deploy MariaDB in a Docker container running on an GCE instance. First we'll create the GCE VM, then we'll deploy Docker to it. After that, we'll pull the MariaDB Docker image which we'll use to create a running container with a MariaDB instance. Finally, we'll load a sample database into the MariaDB instance.

Create a VM in Google Cloud Compute Engine

  1. Install MariaDB client on your local machine, either bundled with Maria DB server or standalone.

  2. Login to Google Cloud, navigate to VM instances

  3. Enable Compute Engine API if you haven’t already.

  4. Click create instance, give instance a name (e.g. mrdb-ubuntu-docker-use1b), choose a region and zone.

  5. Machine configuration: Choose general-purpose / E2 micro

gcp-e2
gcp-e2-micro
  1. Boot Disk > Change

Switch the operating system to a modern Ubuntu release x86/64 CPU architecture, or similar free tier offering.

  1. Create a firewall rule in the Firewall Policies section of the console. After naming it, change the targets, add 0.0.0.0/0 as a source IP range, and open TCP port 3306. Then Click create.

  2. Connect using Google Cloud’s built in browser SSH. Accept all prompts for authorization.

Install Docker on the GCE VM

For more detailed instructions, refer to Installing and Using MariaDB via Docker

  1. Escalate to root Escalate to root

$ sudo su
  1. Install Docker

$ curl -fsSL get.docker.com | sudo sh
  1. Pull Docker image

$ docker pull mariadb:lts
  1. Start MDRB docker process at your terminal / command line.

$ docker run --detach --name mariadb-docker -v \Users\YouUID\Documents\YourDirName:/var/lib/mysql:Z -p 3306:3306 -e MARIADB_ROOT_PASSWORD=yoursecurepassword mariadb:lts

Mounting a Directory for Persistent MySQL Storage

When using the -v flag to mount a directory as /var/lib/mysql, you ensure that your MySQL data is stored persistently.

File Path Instructions:

  • Windows: Use a file path format similar to C:\Users\YourUsername\Documents\YourDirectory.

  • Linux: Always use absolute paths instead of relative ones.

  1. Shell into container

    docker exec -it mariadb-docker bash
  2. Login to MRDB inside container using the root password specified in step 12.

$ mariadb -pyoursecurepassword
  1. Setup admin account with permission for remote connection, configure access control Execute these SQL commands in sequence:

MariaDB [(none)]> CREATE USER 'admin'@'%' IDENTIFIED BY 'admin';
MariaDB [(none)]> GRANT ALL ON _._ to 'admin'@'%' WITH GRANT OPTION;
MariaDB [(none)]> SHOW GRANTS FOR admin;

Obviously replace these passwords with something that is a bit more secure than you see in this example for anything other than development purposes.

  1. Setup service account for your app with permission for remote connection, configure access control Execute these SQL commands in sequence:

MariaDB [(none)]> CREATE USER 'yourappname'@'%' IDENTIFIED BY 'yoursecurepassword';
MariaDB [(none)]> GRANT INSERT, UPDATE, DELETE ON *.* TO 'yourappname'@'%';
MariaDB [(none)]> SHOW GRANTS FOR yourappname;

Obviously replace these passwords with something that is a bit more secure than you see in this example for anything other than development purposes.

  1. Load up your database from your preexisting SQL script that contains CREATE DATABASE; USE DATABASE; and CREATE TABLE statements.

Copy the external IP address of your VM instance from the Console in the VM instances list.

To run your database creation script using MariaDB, open a new terminal window and navigate to the directory containing the script, init.sql. Then, execute the following command, replacing ww.xx.yyy.zzz with your IP address:

$ mariadb --host=your_ip_address --port=3306 --user=admin --password=admin -e "SOURCE init.sql"

This page is licensed: CC BY-SA / Gnu FDL

Last updated

Was this helpful?