docker 镜像的制作方法

一、docker image 的制作两种方法:
 

方法1:docker commit     # 保持 container 的当前状态到image后,然后生成对应的 image

方法2:docker build         # 使用dockerfile 文件自动化制作 image

方法1:docker commit

1.创建一个安装好apache 工具的容器镜像。

命令是:

[root@localhost]#docker run -it docker.io/centos:latest  /bin/bash              #运行docker 镜像

[root@localhost]# yum install httpd                      # 在container 中安装 apache 软件包

[root@localhost]# exit   #退出容器镜像

 

效果:下图是没有安装apache 服务的镜像

2.根据容器当前状态做一个image 镜像:创建一个安装了apache 工具的centos 镜像

语法:docker  commit <container 的ID>或者<image_name>

例如:{{

1.查看关闭的镜像

[root@localhost]#docker ps -a

注意:目前在整个Linux的docker中只有一个centos的镜像,上图中所有的历史打开的那个镜像都是基于那个唯一的docker centos镜像。

2.创建 镜像

[root@localhost]# docker commit 2c5f2396d3f  docker.io/centos:latest

如下图:

查看容器中的镜像:

[root@localhost]# docker images

注意:

docker commit 2c5f2396d3f  docker.io/centos:latest 这个命令中latest也可以才成nginx,就成了docker commit 2c5f2396d3f  docker.io/centos:nginx ,这个是随便修改的。如下图:

3.使用 新创建的 docker.io/centos:nginx镜像,生成一台容器实例:

镜像,生成一台容器实例:

[root@localhost]# docker run -it docker.io/centos:nginx  /bin/bash

[root@localhost]# rpm -qa httpd #查看,已经安装好apache命令

当返回:

httpd-2.4.6-67.el7.centos.2x.x86_64  #说明基于apache 镜像的容器创建成功。

}}

方法2通过:docker build 创建一个基于centos的httpd web 服务器镜像。

使用docker build 创建镜像时,需要使用 dockerfile 文件自动化制作image 镜像

注意:dockerfile 有点像源码编译时./configure后产生的 Makefile

 

1.创建 docker-build目录

[root@localhost]# mkdir /docker -build

2.进入这个目录

[root@localhost]# cd /docker-build

3.创建一个文件 Dockerfile

[root@localhost docker-build]# touch Dockerfile

注意:make自动化编译时需要makefile文件,自动化创建docker 镜像时,需要Dockerfile

4.编辑dockerfile

Dockerfile 用来创建一个自定义的image,包含了用户指定的软件依赖等。

[root@localhost]#vim Dockerfile

写入的内容:

FROM docker.io/centos:latest               #我是基于那个镜像来做的 

MAINTAINER    <[email protected]>             #作者是谁<镜像创建者>

RUN yum  -y  install httpd                                  #做什么《拿这个镜像docker.io/centos:latest运行实例,然后执行yum命令,进行安装》

ADD   start.sh  /usr/local/bin/start.sh            #把start.sh这个启动脚本添加到/usr/local/bin这个文件下

ADD    index.html    /var/www/html/index.html         #把index.html这个启动脚本添加到/var/www/html这个文件下

 

CMD   echo   hello word  #container 启动时执行的命令或启动的服务,但是一个dockerfile中只能有一条CMD命令,多条则只执行最后一条CMD

注意:ADD 将文件<src>拷贝到新产生的镜像的文件系统对应的路径<dest>,所有拷贝到新镜像重的文件和文件夹权限为0755,uld和gld为0

{{

例如:dockerfile1 中的内容如下:

#vim dockerfile1

FROM Ubuntu

MAINTAINER  XXX

RUN echo hello 1 > test1.txt

RUN echo hello2 > /test2.txt

EXPOSE 80

EXPOSE 81

CMD ["/bin/bash"]

 

}}

5.启动一个东西

[root@localhost docker- build]# echo "/usr/sbin/httpd -DFOREGROUND"  > start.sh     #在DFOREGROUND里面启动一个apache里面用的东西->httpd   <相当于执行了 systemctl start httpd>

6.加载 一个权限

[root@localhost docker- build]# chmod a+x start.sh

7.创建一个index.html 文件

[root@localhost docker- build]# echo “docker image build test” > index.html

8.查看我们创建的文件有没有    #非必敲项

[root@localhost docker- build]#  ls

9.使用build 来创建新的image

语法:docker build -t  父镜像名:镜像的tag    Dockerfile  文件所在的路径

-t:表示tage,镜像名

[root@localhost docker- build]# docker build  -t  docker.io/centos:httpd ./

10.使用docker images查看我们创建的镜像

注意: ./表示当前目录。另外你的当前目录下要包含Dockerfile 

注意:什么是docker镜像?

{

docker 镜像=应用/程序 + 库

}

 

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