Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone Docker containers with data to another Host

I have two running docker containers. one container has MySQL with data, another one container has Java web application in Tomcat.

How to clone MySQL container to another Docker host with data?

Tried Save/Load method. but no success because doesn't have data

but Java web application container is working

like image 566
Mahadevan Avatar asked Jun 19 '26 02:06

Mahadevan


2 Answers

The docker way to save a container is using docker commit. However:

The commit operation will not include any data contained in volumes mounted inside the container.

Thus data saved in volumes won't be part of the commited container. This is the case for the MySQL docker image which saves the data under /var/lib/mysql and declares it as a docker volume.

There is a two step solution:

  1. Commit the container using docker commit into a new image and transfer this image to the other machine.

  2. Copy the data folder from the old container using docker cp <mysql-container>:/var/lib/mysql ./mysql-data and transfer the mysql-data folder to the new machine. When starting the new container on the new host, run it using docker run -v ./mysql-data:/var/lib/mysql .... This will mount the data folder from the old container, and you should get an identical container with data.

like image 56
yamenk Avatar answered Jun 21 '26 00:06

yamenk


Have you tried creating data Volume container? Mount volume directory to mysql container

  docker create -v /var/lib/mysql --name mysqldata mysql

Refer this link for more details https://www.melvinvivas.com/using-docker-data-volume-with-a-mysql-container/

like image 40
Sudhir G Avatar answered Jun 21 '26 00:06

Sudhir G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!