docker镜像的导入和导出

Posted 未末0902

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker镜像的导入和导出相关的知识,希望对你有一定的参考价值。


前言

**镜像的导入和导出在docker日常使用中非常频繁,所以说一定要学会这个操作 接下来地一个小练习希望大家好好体会.**

一、images 练习:

1.拉取centos:7和Ubuntu的镜像
2.在里面安装软件 tree,nginx
3.镜像导出
4.在另外一台机器里导入

代码如下(示例):

1.拉取centos:7和Ubuntu的镜像

代码如下(示例):

root@sanchuang-server:~# docker run -it  --name ubuntu-liu-4 -d  ubuntu
root@sanchuang-server:~# docker exec -it ubuntu-liu-4 /bin/bash

root@a1712fe90c29:/# cat /etc/issue
Ubuntu 20.04.2 LTS \\n \\l

root@a1712fe90c29:/#

2.在里面安装软件 tree,nginx

docker pull nginx
直接拉取就可以了,这一步比较简单.

3.镜像导出

代码如下(示例):

root@sanchuang-server:~# docker save -o sc-ubuntu.tar ubuntu
root@sanchuang-server:~# ls
sc-ubuntu.tar  snap
scp传递到另外一台服务器上
root@sanchuang-server:~# scp sc-ubuntu.tar root@192.168.0.17:/root
The authenticity of host '192.168.0.17 (192.168.0.17)' can't be established.
ECDSA key fingerprint is SHA256:ekPSYt7PzharlAuHlYLxgvsmdJewVxXNNYJgQp6Pqro.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.17' (ECDSA) to the list of known hosts.
root@192.168.0.17's password:
sc-ubuntu.tar                                                                              100%   72MB  42.1MB/s   00:01
root@sanchuang-server:~#

4.在另外一台机器里导入*

代码如下(示例):

[root@sc-docker ~]# ls
anaconda-ks.cfg  sanchuang  sc-ubuntu.tar
[root@sc-docker ~]# docker images
REPOSITORY                 TAG       IMAGE ID       CREATED        SIZE
redis                      latest    aa4d65e670d6   2 weeks ago    105MB
nginx                      latest    08b152afcfae   2 weeks ago    133MB
mysql                      5.7.35    8cf625070931   2 weeks ago    448MB
daocloud.io/nginx          latest    6084105296a9   5 months ago   133MB
hello-world                latest    d1165f221234   5 months ago   13.3kB
centos/python-35-centos7   latest    2db34dda8fd8   2 years ago    645MB
[root@sc-docker ~]# docker load -i sc-ubuntu.tar
7555a8182c42: Loading layer [==================================================>]  75.16MB/75.16MB
Loaded image: ubuntu:latest
[root@sc-docker ~]# docker images
REPOSITORY                 TAG       IMAGE ID       CREATED        SIZE
ubuntu                     latest    1318b700e415   2 weeks ago    72.8MB
redis                      latest    aa4d65e670d6   2 weeks ago    105MB
nginx                      latest    08b152afcfae   2 weeks ago    133MB
mysql                      5.7.35    8cf625070931   2 weeks ago    448MB
daocloud.io/nginx          latest    6084105296a9   5 months ago   133MB
hello-world                latest    d1165f221234   5 months ago   13.3kB
centos/python-35-centos7   latest    2db34dda8fd8   2 years ago    645MB
[root@sc-docker ~]#


root@sanchuang-server:~# docker export -o sc-ubuntu2.tar ubuntu-liu-4
root@sanchuang-server:~# ls
sc-ubuntu2.tar  sc-ubuntu.tar  snap
root@sanchuang-server:~#

[root@sc-docker ~]# docker import  sc-ubuntu2.tar   sc-ubuntu:2.0
sha256:619407bc7aa57128a71365440910b107db3eaad949d0e383aac8bfbc24c668bc
[root@sc-docker ~]# docker images
REPOSITORY                 TAG       IMAGE ID       CREATED          SIZE
sc-ubuntu                  2.0       619407bc7aa5   3 seconds ago    72.8MB
<none>                     <none>    73a107c7cc6c   59 seconds ago   72.8MB
ubuntu                     latest    1318b700e415   2 weeks ago      72.8MB
redis                      latest    aa4d65e670d6   2 weeks ago      105MB
nginx                      latest    08b152afcfae   2 weeks ago      133MB
mysql                      5.7.35    8cf625070931   2 weeks ago      448MB
daocloud.io/nginx          latest    6084105296a9   5 months ago     133MB
hello-world                latest    d1165f221234   5 months ago     13.3kB
centos/python-35-centos7   latest    2db34dda8fd8   2 years ago      645MB
[root@sc-docker ~]#


