Run MariaDB in Docker with Compose and Persistent Storage
Run MariaDB in Docker with a deliberate image policy, persistent data and protected credentials. Verify the database connection path and recovery procedure instead of assuming a running container is enough.
Prepare persistent storage and a secret
Use the official MariaDB image with a reviewed version policy. Create a private secrets/db-root.txt file containing a strong deployment password, restrict its permissions and keep it out of source control. The file must exist before Compose starts; do not use a tutorial password on a live service.
The following Compose example tracks MariaDB’s LTS image channel. Record the resolved image version or digest for repeatable deployments and review upgrades before pulling a changed image:
services:
db:
image: mariadb:lts
restart: unless-stopped
environment:
MARIADB_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
secrets:
- db_root_password
volumes:
- db_data:/var/lib/mysql
secrets:
db_root_password:
file: ./secrets/db-root.txt
volumes:
db_data:
No host database port is published. A Compose file secret is supplied as a container file; the local source file still needs host-side protection and a deliberate secret-management process.
Validate and start the service
docker compose config
docker compose up -d
docker compose ps
docker compose logs --tail=100 db
Allow initialization to complete and review errors before connecting. Initial environment settings affect a newly initialized database; editing them is not a general password-change procedure for an existing data volume.
Connect to the correct endpoint
For an initial administrative check:
docker compose exec db mariadb -u root -p
Enter the approved password at the prompt. Create a separate least-privilege account for the application. Applications on the Compose network can connect to the service name and database port; a host client cannot use an unpublished port merely by specifying localhost.
Test persistence and recovery
Create disposable test data, restart the service and confirm it persists in the named volume. Use a database-consistent backup method and test restoration independently. A volume protects data across container replacement but is not a backup.
Before an image upgrade, review MariaDB’s compatibility and upgrade procedure. Do not remove a data volume to troubleshoot an unexplained connection error. The previous host-connection example lacked a published port; this update makes the chosen connection path explicit.
Practical checks and next steps
Changing an initialization variable does not rotate an existing database password. Back up with a database-consistent method and test an independent restore before upgrading the image.
Explore Virtualization & Containers for related guides.
Reference 1 Reference 2 Reference 3
Content review: November 24, 2026. Reviewed the stated scope, guidance and source references; tenant, hardware and production changes were not executed as part of this editorial review. Original publication date retained.