It is importent to keep data persistent for containers running databases. Docker provides you option to keep database files persistent over the docker volumes or storing files directly on host machine.
Use one of the below options to keep MySQL data persistent even after recreating or deleting docker containers.
Option 1: Storing MySQL Data on Docker Volumes
The Docker volumes are preferred mechanism by the Docker for storing persistent data of Docker containers. You can easily create a Docker volume on your host machine and attach to a Docker containers.
Let’s create a docker-compose file on your system with the following content.
docker-compose.yml:
services:
db:
image: mysql:8
container_name: db
environment:
MYSQL_ROOT_PASSWORD: my_secret_password
MYSQL_DATABASE: app_db
MYSQL_USER: db_user
MYSQL_PASSWORD: db_user_pass
ports:
- "6033:3306"
volumes:
- dbdata:/var/lib/mysql
volumes:
dbdata:
The above configuration defined one data volume named “dbdata”, which is attached to MySQL container and mounted on /var/lib/mysql directory. This is the default directory used by MySQL to store all data files.
Next, run below command to launch Docker container.
docker-compose up -d
Output:
Creating network "db_default" with the default driver Creating volume "db_dbdata" with default driver Creating db ... done
You can view the docker volumes by running commnad:
docker volume ls
Option 2 – Storing MySQL Data on Host Machine
We recommend to use data volume instead of putting files on host machine. But, If you like, you can keep database files on the host machine. In any case docker container get terminated, you can relaunch container using the existing data files.
Create a directory to keep your MySQL data files. I am creating below directory structure under the current directory.
mkdir -p ./data/db
Then configure docker-compose.yml to use ./data/db as volume to store all files created by the MySQL server. Next create compose file in current directory.
docker-compose.yml:
services:
db:
image: mysql:8
container_name: db
environment:
MYSQL_ROOT_PASSWORD: my_secret_password
MYSQL_DATABASE: app_db
MYSQL_USER: db_user
MYSQL_PASSWORD: db_user_pass
ports:
- "6033:3306"
volumes:
- ./data/db:/var/lib/mysql
After creating file, just run the below command to launch container.
docker-compose up -d
Output:
Creating network "db_default" with the default driver Creating db ... done
In this case the MySQL container creats all files on host machine under ./data/db directory. To view these files, just run below command.
ls -l ./data/db
drwxr-x--- 2 systemd-coredump systemd-coredump 4096 Jul 1 11:07 app_db
-rw-r----- 1 systemd-coredump systemd-coredump 56 Jul 1 11:07 auto.cnf
-rw------- 1 systemd-coredump systemd-coredump 1676 Jul 1 11:07 ca-key.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump 1112 Jul 1 11:07 ca.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump 1112 Jul 1 11:07 client-cert.pem
-rw------- 1 systemd-coredump systemd-coredump 1680 Jul 1 11:07 client-key.pem
-rw-r----- 1 systemd-coredump systemd-coredump 1346 Jul 1 11:07 ib_buffer_pool
-rw-r----- 1 systemd-coredump systemd-coredump 50331648 Jul 1 11:07 ib_logfile0
-rw-r----- 1 systemd-coredump systemd-coredump 50331648 Jul 1 11:07 ib_logfile1
-rw-r----- 1 systemd-coredump systemd-coredump 79691776 Jul 1 11:07 ibdata1
-rw-r----- 1 systemd-coredump systemd-coredump 12582912 Jul 1 11:07 ibtmp1
drwxr-x--- 2 systemd-coredump systemd-coredump 4096 Jul 1 11:07 mysql
drwxr-x--- 2 systemd-coredump systemd-coredump 4096 Jul 1 11:07 performance_schema
-rw------- 1 systemd-coredump systemd-coredump 1680 Jul 1 11:07 private_key.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump 452 Jul 1 11:07 public_key.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump 1112 Jul 1 11:07 server-cert.pem
-rw------- 1 systemd-coredump systemd-coredump 1680 Jul 1 11:07 server-key.pem
drwxr-x--- 2 systemd-coredump systemd-coredump 12288 Jul 1 11:07 sys
Conclusion
You can choose any one method for creating MySQL docker container with persistent data. It is very important to create data outside of container.
1 Comment
try to run docker-compose build