Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Docker»Docker-compose with Persistent MySQL Data

    Docker-compose with Persistent MySQL Data

    By 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.

    Advertisement

    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
    
    

    compose Docker volume
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Running a Cronjob Inside Docker: A Beginner’s Guide

    Running a Cronjob Inside Docker: A Beginner’s Guide

    Docker Run: A Beginner’s Guide to Run Docker Containers

    Docker Run: A Beginner’s Guide to Run Docker Containers

    Docker Build: A Beginner’s Guide to Building Docker Images

    Docker Build: A Beginner’s Guide to Building Docker Images

    View 1 Comment

    1 Comment

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

      try to run docker-compose build

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • tail Command in Linux with Examples
    • What is a Orphan Process in Unix/Linux
    • How To Display Warning Message to Unauthorized SSH Access
    • How to Set a Custom SSH Login Banner and MOTD
    • Understanding Reverse DNS: What it is and Why it Matters?
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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