• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

Docker-compose with Persistent MySQL Data

Written by Rahul, Updated on July 1, 2020

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
version: '3'
 
services:
  db:
    image: mysql:5.7
    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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
version: '3'
 
services:
  db:
    image: mysql:5.7
    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

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Ubuntu 20.04 5
  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy