实验 详解Docker的各种操作小实验

一 当镜像有别名时,此时删除ID是删不掉的

[root@localhost overlay2]# docker images | grep nginx
nginx               latest              c39a868aad02        3 days ago          133MB
nginx               web                 c39a868aad02        3 days ago          133MB
[root@localhost overlay2]# docker rmi c39a868aad02		##此时有别名,删除ID时会报错  
Error response from daemon: conflict: unable to delete c39a868aad02 (must be forced) - image is referenced in multiple repositories
[root@localhost overlay2]# docker rmi nginx:latest 
Untagged: nginx:latest
[root@localhost overlay2]# docker images | grep nginx
nginx               web                 c39a868aad02        3 days ago          133MB

二 验证docker 0是所有容器的网桥,同属一个网段

[root@localhost overlay2]# ifconfig 		##发现docker 0 虚拟网卡,地址为172.17.0.1,为所有容器的网桥
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:3bff:fee9:944  prefixlen 64  scopeid 0x20<link>
        ether 02:42:3b:e9:09:44  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 13  bytes 1760 (1.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost overlay2]# docker create -it centos:7 /bin/bash		##测试容器的IP地址,需要装个centos7操作系统环境
Unable to find image 'centos:7' locally
7: Pulling from library/centos
75f829a71a1c: Pull complete 
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:7
dd81304926723c11552fde4b5fa223cf2f629ea6cb579c84d9dcecc1ed26312e
[root@localhost overlay2]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
dd8130492672        centos:7            "/bin/bash"              17 seconds ago      Created                                 kind_hermann
f9eaf968d810        nginx:web           "/docker-entrypoint.…"   9 minutes ago       Up 7 minutes        80/tcp              hopeful_brown
[root@localhost overlay2]# docker start dd8130492672		##启动容器
dd8130492672
[root@localhost overlay2]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
dd8130492672        centos:7            "/bin/bash"              About a minute ago   Up 20 seconds                           kind_hermann
f9eaf968d810        nginx:web           "/docker-entrypoint.…"   10 minutes ago       Up 8 minutes        80/tcp              hopeful_brown
[root@localhost overlay2]# docker exec -it dd8130492672 /bin/bash		##进入已经在运行的容器中
[root@dd8130492672 /]# yum -y install net-tools
[root@dd8130492672 /]# ifconfig 		##查看容器IP地址,发现同属一个网段
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 1399  bytes 11834645 (11.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1346  bytes 75971 (74.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@dd8130492672 /]# exit			##退出容器
exit

三 存出镜像,载入镜像

[root@localhost overlay2]# docker save -o /opt/nginx nginx:web		##把镜像命令为nginx存在opt目录下 
[root@localhost overlay2]# docker rmi nginx:web				##删除镜像 
Untagged: nginx:web
Untagged: nginx@sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Deleted: sha256:c39a868aad02a383c7e490e0fc4a5b0217f667f2de764bc2755e315a5adf64a1
Deleted: sha256:1af47386c12cb24b362bd785e71058ab675fbdcbda8e7a7ba0dd10c3cff75a2a
Deleted: sha256:25ee02f9e42f8cdca7ca8bc522cb69ac4a86a55401818a13c7a01e52a81894fe
Deleted: sha256:b1d2bc3292aa3d64794f99d2885d35b454993b4af24ad350a969d27925db7ebb
Deleted: sha256:4216e20d59f9fc90bccdd14493452532806d4803a25514366de2a9c5560624d0
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c
[root@localhost overlay2]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@localhost overlay2]# docker load < /opt/nginx 			##载入镜像
d0fe97fa8b8c: Loading layer  72.49MB/72.49MB
2baf69a23d7a: Loading layer  64.54MB/64.54MB
2f57e21e4365: Loading layer  3.072kB/3.072kB
aee208b6ccfb: Loading layer  4.096kB/4.096kB
7b5417cae114: Loading layer  3.584kB/3.584kB
Loaded image: nginx:web
[root@localhost overlay2]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               web                 c39a868aad02        3 days ago          133MB

四 上传镜像

[root@localhost overlay2]# docker tag nginx:web cwj/nginx:web
[root@localhost overlay2]# docker login 
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 
Password: 
docker push cwj/nginx:web		##上传镜像

五 容器导出,导入

[root@localhost overlay2]# docker export f9eaf968d810 > nginx_c		##容器导出
[root@localhost overlay2]# ll
总用量 132172
drwx------ 4 root root        58 11月  9 18:50 distribution
drwx------ 4 root root        37 11月  9 17:36 imagedb
drwx------ 5 root root        45 11月  9 19:19 layerdb
-rw-r--r-- 1 root root 135337984 11月  9 19:56 nginx_c
-rw------- 1 root root       660 11月  9 19:43 repositories.json
[root@localhost overlay2]# docker stop f9eaf968d810 			##删除容器前一定要先停止运行
f9eaf968d810
[root@localhost overlay2]# docker rm f9eaf968d810 			##删除容器
f9eaf968d810
[root@localhost overlay2]# docker images 
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
nginx                 web                 c39a868aad02        3 days ago          133MB
centos                7                   7e6257c9f8d8        3 months ago        203MB
paigeeworld/centos7   latest              4cbe7aa905e7        6 years ago         382MB
[root@localhost overlay2]# docker rmi c39a868aad02 
Untagged: nginx:web
Deleted: sha256:c39a868aad02a383c7e490e0fc4a5b0217f667f2de764bc2755e315a5adf64a1
Deleted: sha256:1af47386c12cb24b362bd785e71058ab675fbdcbda8e7a7ba0dd10c3cff75a2a
Deleted: sha256:25ee02f9e42f8cdca7ca8bc522cb69ac4a86a55401818a13c7a01e52a81894fe
Deleted: sha256:b1d2bc3292aa3d64794f99d2885d35b454993b4af24ad350a969d27925db7ebb
Deleted: sha256:4216e20d59f9fc90bccdd14493452532806d4803a25514366de2a9c5560624d0
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c
[root@localhost overlay2]# docker images 
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
centos                7                   7e6257c9f8d8        3 months ago        203MB
paigeeworld/centos7   latest              4cbe7aa905e7        6 years ago         382MB
[root@localhost overlay2]# cat nginx_c | docker import - nginx:web		##容器导入,只会生成镜像,而不会创建容器
sha256:71f2ea5d8f475762fd9704c3c88560340a0b4d53d7d570ece6fb1f99c6607629
[root@localhost overlay2]# docker images 
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
nginx                 web                 71f2ea5d8f47        10 seconds ago      131MB
centos                7                   7e6257c9f8d8        3 months ago        203MB
paigeeworld/centos7   latest              4cbe7aa905e7        6 years ago         382MB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值