**

二.拓展:

1.导出镜像分类:

静态:存放的镜像: docker save  ---》docker load
动态:运行在容器里的镜像:docker export --》docker import

-p 做端口映射的时候,其实背后就是在iptables里添加DNAT和SNAT策略


[root@sc-docker ~]# docker run -it -d --name  luojiajun  centos:7 /bin/bash
[root@sc-docker ~]# docker run -it -d --name luojiajun7 -p 9998:90    centos:7
3af5f37ee5925249aa6d0fd51c7b6cb7689a8aaf4b054e64f472303e5206f9b0
[root@sc-docker ~]# docker ps
CONTAINER ID   IMAGE      COMMAND       CREATED         STATUS         PORTS                                   NAMES
3af5f37ee592   centos:7   "/bin/bash"   5 seconds ago   Up 3 seconds   0.0.0.0:9998->90/tcp, :::9998->90/tcp   luojiajun7
822a3a150d08   centos:7   "/bin/bash"   8 minutes ago   Up 8 minutes                                           luojiajun5
3ea804c5dd0c   centos:7   "/bin/bash"   2 hours ago     Up 2 hours                                             luojiajun3
69eb9fc6eef8   centos:7   "/bin/bash"   3 hours ago     Up 3 hours     0.0.0.0:8090->90/tcp, :::8090->90/tcp   luojiajun2
[root@sc-docker ~]# iptables -L -t nat -n
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
MASQUERADE  all  --  172.17.0.0/16        0.0.0.0/0
MASQUERADE  tcp  --  172.17.0.2           172.17.0.2           tcp dpt:90
MASQUERADE  tcp  --  172.17.0.5           172.17.0.5           tcp dpt:90

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
DOCKER     all  --  0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL

Chain DOCKER (2 references)
target     prot opt source               destination
RETURN     all  --  0.0.0.0/0            0.0.0.0/0
DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8090 to:172.17.0.2:90
DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:9998 to:172.17.0.5:90
[root@sc-docker ~]# docker run -it -d --name luojiajun9 -p 9999:90    centos:7
a9db103780634654c8958d9c3103e76a45935d03708822e174383d4c905d2fc8
[root@sc-docker ~]# docker ps
CONTAINER ID   IMAGE      COMMAND       CREATED              STATUS              PORTS                                   NAMES
a9db10378063   centos:7   "/bin/bash"   3 seconds ago        Up 2 seconds        0.0.0.0:9999->90/tcp, :::9999->90/tcp   luojiajun9
3af5f37ee592   centos:7   "/bin/bash"   About a minute ago   Up About a minute   0.0.0.0:9998->90/tcp, :::9998->90/tcp   luojiajun7
822a3a150d08   centos:7   "/bin/bash"   10 minutes ago       Up 10 minutes                                               luojiajun5
3ea804c5dd0c   centos:7   "/bin/bash"   2 hours ago          Up 2 hours                                                  luojiajun3
69eb9fc6eef8   centos:7   "/bin/bash"   3 hours ago          Up 3 hours          0.0.0.0:8090->90/tcp, :::8090->90/tcp   luojiajun2


2.查看路由器详情:

[root@sc-docker ~]# docker container inspect luojiajun10 查看容器的详情
[
    {

 "MacAddress": "02:42:ac:11:00:07",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "8492b65ba9220655d8c9cad020e0eeaa2a4dbd25cbf3f3d6f44edc2fec4a68c2",
                    "EndpointID": "4f13d3186fd4e0a9aca5663435716fddd9ec0de0b8c3e8673447fc0e00a91503",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.7",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:07",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@sc-docker ~]#



总结

1.我相信同学们一定掌握了镜像的相关操作,针不戳!
其实镜像的导出中-p 做端口映射的时候,其实背后就是在iptables里添加DNAT和SNAT策略,这点还需要好好理解一下.

2.下节的内容我会更新镜像的制作和使用,大家一起加油哈.

**

3.如果你看到这里了,麻烦👍 + 关注哈,感谢支持,码字不易,谢谢理解.

**

以上是关于docker镜像的导入和导出的主要内容,如果未能解决你的问题,请参考以下文章

docker镜像的导入和导出

Docker 导入和导出镜像

Docker 镜像-管理-导入-导出

Docker镜像的导出和导入,打标签,提交到本地仓库

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

docker镜像和容器的导入导出