Docker系列之鏡像

一、鏡像是什麼

       Docker鏡像(Image)類似於虛擬機鏡像,可以將它理解爲一個面向Docker引擎的只讀模板,包含了文件系統。

       鏡像是創建Docker容器的基礎。通過版本管理和增量的文件系統,docker提供了一套十分簡單的機制來創建和更新現有的鏡像,用戶甚至可以從網上下載一個已經做好的應用鏡像,並通過簡單的命令就可以直接使用。

二、鏡像和容器的聯繫

       容器其實是在鏡像的最上面加了一層讀寫層,在運行容器裏文件改動時,會先從鏡像裏面把要寫的文件複製到容器自己的文件系統中(讀寫層)。

       如果容器刪除了,最上的讀寫層也就刪除了,改動也就丟失了。所以無論多少個容器共享一個鏡像,所做的寫操作都是從鏡像的文件系統中複製過來操作的,並不會修改鏡像的源文件,這種方式提高磁盤利用率。

       如果想持久化這些改動,可以通過docker commit 將容器保存成一個新鏡像。不太推薦。

三、管理鏡像常用命令
ls              列出鏡像

[root@localhost docker]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        3 weeks ago         1.84kB
nginx            latest              7042885a156a        3 weeks ago         109MB
[root@localhost docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        3 weeks ago         1.84kB
nginx            latest              7042885a156a        3 weeks ago         109MB

build        構建鏡像來自Dockerfile


history     查看鏡像歷史
inspect    顯示一個或多個鏡像詳細信息

[root@localhost docker]# docker image inspect nginx
[
    {
        "Id": "sha256:7042885a156a01cc99e5a531f41ff47ea2facf655d4fc605aa80b216489586a4",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [
            "nginx@sha256:b543f6d0983fbc25b9874e22f4fe257a567111da96fd1d8f1b44315f1236398c"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2018-12-29T03:31:31.255529383Z",
        "Container": "6d43036a59e9e40ae167cde76a9ce35627ff4f4f224403ae6b0d73a2dfdfe90a",
        "ContainerConfig": {
            "Hostname": "6d43036a59e9",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,

pull          從鏡像倉庫拉取鏡像

[root@localhost docker]# docker pull nginx:1.14
1.14: Pulling from library/nginx
177e7ef0df69: Already exists 
132d4353ecd5: Pull complete 
9482632c8a8f: Pull complete 
Digest: sha256:98f78a1dde1ba9c9fbb10671b14a74fcff97f0cc85c182e217618397fcaf63fa
Status: Downloaded newer image for nginx:1.14

push     推送一個鏡像到鏡像倉庫
rm     移除一個或多個鏡像

[root@localhost docker]# docker image rm nginx:1.14
Untagged: nginx:1.14
Untagged: nginx@sha256:98f78a1dde1ba9c9fbb10671b14a74fcff97f0cc85c182e217618397fcaf63fa
Deleted: sha256:3f55d5bb33f3ae6e7f232c82f3bc09f2aa8029d9d213bf69324c95ac1cb9d7ae
Deleted: sha256:d178dd6080618d71e1b85319821b265d20c422511f59f58f4ad84d6341a497a2
Deleted: sha256:2784880ce5781d29eed1af54c19774de0f6fabd3d63db6e0bd2103d4febedbc0

prune     移除未使用的鏡像。沒有被標記或被任何容器引用的。

[root@localhost docker]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

tag     創建一個引用源鏡像標記目標鏡像
export     導出容器文件系統到tar歸檔文件
import     導入容器文件系統tar歸檔文件創建鏡像
save     保存一個或多個鏡像到一個tar歸檔文件

[root@localhost docker]# docker image save nginx1 -o nginx.tar
[root@localhost docker]# ll
總用量 110268
-rw-r--r-- 1 root root        67 1月  22 15:06 daemon.json
-rw------- 1 root root       244 1月  22 12:02 key.json
-rw------- 1 root root 112903680 1月  22 16:31 nginx.tar

load 加載鏡像來自tar歸檔或標準輸入

[root@localhost docker]# docker image load -i nginx.tar 
7b4e562e58dc: Loading layer [==================================================>]  58.44MB/58.44MB
c9c2a3696080: Loading layer [==================================================>]  54.44MB/54.44MB
b7efe781401d: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: nginx1:latest



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章