Home > Database > Mysql Tutorial > body text

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

Linda Hamilton
Release: 2024-10-24 10:48:29
Original
142 people have browsed it

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"]
Copy after login
  • Remove the /var/lib/mysql volume.
  • Build the image:
<code class="sh">sudo docker build -t <image-name> .</code>
Copy after login
  • 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>
Copy after login
  • Commit the changes to the image:
<code class="sh">sudo docker commit -m "Imported liferay sql dump" mysql-psat1 <image-name>:v1</code>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!