Running MariaDB in Docker with Proper Data Separation
Following the method found online:
docker run -d -p 3306:3306 --restart always -e MYSQL_ROOT_PASSWORD={password} --name db-mysql -v /docker/mysql/:/var/lib/mysql mysql/mysql-server
The only result is a notice that the root user cannot log in. To build a properly working environment with the database files stored on the host, follow these steps:
1. First create a Docker container, automatically pulling the latest image.
docker run -d -p 3306:3306 --restart always -e MYSQL_ROOT_PASSWORD={password} --name db-mariadb docker.io/mariadb:latest
2. Enter the container and configure MySQL.
docker exec -it db-mariadb bash
3. Configure the MariaDB database normally.
4. Exit the container.
exit
5. On the host, create a directory to hold the database files, e.g. /Docker.
mkdir /Docker
6. Copy the data files out of the container to the host.
docker cp db-mariadb:/var/lib/mysql /docker/
7. Stop the original container and remove it.
docker stop db-mariadb
docker rm db-mariadb
8. Create a new container and mount the directory. Note that if SELinux is enabled on the system, you must add --privileged=true.
docker run -d -p 3306:3306 --restart always -e MYSQL_ROOT_PASSWORD={password} --name db-mariadb -v /docker/mysql:/var/lib/mysql --privileged=true mariadb