Docker容器迁移

1.将容器保存为镜像(先把容器停止运行不然会有问题)

docker commit 容器名称 保存的新镜像名称

操作:
[root@VM_0_7_centos nexus]# docker commit nexus nexus-z
sha256:511575baf7897e13c17da89e470b92a522785e2a9e61f32759eb49cc61a4140e
[root@VM_0_7_centos nexus]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nexus-z             latest              511575baf789        11 seconds ago      599MB

2.将镜像打为压缩包

docker save -o 打包后的文件名称 镜像名称

操作:
[root@VM_0_7_centos nexus]# docker save nexus-z -o ./nexus-z.tar
[root@VM_0_7_centos nexus]# ll
total 596132
-rw-------  1 root root 610430976 Dec 31 16:33 nexus-z.tar

3.将打包后的镜像压缩包和挂载的文件夹迁移(Mac可以使用Transmit工具)

4.镜像恢复

docker load -i 镜像保存的tar包

操作:

[root@localhost nexus]# docker load -i ./nexus-z.tar

5.修改docker-compose.yml文件

version: '3.7'
services:
  nexus:
    restart: always
    image: nexus-z #修改这里依赖的镜像
    container_name: nexus
    ports:
      - 8081:8081
    volumes:
      - /usr/local/docker/nexus/data:/nexus-data

6.根据docker-compose生成容器

[root@localhost nexus]# docker-compose up -d
Creating network "nexus_default" with the default driver
Creating nexus ... done
通过查看日志发现没有读写文件权限(Permission denied):
[root@localhost nexus]# docker logs 57ee
Warning:  Cannot open log file: ../sonatype-work/nexus3/log/jvm.log
Warning:  Forcing option -XX:LogFile=/tmp/jvm.log
OpenJDK 64-Bit Server VM warning: Cannot open file ../sonatype-work/nexus3/log/jvm.log due to Permission denied
java.io.FileNotFoundException: ../sonatype-work/nexus3/tmp/i4j_ZTDnGON8hezynsMX2ZCYAVDtQog=.lock (Permission denied)
修改文件夹操作权限
[root@localhost nexus]# chmod -R 777 data
[root@localhost nexus]# ll
total 596132
drwxrwxrwx. 16  777 root      4096 Dec 31 16:55 data
原文地址:https://www.cnblogs.com/zhufanfan/p/13132054.html