Docker mysql image - move internal container DB to the volume
docker, docker-compose
Solution
You won't lose your data if you run `docker pull mysql:5.6` since you were running an instance of `mydbimage`.
Also, there is no data loss unless you remove the container with `docker rm -v`. Docker Compose does not remove volumes, either, unless you run `docker-compose rm -v`.
Get the name or ID of the container that was running mysql:5.6 with `docker ps -a` and run:
`docker run --rm --volumes-from $container ubuntu tar -zcvf- /var/lib/mysql > /tmp/backup.tar.gz`
Note: Substitute `$container` with the name or ID of the container.
Then you can decompress the tarball to some location on the host, say /var/mysql, update `mydbimage` if you want, and add this line to your Docker Compose YAML:
volumes:
-v /var/mysql:/var/lib/mysql
NOTE: A `docker commit` never saves the volumes in a container.
Problem
I have started using docker with docker-compose where I have the database defined. Since I did not have volumes defined in the configuration (my mistake), the database has been created inside the container and now next time when I run docker-pull, I will get this database re-created causing my data loss. What would be the best way to move the data out of the container without losing the data? ``` db: image: "mydbimage" environment: MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}" MYSQL_DATABASE: "jirio" MYSQL_USER: "${DB_USERNAME}" MYSQL_PASSWORD: "${DB_PASSWORD}" ``` Dockerfile ``` FROM mysql:5.6 ADD schema.sql /docker-entrypoint-initdb.d/schema.sql ```