Sign inSign up

44934045/mariadb_alpine

By 44934045

Updated 5 months ago

MariaDB 11.4.10 on Alpine Linux

Image
Databases & storage
0

1.4K

44934045/mariadb_alpine repository overview

MariaDB on Alpine Linux — Docker Image

A lightweight MariaDB Docker image built on top of Alpine Linux, inspired by yobasystems/alpine-mariadb.
For the official MariaDB image maintained by the MariaDB developer community, see hub.docker.com/_/mariadb.

Compatible with 32-bit SOC boards, such as Raspberry Pi 3, Orange Pi PC, etc.


Overview

ComponentVersion
Alpine Linuxlatest (≥ 3.23.4)
MariaDBlatest (≥ 11.4.10)

Key characteristics:

  • Runs under the unprivileged mysql user (uid=1000, gid=1000 by default — both adjustable via environment variables).
  • Minimal footprint: only what is needed to run MariaDB is included.
  • Managed by dumb-init to ensure correct signal handling and graceful shutdown.
  • Automatically executes initialization scripts on first boot (see Initialization Scripts).

Volumes

Mount pathPurpose
/var/lib/mysqlDatabase data files
/etc/my.cnf.dConfiguration files and X.509 certificates

Environment Variables

VariableRequiredDefaultDescription
MYSQL_ROOT_PASSWORDNo(random)Root password. If omitted on a fresh install, a random password is generated and printed to the log.
MYSQL_DATABASENoName of the database to create on first boot.
MYSQL_USERNoApplication user granted full access to MYSQL_DATABASE. Requires MYSQL_DATABASE to be set.
MYSQL_PASSWORDNoPassword for MYSQL_USER.
MYSQL_CHARSETNoutf8Default character set for the created database.
MYSQL_COLLATIONNoutf8_general_ciDefault collation for the created database.
PUIDNo1000UID to run the mysql service user as.
PGIDNo1000GID to run the mysql service user as.
TZNoUTCContainer timezone (e.g. America/Sao_Paulo).
VERBOSENo0Set to 1 for more detailed startup logs.

Note: MYSQL_USER is only created when MYSQL_DATABASE is also defined.
All variables that affect database creation are evaluated only on the first boot, when /var/lib/mysql/mysql does not yet exist.


Initialization Scripts

On the first boot only, any files placed in /docker-entrypoint-initdb.d/ are automatically executed after the initial database setup, in alphabetical order. Supported file types:

ExtensionBehaviour
.shExecuted as a shell script. Has full access to all container environment variables.
.sqlImported directly into MariaDB.
.sql.gzDecompressed on the fly and imported into MariaDB.

Files with any other extension are silently ignored.

How it works internally:

  1. After the bootstrap phase, a temporary MariaDB instance is started on a local Unix socket (no TCP port is exposed).
  2. The container waits until the server is ready to accept connections (up to 30 seconds).
  3. Each file in /docker-entrypoint-initdb.d/ is processed in order.
  4. The temporary server is shut down gracefully before the final MariaDB process starts.

If MYSQL_DATABASE is defined, SQL scripts are executed with that database pre-selected — useful for dumps that do not contain a USE <database>; statement.

Example — mounting an init directory:

docker run -d \
  --name mariadb \
  --stop-timeout 60 \
  -e MYSQL_ROOT_PASSWORD="Hard2Gue$$Password" \
  -e MYSQL_DATABASE=myapp \
  -e MYSQL_USER=myuser \
  -e MYSQL_PASSWORD=mypassword \
  -v /your_data_dir:/var/lib/mysql \
  -v /your_init_dir:/docker-entrypoint-initdb.d \
  -p 3306:3306 \
  44934045/mariadb_alpine

Files inside /docker-entrypoint-initdb.d/ are only executed once.
On subsequent container starts the directory is ignored because the data volume already exists.


Usage

Creating a New Instance
docker run -d \
  --name mariadb \
  --stop-timeout 60 \
  -e VERBOSE=1 \
  -e MYSQL_ROOT_PASSWORD="Hard2Gue$$Password" \
  -e MYSQL_DATABASE=myapp \
  -e MYSQL_USER=myuser \
  -e MYSQL_PASSWORD=mypassword \
  -v /your_data_dir:/var/lib/mysql \
  -p 3306:3306 \
  44934045/mariadb_alpine
Running with an Existing Database
docker run -d \
  --name mariadb \
  --stop-timeout 60 \
  -e VERBOSE=1 \
  -v /your_config_dir:/etc/my.cnf.d \
  -v /your_data_dir:/var/lib/mysql \
  -p 3306:3306 \
  44934045/mariadb_alpine
