Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose save/load images to another host

I have found this, but it does not work for me.

My (really) simple docker-compose.yml:

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_PASSWORD: example

  mysql:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example

Starting:

docker-compose up

After made some change to containers (install plugins and themes on wordpress).

docker-compose stop
docker commit main_mysql_1 test-mysql
docker commit main_wordpress_1 test-wordpress
docker save test-mysql > test-mysql.tar
docker save test-wordpress > test-wordpress.tar

Save the two tar files on another machine and load them:

docker load -i ./test-mysql.tar
docker load -i ./test-wordpress.tar

Now change the docker-compose.yml to:

version: '3.1'

services:

  wordpress:
    image: test-wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_PASSWORD: example

  mysql:
    image: test-mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example

But the container started is wordpress from scratch. Nothing of work done (plugin, themes, etc) was preserved.

What is my mistake? I don't want to use online repository for these private purposes.. Could you suggest a more simple and powerful procedure for pass container between two hosts?


A workaround with volumes:

version: '3.1'

services:

  wordpress:
    container_name: GREB_wordpress      
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_PASSWORD: example
    volumes:
      - ./www:/var/www/html  

  mysql:
    container_name: GREB_mysql
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - ./mysql_data:/var/lib/mysql  
like image 592
elbowz Avatar asked Dec 17 '17 14:12

elbowz


People also ask

Can we copy docker images from one host to another?

Copy Docker images from one host to another by tar files Docker allows you to save images into tar files using the docker save . This command also compresses images and enables sharing them easily and quickly. You can then use the docker load command to load the Docker image back from the tar file into another host.

Are docker images shared between users?

There is only one Docker installation on a server. Anyone who can access Docker can access any of the images and manage any of the containers.

Does docker load overwrite?

As shown in docker load page, it "restores both images and tags." So an image from the same name doesn't get overwritten.

How do I save multiple docker images in a tar file?

The 'docker save' is used to save one or more than one image to a tar archive. It includes all parent layers, and all tags or versions. It is by default streamed to STDOUT, however, we can write to a file, instead of STDOUT by specifying a flag.


1 Answers

First of all, docker volumes are not part of an image and/or a container. So these should be saved further the docker images (docker save).

For a better understanding of docker file system, volumes, ro/rw layer, could be read http://container-solutions.com/understanding-volumes-docker/.

Figure out if our image use volumes (seek "Volumes" key):

docker inspect image_name

You have different advantage to use volumes (refer to docker documentation for well understand) such as I/O performance.

A the end, for backup volumes:

  • Simply backup volumes folder e.g. tar -cvzPf volume_name_backup.tar.gz /var/lib/docker/volumes/VOLUME_NAME...and restore them in the same place
  • Backup, restore, or migrate data volumes through another docker container
  • In case of db image (like my case with mysql) you can also dump the db: docker exec mysql_container /usr/bin/mysqldump -u root --password=root --all-databases > mysql_dump_backup.sql
like image 168
elbowz Avatar answered Sep 29 '22 13:09

elbowz