0

If I'm working with a containerized MySQL database that wasn't originally run with shared volume options, what's the easiest way to sort of externalize the data? Is there still a way to modify the container so that it shares its data with the Docker host in a specified directory?

Note: if you're still having problems with this question, please comment so I can improve it further.

Psycho Punch
  • 6,418
  • 9
  • 53
  • 86
  • 1
    I wish people can elaborate on why this question is getting down voted. – Psycho Punch Mar 20 '16 at 17:43
  • Perhaps the term “good practice” inspired someone (not me). People prefer questions like “I have problem U. I tried V, it gave me W but I want X. My code is Y”. Yours doesn't fall into this category. – PerlDuck Mar 20 '16 at 17:59
  • Well, I don't really want to argue about the value of the practice; it was pretty much just an opening remark. However, outside of that, I think I've raised pretty much objective questions here. – Psycho Punch Mar 20 '16 at 18:06
  • I don't want to argue either, I just gave a possible explanation for the downvote. I've seen really bad questions but mostly these people had a reputation that didn't significantly differ from 1. Given your rep, I'm sure you know how to ask. OMG, I'm talking like I'm the wise guy here... -- _far, far_ away from that. ;-) – PerlDuck Mar 20 '16 at 18:23
  • A way to do that is to commit your container to an image, and run a new container out of the image with a volume mounted. See http://stackoverflow.com/questions/28302178/how-can-i-add-a-volume-to-an-existing-docker-container – Xiongbing Jin Mar 20 '16 at 19:40

1 Answers1

2

Official Docker documentation provides a great overview on how to backup, restore, or migrate data volumes. For my problem, in particular, I did the following:

  1. Run a throw-away Docker container that runs Ubuntu, shares volumes with currently running MySQL container, and backs up database data in local machine (as described in the overview):

    docker run --rm --volumes-from some-mysql -v /path/to/local/directory:backup ubuntu:15.10 tar cvf /backup/mysql.tar /var/lib/mysql

(The official MySQL Docker image uses /var/lib/mysql for storing data.)

  1. The previous step will result in creation of /path/to/directory/mysql.tar in the Docker host. This can now be extracted like:

    tar -xvf mysql.tar

(Assuming cd /path/to/directory). The resulting directory (/var/lib/mysql) can now be used as shared volume with same instance, or any other instance of containerized MySQL.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Psycho Punch
  • 6,418
  • 9
  • 53
  • 86