User Tools

Site Tools


migrating_mariadb_between_hosts

This is an old revision of the document!


Docker + MariaDB Setup on Ubuntu 22.04

This guide documents the steps used to set up Docker CE and a MariaDB instance on a minimal VPS (1 vCPU, 1 GB RAM) running Ubuntu 22.04. It's designed to be simple and portable β€” ideal for DevOps-style projects, dashboards, and APIs that may later be migrated to a cloud platform like Azure Container Apps.

πŸ’‘ Why this approach?

The goal is to:

  • Make the environment reproducible and easy to redeploy
  • Avoid configuring software manually on the system (e.g., `apt install mariadb-server`)
  • Use Docker volumes to persist data independently of the container
  • Support CI/CD, local testing, and future migration to the cloud

This method works well even on small servers, and can easily be integrated into Terraform, Ansible, or CI workflows later on.

β€”

🐳 Script: Install Docker CE on Ubuntu

#!/bin/bash
# install-docker-ce.sh β€” Installs Docker Engine on Ubuntu 22.04+
 
set -e
 
sudo apt-get update
sudo apt-get install -y \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
 
# Add Docker’s official GPG key
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
    sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
 
# Add the repository
echo \
  "deb [arch=$(dpkg --print-architecture) \
  signed-by=/etc/apt/keyrings/docker.gpg] \
  https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
 
# Install Docker Engine
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
 
# Enable Docker service
sudo systemctl enable docker
sudo systemctl start docker
 
echo "Docker installed successfully!"

β€”

🐬 docker-compose.yml for MariaDB

This Compose file defines a MariaDB service with a persistent volume. The `restart: unless-stopped` line ensures it comes back after a reboot, unless stopped manually.

services:
  mariadb:
    image: mariadb:latest
    container_name: mariadb
    restart: unless-stopped
    environment:
      - MYSQL_ROOT_PASSWORD=your_root_pass
    ports:
      - "3306:3306"
    volumes:
      - mariadb_data:/var/lib/mysql

volumes:
  mariadb_data:
    external: true

β€”

πŸ“¦ Notes on Volume Migration and Backup

This setup stores your MariaDB data inside a Docker-managed volume (not inside the container). You can move this volume to another server like this:

1. Find the volume's path on the source server:

  <code bash>
  docker inspect mariadb | grep Source
  </code>

2. Stop the container, then tar the volume contents:

  <code bash>
  docker stop mariadb
  sudo tar -czvf mariadb_data.tar.gz /var/lib/docker/volumes/your_volume_name/_data
  </code>

3. Copy the `.tar.gz` to the new server:

  <code bash>
  scp mariadb_data.tar.gz user@newserver:~
  </code>

4. Extract into a Docker volume path (on the new server):

  <code bash>
  docker volume create mariadb_data
  sudo tar -xzvf mariadb_data.tar.gz -C /var/lib/docker/volumes/mariadb_data/_data
  </code>

5. Now `docker compose up -d` with the same `docker-compose.yml`, and your DB will be live.

β€”

βœ… Final Thoughts

This method allows you to:

  • Maintain a clean server environment
  • Version control your infra and DB layout via YAML
  • Restore or replicate the setup with minimal effort
  • Migrate between servers or to the cloud with ease

You can add your Flask API and Dashboard apps into the same Compose file, or deploy them independently. This is the foundation of a production-ready system built from tiny building blocks.

Enjoy!

migrating_mariadb_between_hosts.1744509374.txt.gz Β· Last modified: 2025/04/13 01:56 by oso