Docker image Introduce

本文详细介绍了如何使用Dockerfile构建自定义的Docker镜像,并通过实例演示了从基本镜像出发,逐步构建及运行容器的过程。文中还解释了Docker镜像的工作原理及其分层特性。
Docker 的image是运行的基本.
例如我们build一个image时, 在Dockerfile每条指令会产生一个可读写的image, 下一条指令使用上一条指令产生的image为基础, 继续产生image(然后删除上一个image), 如果指令没有对image有修改的动作, 那么可以使用image cache. 所有的指令执行完, 生成最终的image.
然后我们就可以使用image来创建并运行container了.

例如我当前本地有一个images, centos, TAG是centos5, 我需要使用它作为base image, 来创建我自己的image.
[root@150 docker]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos                    centos5             d5844d902074        2 days ago          467.1 MB


创建一个空目录, 并在这个空目录中写一个简单的Dockerfile内容如下 : 
# vi Dockerfile 
FROM centos:centos5
CMD ["-l"]
ENTRYPOINT ["ls"]


使用docker build .来创建image.
[root@150 docker]# docker build .
Sending build context to Docker daemon  2.56 kB
Sending build context to Docker daemon 
Step 0 : FROM centos:centos5
 ---> d5844d902074   # 这个ID是centos:centos5的IMAGE id
Step 1 : CMD ["-l"]
 ---> Running in 9fd2f16fcf97   # 这个ID是执行第一条指令CMD ["-l]时产生的中间过程IMAGE ID
 ---> e253cb9b0ea4   # 这个ID是执行第一条指令CMD ["-l]后产生的IMAGE ID
Removing intermediate container 9fd2f16fcf97   #  移除中间image
Step 2 : ENTRYPOINT ["ls"]
 ---> Running in 52c06c9fae1b  
 ---> a18fae31b310
Removing intermediate container 52c06c9fae1b
Successfully built a18fae31b310    # 最终的ID, 可以使用docker images看到这个ID


[root@150 docker]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>                    <none>              a18fae31b310        14 minutes ago      467.1 MB
centos                    centos5             d5844d902074        2 days ago          467.1 MB


使用这个image运行
[root@150 docker]# docker run a18fae31b310
total 64
drwxr-xr-x   2 root root 4096 Sep 26 12:21 bin
drwxr-xr-x   4 root root  340 Sep 30 10:29 dev
drwxr-xr-x  39 root root 4096 Sep 30 10:29 etc
drwxr-xr-x   2 root root 4096 May 11  2011 home
drwxr-xr-x   6 root root 4096 Sep 26 12:20 lib
drwxr-xr-x   7 root root 4096 Sep 26 12:20 lib64
drwx------   2 root root 4096 Sep 26 12:19 lost+found
drwxr-xr-x   2 root root 4096 May 11  2011 media
drwxr-xr-x   2 root root 4096 May 11  2011 mnt
drwxr-xr-x   2 root root 4096 May 11  2011 opt
dr-xr-xr-x 372 root root    0 Sep 30 10:29 proc
drwxr-x---   2 root root 4096 May 11  2011 root
drwxr-xr-x   2 root root 4096 Sep 26 12:21 sbin
drwxr-xr-x   3 root root 4096 Sep 26 12:21 selinux
drwxr-xr-x   2 root root 4096 May 11  2011 srv
drwxr-xr-x  13 root root    0 Sep 30  2014 sys
drwxrwxrwt   2 root root 4096 Sep 26 12:21 tmp
drwxr-xr-x  14 root root 4096 Sep 26 12:20 usr
drwxr-xr-x  17 root root 4096 Sep 26 12:19 var

[root@150 docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES
ae77e79c4f58        a18fae31b310        ls -l               29 seconds ago      Exited (0) 28 seconds ago                          desperate_lovelace 

[root@150 docker]# docker rm ae77e79c4f58
ae77e79c4f58
[root@150 docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


image的介绍 : 

Introduction

Docker image Introduce - 德哥@Digoal - PostgreSQL research

 

In Docker terminology, a read-only Layer is called an image. An image never changes.

Since Docker uses a Union File System, the processes think the whole file system is mounted read-write. But all the changes go to the top-most writeable layer, and underneath, the original file in the read-only image is unchanged. Since images don't change, images do not have state.

Docker image Introduce - 德哥@Digoal - PostgreSQL research

 

Parent Image

Docker image Introduce - 德哥@Digoal - PostgreSQL research

Each image may depend on one more image which forms the layer beneath it. We sometimes say that the lower image is the parent of the upper image.

Base Image

An image that has no parent is a base image.

Image IDs

All images are identified by a 64 hexadecimal digit string (internally a 256bit value). To simplify their use, a short ID of the first 12 characters can be used on the command line. There is a small possibility of short id collisions, so the docker server will always return the long ID.


[参考]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值