Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

How to copy docker containers to a new host, including stored data?

I have done some developing with docker containers for InfluxDB, Chronograf and Grafana (using this docker-compose setup) on my local machine::

version: "3"

services:

    influxdb:
        image: influxdb:1.5
        volumes:
            - influxdb_data:/var/lib/influxdb
        ports:
            - 127.0.0.1:8086:8086
        restart: always
    chronograf:
        image: chronograf:1.5
        volumes:
            - chronograf_data:/var/lib/chronograf
        ports:
            - 127.0.0.1:8087:8888
        restart: always
    grafana:
        image: grafana/grafana
        ports:
            - 127.0.0.1:3000:3000
        volumes:
            - grafana_data:/var/lib/grafana
        restart: always

volumes:
    influxdb_data:
    chronograf_data:
    grafana_data:

I want to copy these containers to a cloud based VM, including data that is stored in InfluxDB, and dashboards created in Grafana.

What is the preferred way to do this?

Things I have tried:

  • Save and Load: this only includes the docker images, so no data is loaded on the new host.
  • Export and Import: somehow when I try to import a docker container, it shows no error, but I can't start the container as it doesn't show up in my list of containers, nor my list of images.
question from:https://stackoverflow.com/questions/65853463/how-to-copy-docker-containers-to-a-new-host-including-stored-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Whenever I need to move containers here's all I do; I just backup any named volumes I need, and then restore them to the new host (I manually create the new named volume on the host first and import to it there).

Aside from that there's not much more to containers; the rest gets fetched when you re-deploy your compose file (or do docker run).

# Backup
docker run --rm -v volume_name:/volume -v c:migrate:/backup alpine tar -cjf /backup/backup.tar.bz2 -C /volume ./
# Restore
docker run --rm -v volume_name:/volume -v c:migrate:/backup alpine tar -C /volume/ -xjf /backup/backup.tar.bz2

In my compose file I set those volumes as external so docker knows not to try to create them, and use the existing one(s):

i.e. if I created a named volume "my_bitwarden_data" then I'd use:

volumes:
  my_bitwarden_data:
    external: true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...