MariaDB and phpMyAdmin - Docker Setup & Usage Guide
ℹ️ Introduction
This guide explains how to set up and access a MariaDB database with phpMyAdmin using Docker Compose.By default this container uses our default External Docker network settings, to make sure containers in this network can work together.
What’s in my network
It can be useful to know what container, IPv4 addresses and ports are used in a network
For this we have a script that displays the information for you. it can be found in my Powershelll-Utilities repository here. Use the docker-netw-info directory to execute the script
⚡🏃♂️ Quick Setup
- Make sure the external network exists (or create it)
docker network create --subnet=172.40.0.0/24 dev1-net
- Create and start the container
docker compose -f compose.yaml up -d --build --force-recreate --remove-orphans
- Verify Network
- Verify that both containers are on the same network:
docker network inspect dev1-net
Also the other containers in the network will be shown including details like Ip address - Check the IP address inside a container:
hostname -I
▶️ Usage
Default Credentials
- Database:
docker - Username:
docker - Password:
docker - Port:
3306(internal),9004(external)
Access phpMyAdmin
Access phpMyAdmin from the host system at:
http://localhost:9001/ # Login using the default credentials above.
Access the Database from a Another Container
When connecting other Docker containers to this MariaDB instance, configure your compose file with these environment variables:
- The service name from the current compose file as the host environment variable (
DB_HOST) - The internal port (
3306) as the port environment variable (DB_PORT) - The
MARIADB_USERvalue as the user environment variable (DB_USER) - The
MARIADB_PASSWORDvalue as the password environment variable (DB_PASSWORD) - The
MARIADB_DATABASEvalue as the database name environment variable (DB_NAME)
Example: Web Application Compose File
webapp:
build: ./webapp
depends_on:
- mariadb
environment:
DB_HOST: mariadb
DB_PORT: 3306 # Use the internal port (second value)
DB_USER: docker
DB_PASSWORD: docker
DB_NAME: docker
Access via DNS Name (from within Docker)
$dsn = 'mysql:host=mariadb;port=3306;dbname=docker;charset=utf8mb4'; $db = new PDO($dsn, 'docker', 'docker'); // first 'docker' = user, second = password
Access from Host or External Machine
When connecting from the host system or another machine on the network:
DB_HOST=127.0.0.1 # Use host's IP address for external machines DB_PORT=9004 # Use the external port (first value in ports mapping) DB_USER=docker DB_PASSWORD=docker DB_NAME=docker