Home  >  Article  >  Database  >  How to Commit Data in a MySQL Docker Container and Preserve It

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

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-24 09:07:29238browse

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"]

Then, build the custom image:

docker build -t my-custom-mysql-image .

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

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

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!

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