Home >Database >Mysql Tutorial >How Do I Commit Data Changes to a MySQL Container Image?

How Do I Commit Data Changes to a MySQL Container Image?

Linda Hamilton
Linda HamiltonOriginal
2024-10-24 10:48:29240browse

How Do I Commit Data Changes to a MySQL Container Image?

How to Commit Data Changes to a MySQL Container Image

When using the official MySQL image to create a container, any data stored in the database will persist even after the container is stopped or deleted. This is because the data is stored in a persistent volume that is not part of the image itself.

However, in some cases, you may want to commit the database changes to the image so that the data is included in the new image. To do this, you need to create a custom MySQL image with no volumes.

Steps:

  • Create a new Dockerfile and include the following lines:
FROM mysql
VOLUME ["/var/lib/mysql"]
  • Remove the /var/lib/mysql volume.
  • Build the image:
<code class="sh">sudo docker build -t <image-name> .</code>
  • Run the container using the new image and import the SQL dump:
<code class="sh">sudo docker run --name mysql-psat1 -e MYSQL_ROOT_PASSWORD=secret -d <image-name>
sudo docker exec -it mysql-psat1 bash
> mysql -uroot -psecret -e 'create database liferay_psat1;'
> mysql -uroot -psecret liferay_psat1 < /mnt/liferay_sql_dump.sql</code>
  • Commit the changes to the image:
<code class="sh">sudo docker commit -m "Imported liferay sql dump" mysql-psat1 <image-name>:v1</code>

The new image will contain the newly created database and its data.

The above is the detailed content of How Do I Commit Data Changes to a MySQL Container Image?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn