Docker 镜像和容器的导入导出及常用命令

Docker 镜像和容器的导入导出

1.1 镜像的导入导出

1.1.1 镜像的保存

  • 通过镜像ID保存

    • 方式一:
bash 复制代码
docker save image_id > image-save.tar

例如:

bash 复制代码
root@Ubuntu:/usr/local/docker/nginx# docker imagesREPOSITORY      TAG                 IMAGE ID       CREATED         SIZEopenjdk         8-jre               26ac3f63d29f   2 months ago    273MBnginx           1.21.3              87a94228f133   4 months ago    133MBroot@Ubuntu:/usr/local/docker/nginx# docker save 87a94228f133 > nginx-save.tar
    • 方式二:
bash 复制代码
docker save -o image-save.tar image_id

例如:

bash 复制代码
root@Ubuntu:/usr/local/docker/nginx# docker imagesREPOSITORY      TAG                 IMAGE ID       CREATED         SIZEopenjdk         8-jre               26ac3f63d29f   2 months ago    273MBnginx           1.21.3              87a94228f133   4 months ago    133MBroot@Ubuntu:/usr/local/docker/nginx# docker save -o nginx-save.tar 87a94228f133
  • 通过镜像repository和tag保存
bash 复制代码
docker save -o image-save.tar repository:tag

例如:

bash 复制代码
root@Ubuntu:/usr/local/docker/nginx# docker imagesREPOSITORY      TAG                 IMAGE ID       CREATED         SIZEopenjdk         8-jre               26ac3f63d29f   2 months ago    273MBnginx           1.21.3              87a94228f133   4 months ago    133MBroot@Ubuntu:/usr/local/docker/nginx# docker save -o nginx-save.tar nginx:1.21.3

1.1.2 镜像的导入

  • 方式一:
bash 复制代码
docker load < nginx-save.tar
  • 方式二:
bash 复制代码
docker load -i nginx-save.tar
  • 注意

    • 使用 image_id作为参数的方式导出的镜像包进行导入会出现 none的情况,需要手动打标签
bash 复制代码
docker tag 87a94228f133 nginx:1.21.3
    • 使用镜像 repository:tag 作为导出参数的方式则正常

1.2 容器的导入和导出

1.2.1 容器的导出

容器的导出是将当前容器变成一个容器包

bash 复制代码
root@Ubuntu:/usr/local/docker/nginx# docker ps -aCONTAINER ID   IMAGE         COMMAND                  CREATED      STATUS       PORTS                                        NAMES66b23477cdc6   nginx:1.21.3  "/docker-entrypoint...."   7 days ago   Up 3 hours   80/tcp, 0.0.0.0:80->80/tcp, :::80->80/tcp    nginx_51tjroot@Ubuntu:/usr/local/docker/nginx# docker export -o nginx-export.tar 66b23477cdc6

1.2.2 容器包的导入

bash 复制代码
docker import nginx-export.tar nginx:1.21.3-new

export 和 import 导出的是一个容器的快照, 不是镜像本身, 也就是说没有 layer。

你的 dockerfile 里的 workdir, entrypoint 之类的所有东西都会丢失,commit 过的话也会丢失。

快照文件将丢弃所有的历史记录和元数据信息(即仅保存容器当时的快照状态),而镜像存储文件将保存完整记录,体积也更大。

注意:

  • docker save 保存的是镜像(image),docker export 保存的是容器(container);

  • docker load 用来载入镜像包,docker import 用来载入容器包,但两者都会恢复为镜像;

  • docker load 不能对载入的镜像重命名,而 docker import 可以为镜像指定新名称。

Docker 常用命令

查看 Docker 版本

bash 复制代码
docker version

从 Docker 文件构建 Docker 映像

bash 复制代码
docker build -t image-name docker-file-location

运行 Docker 映像

bash 复制代码
docker run -d image-name

查看可用的 Docker 映像

bash 复制代码
docker images

查看最近的运行容器

bash 复制代码
docker ps -l

查看所有正在运行的容器

bash 复制代码
docker ps -a

停止运行容器

bash 复制代码
docker stop container_id

删除一个镜像

bash 复制代码
docker rmi image-name

删除所有镜像

bash 复制代码
docker rmi $(docker images -q)

强制删除所有镜像

bash 复制代码
docker rmi -r $(docker images -q)

删除所有虚悬镜像‍

bash 复制代码
docker rmi $(docker images -q -f dangling=true)  docker image prune

删除所有容器

bash 复制代码
docker rm $(docker ps -a -q)

进入 Docker 容器

bash 复制代码
docker exec -it container-id /bin/bash

查看所有数据卷

bash 复制代码
docker volume ls

删除指定数据卷

bash 复制代码
docker volume rm [volume_name]

删除所有未关联的数据卷

bash 复制代码
docker volume rm $(docker volume ls -qf dangling=true)

从主机复制文件到容器

bash 复制代码
sudo docker cp host_path containerID:container_path

从容器复制文件到主机

bash 复制代码
sudo docker cp containerID:container_path host_path

读到这里,想必你已经对 Docker 容器和镜像的常用操作以及命令有了更加深入的理解。

转至:https://blog.csdn.net/FL63Zv9Zou86950w/article/details/126132387

相关推荐
π大星星️31 分钟前
Jenkins 工作流程
运维·jenkins
Juicedata1 小时前
JuiceFS v1.3-Beta2:集成 Apache Ranger,实现更精细化的权限控制
运维·人工智能·ai
❀͜͡傀儡师1 小时前
如何使用k8s安装redis呢
redis·容器·kubernetes
IT成长日记2 小时前
05【Linux经典命令】Linux 用户管理全面指南:从基础到高级操作
linux·运维·服务器·用户管理·命令
资讯第一线5 小时前
Windows系统工具:WinToolsPlus 之 SQL Server Suspect/质疑/置疑/可疑/单用户等 修复
运维
惊起白鸽4506 小时前
LVS负载均衡
运维·负载均衡·lvs
伤不起bb7 小时前
NoSQL 之 Redis 配置与优化
linux·运维·数据库·redis·nosql
广东数字化转型8 小时前
nginx怎么使用nginx-rtmp-module模块实现直播间功能
linux·运维·nginx
love530love8 小时前
【笔记】在 MSYS2(MINGW64)中正确安装 Rust
运维·开发语言·人工智能·windows·笔记·python·rust
啵啵学习8 小时前
Linux 里 su 和 sudo 命令这两个有什么不一样?
linux·运维·服务器·单片机·ubuntu·centos·嵌入式