"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Commit Data in a MySQL Docker Container and Preserve It

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

Published on 2024-11-08
Browse:580

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 

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.

Release Statement This article is reprinted at: 1729731819 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3