Commit data in a mysql container Commit data in a mysql container docker docker

Commit data in a mysql container


The official mysql image stores data in a volume. Normally this is desired so that your data can persist beyond the life span of your container, but data volumes bypass the Union File System and are not committed to the image.

You can accomplish what you're trying to do by creating your own mysql base image with no volumes. You will then be able to add data and commit it to an image, but any data added to a running container after the commit will be lost when the container goes away.


Based on the @dekin research, I did this to solve the issue:

Dockerfile:

FROM mysql:latestRUN cp -r /var/lib/mysql /var/lib/mysql-no-volumeCMD ["--datadir", "/var/lib/mysql-no-volume"]

Build & run:

docker build . -t my-mysqldocker run -e MYSQL_ROOT_PASSWORD=root -it my-mysql


Based on @Robert answer, I ended up with this Dockerfile:

FROM mysql:5.6.22RUN cp -r /var/lib/mysql /var/lib/mysql-no-volumeRUN sed -i -e "s|/var/lib/mysql|/var/lib/mysql-no-volume|" /etc/mysql/my.cnf

The CMD override didn't work for me, container stopped with a strange error:

2019-02-21 15:18:50 1 [Note] Plugin 'FEDERATED' is disabled.mysqld: Table 'mysql.plugin' doesn't exist2019-02-21 15:18:50 1 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.

I guess that the original CMD command was doing more things that is now missing (in @Robert answer) so I did it in a different approach.I didn't try it with latest version but I think it should work.