docker--镜像与容器的导入和导出

docker–镜像与容器的导入和导出

一、镜像的导入和导出

1、save保存镜像

首先pull一个基础镜像redis,然后进行保存
[root@docker-one ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
a603fa5e3b41: Pull complete 
77631c3ef092: Pull complete 
ed3847cf62b8: Pull complete 
261a8b530567: Pull complete 
7d9005a8af6d: Pull complete 
828da1afb5be: Pull complete 
Digest: sha256:1e3207c292225b6dd21cb74d59255748a50e8f739dd983040df38fa913927cf1
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[root@docker-one ~]# docker images
REPOSITORY        TAG        IMAGE ID       CREATED         SIZE
redis             latest     3358aea34e8c   12 days ago     117MB
[root@docker-one ~]# 
保存镜像

[root@docker-one ~]# docker save --help

Usage: docker save [OPTIONS] IMAGE [IMAGE…]

Save one or more images to a tar archive (streamed to STDOUT by default)

Options:
-o, --output string Write to a file, instead of STDOUT
[root@docker-one ~]#

[root@docker-one ~]# docker save -o sc-redis.tar redis
[root@docker-one ~]# ls
anaconda-ks.cfg  apiserver  busybox  docker  go  mydocker  nginx-docker  scdocker  scdocker_v2  sc-redis.tar
[root@docker-one ~]# 
传输sc-redis.tar到另一台机器上
# 第一台机器
[root@docker-one ~]# scp sc-redis.tar root@192.168.17.145:/root
The authenticity of host '192.168.17.145 (192.168.17.145)' can't be established.
ECDSA key fingerprint is SHA256:XcSgh3YYlNUkOfn95JlQQyCGHc3S6Ae0Zjpcfps4N3E.
ECDSA key fingerprint is MD5:4e:e3:e4:43:70:c8:ab:6a:37:09:5b:a4:81:ba:ed:5c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.17.145' (ECDSA) to the list of known hosts.
root@192.168.17.145's password: 
sc-redis.tar                                                                                                                         100%  115MB 131.9MB/s   00:00    
[root@docker-one ~]# 


# 第二台机器
[root@docker-two ~]# ls
anaconda-ks.cfg  composetest  harbor  my_compose  sc-redis.tar
[root@docker-two ~]# 

2、load导入镜像

[root@docker-two ~]# docker load --help

Usage: docker load [OPTIONS]

Load an image from a tar archive or STDIN

Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
[root@docker-two ~]#

[root@docker-two ~]# docker load -i sc-redis.tar 
84566964bca1: Loading layer [==================================================>]  338.4kB/338.4kB
95dff52c35dd: Loading layer [==================================================>]  4.165MB/4.165MB
1a2d05341f75: Loading layer [==================================================>]  32.02MB/32.02MB
8fc62bcb8f27: Loading layer [==================================================>]  2.048kB/2.048kB
b5e37833e629: Loading layer [==================================================>]  4.096kB/4.096kB
Loaded image: redis:latest
[root@docker-two ~]# docker images
REPOSITORY                      TAG            IMAGE ID       CREATED         SIZE
wordpress                       latest         1730fea0ae8e   11 days ago     612MB
redis                           latest         3358aea34e8c   12 days ago     117MB

二、容器的导入和导出

1、export导出镜像

首先拉取一个centos:7的镜像,然后创建一个centos:7的容器

因为export是从容器(container)中导出,所以需要创建一个容器。运行或停止的容器都可以export出。

[root@docker-one ~]# docker pull centos:7
7: Pulling from library/centos
Digest: sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@docker-one ~]# docker run -d -it --name sc-centos centos:7 bash
b669385f9529fee8e3607a5a2fb76cb4bba86ee100c7aa9e7b7b4f7e3bd4fede
[root@docker-one ~]# docker ps
CONTAINER ID   IMAGE      COMMAND   CREATED         STATUS         PORTS     NAMES
b669385f9529   centos:7   "bash"    3 seconds ago   Up 2 seconds             sc-centos
[root@docker-one ~]# 
导出镜像

[root@docker-one ~]# docker export --help

Usage: docker export [OPTIONS] CONTAINER

Export a container’s filesystem as a tar archive

Options:
-o, --output string Write to a file, instead of STDOUT
[root@docker-one ~]#

[root@docker-one ~]# docker export -o sc-centos.tar sc-centos
[root@docker-one ~]# ls
 sc-centos.tar
[root@docker-one ~]# 
传输sc-centos.tar文件到另一台机器上。
# 第一台机器
[root@docker-one ~]# scp sc-centos.tar root@192.168.17.145:/root
root@192.168.17.145's password: 
sc-centos.tar                                                                                                                        100%  202MB 133.9MB/s   00:01    
[root@docker-one ~]# 

# 第二台机器
[root@docker-two ~]# ls
anaconda-ks.cfg  composetest  harbor  my_compose  sc-centos.tar  sc-redis.tar
[root@docker-two ~]# 

2、import导入文件

[root@docker-two ~]# docker import --help

Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Import the contents from a tarball to create a filesystem image

Options:
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Set commit message for imported image
–platform string Set platform if server is multi-platform capable
[root@docker-two ~]#

[root@docker-two ~]# docker import sc-centos.tar centos:7
sha256:a77b4cd2983fc97d7b3391799db77acd880c4b642a30748ae2f58550f1027bdb
[root@docker-two ~]# docker images
REPOSITORY                      TAG            IMAGE ID       CREATED         SIZE
centos                          7              a77b4cd2983f   5 seconds ago   204MB
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pod️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值