Docker and Microsoft Azure
This process shows how to deploy MariaDB in a Docker container running on an Azure VM instance. First we'll create the Azure 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 Azure
1. Install MariaDB client on your local machine, either bundled with Maria DB server or standalone.
2. Login to Azure, navigate to Azure Virtual Machine
3. Create VM. Give the VM a name (e.g. mrdb-ubuntu-docker-use1), and create new or use an existing resource group. Selection region and availability zone, and choose Ubuntu 22.04 LTS x64 (free services eligible).
4. Choose the VM instance size, like a B1s or similar free tier. Note that Azure free works on a credit based system for new accounts
5. Configure an administrator account and generate a new key pair, and give the key pair a name.
6. Click "Review + Create" at the very bottom of the "create virtual machine" page to create the VM.
7. Download the SSH keys and them in a safe place, you will need them later.
8. Once the VM is Lloyd
Install Docker on the Azure VM
For more detailed instructions, refer to Installing and Using MariaDB via Docker
14. Back in the instance summary (EC2 > Instances > Instance ID), copy the public IP (e.g. ww.xx.yyy.zzz)
15. Open terminal window, navigate to the directory with private key (*.pem or *.ppk) file and start a SSH remote shell session by typing:
$ ssh -i mrdb-docker-pk.pem [email protected]
(switch ww.xx.yyy.zzz for your IP address from step 14).
16. Are you sure you want to continue connecting (yes/no/[fingerprint])? Say yes
17. Escalate to root
$ sudo su
18. Install Docker
$ curl -fsSL https://get.docker.com | sudo sh
Pull the MariaDB Docker image and create the container
19. Pull MariaDB Docker image
$ docker pull mariadb:lts
20. Start MDRB docker process
at your terminal / command line, type:
$ 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
The -v flag mounts a directory that you choose as /var/lib/mysql will ensure that the volume is persistent. Windows file paths like C:\Users\YouUID\Documents\YourDirName should be represented as above. Linux file paths should also be absolute vs. relative. Obviously replace the root password with something that is a bit more secure than you see in this example for anything other than development purposes.
21. Shell into container
$ docker exec -it mariadb-docker bash
22. Login to MRDB inside container
Using the root password specified in step 20, type:
$ mariadb -pyoursecurepassword
23. Setup admin account with permission for remote connection, configure access control
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.
24. Setup service account for your app with permission for remote connection, configure access control
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.
25. Load up your database from your preexisting SQL script that contains CREATE DATABASE; USE DATABASE; and CREATE TABLE statements.
In a new local terminal window, not your SSH session, change directory to the directory containing your database creation script, say, init.sql in this example. Type:
$ mariadb --host=ww.xx.yyy.zzz --port=3306 --user=admin --password=admin -e “SOURCE init.sql”
(switch ww.xx.yyy.zzz for your IP address from step 14).