Committing Data in a MySQL Container
You have encountered an issue while attempting to persist database changes by committing a modified MySQL container to a new image. Despite importing a SQL dump and creating a new database, upon running a container based on the updated image, the changes are not reflected in the database.
Cause:
The official MySQL image utilizes a volume to store data, ensuring data preservation beyond the container's lifetime. However, when committing a container with volumes, the data changes are not included in the resulting image.
Solution:
To rectify this issue, create your own custom MySQL base image that omits the use of volumes. By doing so, data added during the commit process will be embedded in the image. Note that this approach comes with a caveat: subsequent data additions to containers spawned from the committed image will be lost upon container termination.
Modified Dockerfile:
The following Dockerfile demonstrates how to create a custom MySQL base image without volumes:
<code class="dockerfile">FROM mysql:latest # Remove volume mounts RUN rm -rf /var/lib/mysql/*</code>
Creating and Committing the Image:
Build the custom base image using the modified Dockerfile:
docker build -t my-custom-mysql .
Create a new container based on the custom image and import the SQL dump:
docker run --name my-custom-mysql -d my-custom-mysql docker exec -it my-custom-mysql bash mysql -uroot -psecret -e 'create database liferay_psat1;' mysql -uroot -psecret liferay_psat1 < /mnt/liferay_sql_dump.sql
Commit the container as a new image, incorporating the imported data:
docker commit -m "Imported liferay SQL dump" <container-id> jihedamine/mysql-psat1:v2
Verifying the Changes:
Run a container using the updated image and check the database content:
docker run -ti jihedamine/mysql-psat1:v2 bash mysql -uroot -psecret # show databases;
The 'liferay_psat1' database should now be present, reflecting the changes you made.
The above is the detailed content of How to Commit Data Changes in a MySQL Container Without Using Volumes?. For more information, please follow other related articles on the PHP Chinese website!