實驗 詳解Docker的各種操作小實驗

Mr_ChenWJ發表於2020-11-10

一 當映象有別名時,此時刪除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

相關文章