Home > Database > Mysql Tutorial > body text

How to Commit Data in a MySQL Docker Container and Preserve It

Mary-Kate Olsen
Release: 2024-10-24 09:07:29
Original
237 people have browsed it

How to Commit Data in a MySQL Docker Container and Preserve It

Docker: Committing Data in a MySQL Container

When trying to commit data to a MySQL container image, it's important to understand the impact of data volumes.

The official MySQL Docker image uses data volumes to store its data. While this allows for data persistence beyond the lifespan of a container, it also means that data is not included in the committed image.

To commit data to an image along with MySQL, create a custom base image without volumes. For example, create a new image based on the MySQL image with the following Dockerfile:

FROM mysql:latest
RUN rm -rf /var/lib/mysql/
CMD ["mysqld"]
Copy after login

Then, build the custom image:

docker build -t my-custom-mysql-image .
Copy after login

With this custom base image, you can create containers and import data as you did before:

docker run --name my-mysql-container -e MYSQL_ROOT_PASSWORD=secret -d my-custom-mysql-image
docker exec -it my-mysql-container bash
mysql -uroot -psecret -e 'create database liferay_psat1;'
mysql -uroot -psecret liferay_psat1 < /mnt/liferay_sql_dump.sql
Copy after login

Now, when you commit the container as a new image:

docker commit -m "Imported liferay sql dump" my-mysql-container my-custom-mysql-image:v1
Copy after login

The imported data will be included in the committed image and available when starting new containers with that image.

The above is the detailed content of How to Commit Data in a MySQL Docker Container and Preserve It. 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!