2

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

Mahadevan
  • 63
  • 3
  • 9
  • docker commit https://docs.docker.com/engine/reference/commandline/commit/ then Save/Load? https://stackoverflow.com/a/23938978/5330223 – Sumsuddin Shojib Jan 16 '18 at 06:19
  • Upload them to https://hub.docker.com/ and download from another docker host? – SuicideSheep Jan 16 '18 at 06:20
  • Yes Source Machine - docker commit old newcontainer docker save newcontainer > /tmp/newimage.tar Destination Machine- docker load < /tmp/newimage.tar docker run --name containername -d -p 3306:3306 newcontainer – Mahadevan Jan 16 '18 at 06:24
  • yes.. `docker commit` https://gist.github.com/thaJeztah/8d0e901bd21329d80cf2 – Fendi jatmiko Jan 16 '18 at 06:25

2 Answers2

10

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.

yamenk
  • 46,736
  • 10
  • 93
  • 87
  • can you share full command to start new container in new host – Mahadevan Jan 16 '18 at 09:05
  • 1
    @Mahadevan The commands to start mysql are in https://hub.docker.com/_/mysql/. For your question adding `-v ./mysql-data:/var/lib/mysql` to the run command is the relevant part. I don't know what specific configuration you pass to the container. But `docker run --name mysql -v ./mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql` will give a mysql container with data. – yamenk Jan 16 '18 at 09:09
1

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/

Sudhir G
  • 381
  • 4
  • 7