Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Docker»Docker-compose with Persistent MySQL Data

    Docker-compose with Persistent MySQL Data

    RahulBy RahulJuly 1, 20203 Mins Read

    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:

    Shell
    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:

    Shell
    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
    
    
    compose Docker volume
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install Nginx, MySQL & PHP (LEMP) on Ubuntu 20.04
    Next Article How to Install Ionic on Fedora 34/33/32

    Related Posts

    How to Install Docker Compose on Ubuntu 20.04

    4 Mins Read

    Docker-compose for MySQL with phpMyAdmin

    2 Mins Read

    How to Install Docker on Ubuntu 20.04

    Updated:June 2, 20213 Mins Read

    How to Add a Comments in Dockerfile

    1 Min Read

    How to Install Docker on CentOS/RHEL 8

    Updated:June 21, 20203 Mins Read

    How to Clear Log Files of A Docker Container

    Updated:June 10, 20211 Min Read

    1 Comment

    1. hacker on July 1, 2021 8:02 pm

      try to run docker-compose build

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    • (Resolved) Please install all available updates for your release before upgrading
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.