一、dockfile 说明

docker 运行一个自定义的image时,就需要自己来定义,这时候就需要dockfile了。

二、docker commit生成新镜像

2.1.运行容器



# docker run -it centos /bin/bash



2.2.安装vim



# yum install vim



2.3.将容器打包成新镜像



# docker commit 07929e3af3ef cent-vim



2.4.查看新镜像



# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cent-vim            latest              4659d1ec001d        7 seconds ago       225MB
centos              latest              1e1148e4cc2c        8 days ago          202MB
nginx               latest              568c4670fa80        2 weeks ago         109MB
ubuntu              latest              93fd78260bd1        3 weeks ago         86.2MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB



这样操作,非常不便,切容易出错,如何有更好方式来定义镜像呢?这就需要dockfile了


三、dockfile

3.1.编辑dockfile文件



# vim Dockerfile  FROM centos RUN yum update && yum install vim -y




3.2.build构建镜像


# docker build -t centos-vim .   #-t 将新镜像命名为centos-vim
Sending build context to Docker daemon   16.9kB
Step 1/2 : FROM centos  #镜像来源
 ---> 1e1148e4cc2c
Step 2/2 : RUN yum update && yum install vim -y  #构建时执行的命令
 ---> Running in d7a98cfdffb7
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: centos.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.cn99.com
No packages marked for update
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.cn99.com
Resolving Dependencies
--> Running transaction check
---> Package vim-enhanced.x86_64 2:7.4.160-5.el7 will be installed
....
Removing intermediate container d7a98cfdffb7
 ---> 82fd472e3387
Successfully built 82fd472e3387  #构建成功,镜像ID为82fd472e3387
Successfully tagged centos-vim:latest #为镜像添加一个TAG



查看镜像:



# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos-vim          latest              82fd472e3387        2 minutes ago       327MB
cent-vim            latest              4659d1ec001d        About an hour ago   225MB
centos              latest              1e1148e4cc2c        8 days ago          202MB
nginx               latest              568c4670fa80        2 weeks ago         109MB
ubuntu              latest              93fd78260bd1        3 weeks ago         86.2MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB



3.3.构建镜像时拷贝文件

3.3.1.文件内容:



# cat ckl.txt  The narcissus bloom every year.



3.3.2.dockfile 内容



# vim Dockerfile  FROM centos RUN yum update && yum install vim -y COPY ckl.txt /



3.3.3.构建镜像



# docker build -t cent-ckl-vim .



# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cent-ckl-vim        latest              fc090867ce91        42 seconds ago      327MB
centos-vim          latest              82fd472e3387        16 minutes ago      327MB
cent-vim            latest              4659d1ec001d        About an hour ago   225MB
centos              latest              1e1148e4cc2c        8 days ago          202MB
nginx               latest              568c4670fa80        2 weeks ago         109MB
ubuntu              latest              93fd78260bd1        3 weeks ago         86.2MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB



3.3.4.运行容器查看内容



# docker run -it cent-ckl-vim
[root@050821c80ca8 /]# 
[root@050821c80ca8 /]# 
[root@050821c80ca8 /]# ls
anaconda-post.log  bin  ckl.txt  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@050821c80ca8 /]# 
[root@050821c80ca8 /]# 
[root@050821c80ca8 /]# cat ckl.txt 
The narcissus bloom every year.  #拷贝的文件
[root@050821c80ca8 /]# 
[root@050821c80ca8 /]# 
[root@050821c80ca8 /]# vim 1.txt #vim 已经按照



3.3.5.拷贝容器的另一种方式



# vim Dockerfile  FROM centos RUN yum update && yum install vim -y ADD nginx-1.12.2.tar.gz /



构建镜像:



# docker build -t centos-ckl-ng .
Sending build context to Docker daemon      1MB
Step 1/3 : FROM centos  
 ---> 1e1148e4cc2c
Step 2/3 : RUN yum update && yum install vim -y
 ---> Using cache  #之前已经构建过了以上步骤,所以这里使用了缓存
 ---> 82fd472e3387
Step 3/3 : ADD nginx-1.12.2.tar.gz /
 ---> 248da05fb49e
Successfully built 248da05fb49e
Successfully tagged centos-ckl-ng:latest



查看镜像:



# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos-ckl-ng       latest              248da05fb49e        23 seconds ago      333MB
centos-vim          latest              82fd472e3387        24 minutes ago      327MB
cent-vim            latest              4659d1ec001d        About an hour ago   225MB
centos              latest              1e1148e4cc2c        8 days ago          202MB
nginx               latest              568c4670fa80        2 weeks ago         109MB
ubuntu              latest              93fd78260bd1        3 weeks ago         86.2MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB



运行镜像容器:



