dockerdocker-container

Data is lost in MySQL docker container after commiting it to a new image?


I pulled a mysql base image from docker hub and run a container from that image and created a database and some tables in that container, and committed the changes to a new image. But when i run a container of my new image, there are no databases that i have created. How to achieve this? I don't want to use mounted volumes because i want to link this container with another container of my application so that it can inset and update into db .


Solution

  • I don't know which image you're using but I believe every Dockerfile of official mysql images has following command:

    VOLUME /var/lib/mysql
    

    which means all mysql data goes to volume. From docker volume docs they specially mentioned that:

    > Changes to a data volume will not be included when you update an image.
    

    And this is the root cause you loss your data in newly started container.

    For your question:

    How to achieve this?

    You can use either mounted volumes, or create a data volume container, then run mysql container with option --volumes-from. The former links contain step-by-step guide of how to do it.

    I don't want to use mounted volumes because i want to link this container with another container of my application so that it can inset and update into db

    I don't know what other concerns you have about mounted volumes, but as far as I can tell, using mounted volumes does not prevent you to link containers.

    Hope this helps you.