database.
Specify the data directory
replace_data_directory
.
Choose a container name
replace_container
and
volume names
replace_volume
.
Specify the network
replace_network
for connectivity with other services.
Create the directories and the volumes.
data_directory="replace_data_directory" # data_directory=/data/services
container=replace_container # container=mariadb
volume=replace_volume # volume=mariadb
volume_configuration=${volume}_configuration
volume_data=${volume}_data
network=replace_network # network=services
sudo mkdir --parents "$data_directory/mariadb"
sudo chmod --recursive a+rwX "$data_directory/mariadb/"
mkdir "$data_directory/mariadb/configuration"
mkdir "$data_directory/mariadb/data"
docker volume create \
--name $volume_configuration \
--driver local-persist \
--opt mountpoint="$data_directory/mariadb/configuration/"
docker volume create \
--name $volume_data \
--driver local-persist \
--opt mountpoint="$data_directory/mariadb/data/"
Start the database in a temporary container with the root user password setting.
Wait for it to initialize completely and then just interrupt or stop it from another session.
docker run --rm --interactive --tty \
--name $container \
--hostname $container \
--network $network \
--mount type=volume,source=$volume_configuration,destination=/etc/mysql \
--mount type=volume,source=$volume_data,destination=/var/lib/mysql \
--env MYSQL_ROOT_PASSWORD=root \
--mount type=bind,source=/etc/timezone,destination=/etc/timezone,readonly \
--mount type=bind,source=/etc/localtime,destination=/etc/localtime,readonly \
mariadb
# after initialization finishes stop mariadb from another session or
# interrupt it in the temporary container session
docker container stop $container
Create the container. This time it is permanent, so no password setting.
docker run --detach --restart unless-stopped \
--name $container \
--hostname $container \
--network $network \
--publish 3306:3306 \
--mount type=volume,source=$volume_configuration,destination=/etc/mysql \
--mount type=volume,source=$volume_data,destination=/var/lib/mysql \
--mount type=bind,source=/etc/timezone,destination=/etc/timezone,readonly \
--mount type=bind,source=/etc/localtime,destination=/etc/localtime,readonly \
mariadb
Connect with your