# docker run -it centos-ckl-ng  
[root@a7bdc5e9ecf4 /]# 
[root@a7bdc5e9ecf4 /]# 
[root@a7bdc5e9ecf4 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  nginx-1.12.2  opt  proc  root  run  sbin  srv  sys  tmp  usr  var



#tar 包已经被解压,COPY则不会


3.3.6.构建镜像启动后运行命令CMD

编辑文件:



# vim Dockerfile 
RUN yum install -y httpd
RUN yum install -y httpd
FROM centos
RUN yum update && yum install vim -y
ADD nginx-1.12.2.tar.gz /
RUN yum install -y httpd && yum -y install initscripts pm-utils
CMD ["service","httpd","start"]



构建镜像:



# docker build -t cent-ckl-http .



查看镜像:



# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cent-ckl-http       latest              d848cf5482e6        4 seconds ago       443MB
centos-ckl-ng       latest              248da05fb49e        22 minutes ago      333MB
centos-vim          latest              82fd472e3387        About an hour ago   327MB
cent-vim            latest              4659d1ec001d        2 hours ago         225MB
centos              latest              1e1148e4cc2c        8 days ago          202MB
nginx               latest              568c4670fa80        2 weeks ago         109MB
ubuntu              latest              93fd78260bd1        3 weeks ago         86.2MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB




运行镜像的容器:



# docker run -it cent-ckl-http 
Redirecting to /bin/systemctl start httpd.service  #已经执行了CMD的命令
Failed to get D-Bus connection: Operation not permitted



这个的原因是因为dbus-daemon没能启动。其实systemctl并不是不可以使用。将你的CMD或者entrypoint设置为/usr/sbin/init即可。会自动将dbus等服务启动起来。 

然后就可以使用systemctl了。



# docker run -it --privileged cent-ckl-http init
[FAILED] Failed to start LSB: Bring up/down networking.
See 'systemctl status network.service' for details.



没有搞定!!!

3.3.7.构建镜像设置环境变量及目录

修改dockfile文件



# vim Dockerfile 
FROM centos
RUN yum update && yum install vim -y && mkdir /tmp/soft
ADD nginx-1.12.2.tar.gz /tmp/soft
WORKDIR /tmp/soft/nginx-1.12.2
ENV NAME=ckl
RUN yum install -y httpd && yum -y install initscripts pm-utils
CMD ["service","httpd","start"]
CMD ["echo","$NAME"]




构建镜像文件:



# docker build -t cent-ckl-http .



启动镜像容器:



# docker run -it cent-ckl-http /bin/bash
[root@355cc5d1501e nginx-1.12.2]#    #当前目录
[root@355cc5d1501e nginx-1.12.2]# 
[root@355cc5d1501e nginx-1.12.2]# echo $NAME   #变量
ckl



三、dockfile 参数说明



Dockerfile 一般分为四部分:基础镜像信息、维护者信息、镜像操作指令和容器启动时执行指令 FROM:指定基础镜像,必须为第一个命令 MAINTAINER: 维护者信息 RUN:构建镜像时执行的命令 ADD:将本地文件添加到容器中,tar类型文件会自动解压,可以访问网络资源,类似wget COPY:功能类似ADD,但是是不会自动解压文件,也不能访问网络资源 CMD:构建容器后调用,也就是在容器启动时才进行调用。 ENTRYPOINT:配置容器,使其可执行化。配合CMD可省去"application",只使用参数。 LABEL:用于为镜像添加元数据 ENV:设置环境变量 EXPOSE:指定于外界交互的端口 VOLUME:用于指定持久化目录 WORKDIR:工作目录,类似于cd命令 USER:指定运行容器时的用户名或 UID,后续的 RUN 也会使用指定用户。使用USER指定用户时,可以使用用户名、UID或GID,或是两者的组合。当服务不需要管理员权限时,可以通过该命令指定运行用户。并且可以在之前创建所需要的用户 ARG:用于指定传递给构建运行时的变量 ONBUILD:用于设置镜像触发器



四、配置nginx安装示例

4.1.编辑dockfile文件



vim Dockerfile 
FROM centos
MAINTAINER ckl
RUN yum update && yum -y install vim zlib zlib-devel openssl openssl-devel gcc-c++ pcre pcre-devel initscripts pm-utils
RUN mkdir /tmp/soft
RUN adduser ckl
ADD nginx-1.12.2.tar.gz /tmp/soft
WORKDIR /tmp/soft/nginx-1.12.2
RUN ./configure --prefix=/usr/local/nginx --user=ckl --group=ckl --with-http_ssl_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH 
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]



4.2.构建镜像



docker build -t nginx-centos .



4.3.启动镜像容器:



docker run -p 80 --name 'ckl-ng' -d nginx-centos



4.4.已经启动了nginx



# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
4f68eeb38e28        nginx-centos        "/usr/local/nginx/sb…"   7 minutes ago       Up 7 minutes        0.0.0.0:32768->80/tcp   ckl-ng



4.5.访问:



# curl http://172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>





转载于:https://blog.51cto.com/ckl893/2331369