您的位置:首页 > 运维架构 > Docker

Docker系列之镜像

2019-01-22 16:34 148 查看

一、镜像是什么

       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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  docker 镜像