Interactive MariaDB Shell
docker exec -it mariadb mariadb -u root -p
Copy Default Configuration Files from the Container
docker cp mariadb:/etc/my.cnf.d/* ./config/

Docker Compose

services:
  mariadb:
    image: 44934045/mariadb_alpine
    container_name: mariadb
    restart: unless-stopped
    stop_grace_period: 60s        # required for clean InnoDB shutdown
    ports:
      - "127.0.0.1:3306:3306"
    volumes:
      - db-data:/var/lib/mysql
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    environment:
      MYSQL_ROOT_PASSWORD: yourpassword
      MYSQL_DATABASE: myapp
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypassword
      TZ: America/Sao_Paulo

volumes:
  db-data:

Important: stop_grace_period: 60s is required to allow InnoDB to flush data to disk before the container is forcibly killed. Without it, Docker's default 10-second timeout may cause an abrupt shutdown, resulting in a crash recovery (Recovering after a crash using tc.log) on the next startup. This setting cannot be embedded in the image itself and must always be defined at the orchestration level.


Kubernetes / Pod Deployment

apiVersion: v1
kind: Pod
metadata:
  name: mariadb
  namespace: default
  labels:
    app: mariadb
    purpose: database
spec:
  terminationGracePeriodSeconds: 60   # required for clean InnoDB shutdown
  volumes:
    - name: maria-conf
      hostPath:
        path: /data/mariadb/conf
    - name: maria-data
      hostPath:
        path: /data/mariadb/db
  containers:
    - name: mariadb
      image: 44934045/mariadb_alpine
      env:
        - name: VERBOSE
          value: "1"
        - name: MYSQL_ROOT_PASSWORD
          value: "Hard2Gue$$Password"
        - name: MYSQL_DATABASE
          value: myapp
        - name: MYSQL_USER
          value: myuser
        - name: MYSQL_PASSWORD
          value: mypassword
      volumeMounts:
        - name: maria-conf
          mountPath: /etc/my.cnf.d
        - name: maria-data
          mountPath: /var/lib/mysql
      ports:
        - containerPort: 3306
          protocol: TCP

TLS / Encrypted Connections

Copy your certificate and key files into the configuration volume (e.g. /data/mariadb/conf) and add the following to a file named server.cnf in that directory:

[server]
ssl=on
ssl-ca=/etc/my.cnf.d/ca-chain.pem
ssl-cert=/etc/my.cnf.d/server.crt
ssl-key=/etc/my.cnf.d/server.key

Connect a client over TLS:

mariadb -h <host> --ssl -u root -p

Verify the connection and cipher in use:

MariaDB [(none)]> STATUS;
-- Look for a line such as:
-- SSL: Cipher in use is TLS_AES_256_GCM_SHA384

For more information, refer to the MariaDB Secure Connections documentation.


Backup

Using mariadb-dump
export MYSQL_ROOT_PASSWORD="Hard2Gue$$Password"

docker exec mariadb sh -c \
  "exec mariadb-dump --all-databases -uroot -p\"$MYSQL_ROOT_PASSWORD\"" \
  > /backup/all_databases.sql

To back up a single database:

docker exec mariadb sh -c \
  "exec mariadb-dump myapp -uroot -p\"$MYSQL_ROOT_PASSWORD\"" \
  > /backup/myapp.sql

Restore

export MYSQL_ROOT_PASSWORD="Hard2Gue$$Password"

docker exec -i mariadb sh -c \
  "exec mariadb -uroot -p\"$MYSQL_ROOT_PASSWORD\"" \
  < /backup/all_databases.sql

Upgrade

Upgrading between major MariaDB versions can be complex. Always read the official upgrade documentation first.

As a general guideline:

  1. Back up all databases (see Backup).
  2. Pull the new image version.
  3. Start the container pointing to the existing data volume.
  4. Run mariadb-upgrade to update the system tables:
export MYSQL_ROOT_PASSWORD="Hard2Gue$$Password"

docker exec -it mariadb sh -c \
  "exec mariadb-upgrade -uroot -p\"$MYSQL_ROOT_PASSWORD\""

License

This project is provided as-is under the GNU GENERAL PUBLIC LICENSE.

Tag summary

Content type

Image

Digest

sha256:ed203a82d

Size

78.4 MB

Last updated

5 months ago

docker pull 44934045/mariadb_alpine