docker - 迁移和备份
文章目录
- 1、docker commit
- 1.1、查询 容器 docker ps
- 1.2、docker commit zookeeper zookeeper:3.4.13
- 2、docker save -o
- 2.1、宿主机 切换到 /opt 目录下
- 2.2、将镜像保存到 宿主机/opt目录下
- 3、docker load -i
对某一个容器修改完毕以后,我们可以把最新的容器部署到其他的环境中。具体的流程操作如下所示:
涉及的docker命令:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
docker commit 容器名称/容器的id 镜像名称 # 把docker容器保存成一个镜像
docker save -o 镜像tar文件名称 镜像名称/镜像id # 把镜像保存为tar文件
docker load -i 镜像名称 # 把tar文件恢复成为一个镜像
1、docker commit
[root@localhost ~]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Aliases:
docker container commit, docker commit
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
1.1、查询 容器 docker ps
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
903246a96cb1 portainer/portainer "/portainer" 32 minutes ago Up 32 minutes 8000/tcp, 9443/tcp, 0.0.0.0:10010->9000/tcp, :::10010->9000/tcp portainer
8c71efe9dca7 wurstmeister/zookeeper "/bin/sh -c '/usr/sb…" 4 weeks ago Up 7 hours 22/tcp, 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, :::2181->2181/tcp zookeeper
c14772057ab8 redis "docker-entrypoint.s…" 9 months ago Up 7 hours 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp spzx-redis
ab66508d9441 mysql:8 "docker-entrypoint.s…" 9 months ago Up 7 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp spzx-mysql
1.2、docker commit zookeeper zookeeper:3.4.13
[root@localhost ~]# docker commit zookeeper zookeeper:3.4.13
sha256:b95c1d54c7b222f8c57b43ed9aa00e04182cc2907e4c904ede25636afb4f6153
2、docker save -o
[root@localhost opt]# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Aliases:
docker image save, docker save
Options:
-o, --output string Write to a file, instead of STDOUT
2.1、宿主机 切换到 /opt 目录下
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
apache-tomcat-10.1.10 kibana mysql-community-devel-8.0.30-1.el7.x86_64.rpm mysql-community-test-8.0.30-1.el7.x86_64.rpm
apache-tomcat-10.1.10.tar.gz mongo mysql-community-embedded-compat-8.0.30-1.el7.x86_64.rpm redis-7.0.10
containerd mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar mysql-community-icu-data-files-8.0.30-1.el7.x86_64.rpm redis-7.0.10.tar.gz
elasticsearch mysql-community-client-8.0.30-1.el7.x86_64.rpm mysql-community-libs-8.0.30-1.el7.x86_64.rpm rh
jdk-17.0.7 mysql-community-client-plugins-8.0.30-1.el7.x86_64.rpm mysql-community-libs-compat-8.0.30-1.el7.x86_64.rpm tool
jdk-17_linux-x64_bin.tar.gz mysql-community-common-8.0.30-1.el7.x86_64.rpm mysql-community-server-8.0.30-1.el7.x86_64.rpm
kafka mysql-community-debuginfo-8.0.30-1.el7.x86_64.rpm mysql-community-server-debug-8.0.30-1.el7.x86_64.rpm
[root@localhost opt]#
2.2、将镜像保存到 宿主机/opt目录下
[root@localhost opt]# docker save -o z3.tar zookeeper:3.4.13
[root@localhost opt]#
[root@localhost opt]# ls
apache-tomcat-10.1.10 kibana mysql-community-devel-8.0.30-1.el7.x86_64.rpm mysql-community-test-8.0.30-1.el7.x86_64.rpm
apache-tomcat-10.1.10.tar.gz mongo mysql-community-embedded-compat-8.0.30-1.el7.x86_64.rpm redis-7.0.10
containerd mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar mysql-community-icu-data-files-8.0.30-1.el7.x86_64.rpm redis-7.0.10.tar.gz
elasticsearch mysql-community-client-8.0.30-1.el7.x86_64.rpm mysql-community-libs-8.0.30-1.el7.x86_64.rpm rh
jdk-17.0.7 mysql-community-client-plugins-8.0.30-1.el7.x86_64.rpm mysql-community-libs-compat-8.0.30-1.el7.x86_64.rpm tool
jdk-17_linux-x64_bin.tar.gz mysql-community-common-8.0.30-1.el7.x86_64.rpm mysql-community-server-8.0.30-1.el7.x86_64.rpm z3.tar
kafka mysql-community-debuginfo-8.0.30-1.el7.x86_64.rpm mysql-community-server-debug-8.0.30-1.el7.x86_64.rpm
备份成功。
3、docker load -i
[root@localhost opt]# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Aliases:
docker image load, docker load
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output