3.5构建自己的镜像

在本节中,我们基于一个centos base image制作一个安装有vim的镜像。

镜像制作支持两种方式:

  • 基于容器构建镜像

  • 基于Dockerfile构建镜像(推荐)

前提条件

主机已具有centos镜像,如主机不满足条件,可使用如下命令拉取镜像

[root@master hello-world]# docker pull centos 
Using default tag: latest
latest: Pulling from library/centos
d8d02d457314: Pull complete 
Digest: sha256:307835c385f656ec2e2fec602cf093224173c51119bbebd602c53c3653a3d6eb
Status: Downloaded newer image for centos:latest
[root@master hello-world]# docker images 
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
centos                 latest              67fa590cfc1c        6 days ago          202MB

基于容器构建镜像

1.以交互的方式运行一个centos容器

[root@master hello-world]# docker run -it centos

2.在centos容器中安装vim,验证vim功能,并退出容器

[root@6de30d9433e2 /]# yum install -y vim 
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.cn99.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
 ...

[root@6de30d9433e2 /]# vim
[root@6de30d9433e2 /]# exit
exit

3.构建镜像并查看生成的镜像

[root@master hello-world]# docker container ls -a 
CONTAINER ID     IMAGE      COMMAND       CREATED             STATUS                       PORTS                                               NAMES
6de30d9433e2     centos     "/bin/bash"   4 minutes ago       Exited (0) 24 seconds ago                                                        clever_curie

[root@master hello-world]# docker commit 6de30d9433e2 moluo/centos-vim 
sha256:aa853df964e0c73cf3ae1b9828044e37441f960affd8ef93fe68125c1f7618c2

[root@master hello-world]# docker images 
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
moluo/centos-vim       latest              aa853df964e0        8 seconds ago       369MB

扩展:产看镜像层次可以使用:docker history [OPTIONS] IMAGE

[root@master docker-centos-vim]#  docker history moluo/centos-vim
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
92f66f7b9d52        3 minutes ago       /bin/sh -c yum install -y vim                   167MB               
67fa590cfc1c        6 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           6 days ago          /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
<missing>           6 days ago          /bin/sh -c #(nop) ADD file:4e7247c06de9ad117…   202MB

基于Dockerfile构建镜像

1.新建工作目录并打开

[root@master vagrant]# mkdir docker-centos-vim
[root@master vagrant]# cd docker-centos-vim

vim Dockerfile

2.新建Dockerfile文件

[root@master docker-centos-vim]# vi Dockerfile

Dockerfile文件内容如下:

FROM centos
RUN yum install -y vim

3.构建镜像并查看生成的镜像

[root@master docker-centos-vim]# docker build -t moluo/centos-vim .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos
 ---> 67fa590cfc1c
Step 2/2 : RUN yum install -y vim
 ---> Running in 466315518e1a
...

[root@master docker-centos-vim]# docker images 
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
moluo/centos-vim       latest              92f66f7b9d52        14 seconds ago      369MB

Last updated

Was this helpful?