4、CentOS7 安装Docker之Dockerfile
Docker之Dockerfile
上面我们学习了docker的理念,架构以及安装指令。本章我们来学习如何写Dockerfile(注意:D必须是大写),Dockerfile的步骤,如下图:
先查看下本地的镜像,选一个作为base image:
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest c82521676580 8 days ago 109 MB
docker.io/alexeiled/docker-oracle-xe-11g latest f7304758169d 2 years ago 2.39 GB
1、创建Dockerfile的安装包文件文件存放目录:
[[email protected] ~]# mkdir /opt/docker-file
- 以下是编写好的Dockerfile v1版:
[[email protected] docker-file]# cat Dockerfile
#This docker file
#VERSION 1
#Author:yanyb
#Base image 可以是镜像名字或者镜像ID
FROM centos
#Maintainer 作者 邮箱
MAINTAINER yanyb [email protected]
#put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command 在容器里运行nginx需要依赖包的命令
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# change dir to /usr/local/src/nginx-1.12.2 去到nginx解压目录
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx 在容器里运行命令进行编译安装
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
#env 将nginx启动命令加到环境变量里
ENV PATH /usr/local/nginx/sbin:$PATH
#expose 映射80端口
EXPOSE 80
3、查看docker_demo目录情况:
[[email protected] docker-file]# ll
-rw-r--r-- 1 root root 1416 Aug 2 09:00 Dockerfile
-rw-r--r-- 1 root root 981687 Aug 2 08:50 nginx-1.12.2.tar.gz
4、执行docker build进行构建:
[[email protected] ~]# docker build -t centos_nginx:v1 /opt/docker-file/
注意:后面的/opt/docker-file/要指定存放Dockerfile文件和安装包的目录。
当成功后会出现这个,说明执行成功
Successfully built 21273b8e163a
查看新构建的镜像:
5、然后使用构建的镜像启动一个container并开启nginx服务:
[[email protected] docker-file]# docker run -d centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"
e56d38b6821877416bd260bead0e4d1fe0152f74711ee3676263b30fddf3b740
然后查看启动的container是否在运行:
docker ps
虽然nginx服务开启了,但是port并没有进行映射到本机host,所以这个container并不能进行访问,重新启动一个进行了映射端口的容器
[[email protected] docker-file]# docker run -d -p8080:80 centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"
265c9d91afc2a12e02491588cdbe7354680c70e27aeab25389cb93bfbeb41d4e
再次查看端口映射信息:
[[email protected] docker-file]# docker ps -a | grep 265c9d91afc2
访问刚刚创建容器的nginx:
于是基于Dockerfile的一个简单实例构建完成,现在基于这个Dockerfile文件依次添加其他的指令进行构建添加ENV环境变量指令:
lENV PATH /usr/local/nginx/sbin:$PATH
然后进行构建:
由于在构建的过程中docker会采用缓存的机制,上面的构建过程中包含很多using cache,所以这次构建非常快,如果需要重新构建,不想使用cache需要添加--no-cache
--no-cache Do not use cache when building the image
查看v2版本的镜像:
使用v2版本的镜像启动一个container:
进行访问:
上述启动容器的过程中使用的命令docker run -d -p8081:80 centos_nginx:v2 nginx -g "daemon off;"为什么这里使用的nginx而不是
/usr/local/nginx/sbin/nginx,因为在Dockerfile文化中执行了PATH=/usr/local/nginx/sbin:$PATH,添加到了环境变量
添加指令CMD:
CMD /bin/sh -c 'nginx -g "daemon off;"'
然后进行构建:
[[email protected] docker-file]# docker build -t centos_nginx:v3 /opt/docker-file/
查看v3版本的镜像:
[[email protected] docker-file]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v3 d88d67cb2acc 25 seconds ago 476 MB
然后基于v3版本的镜像启动一个container并查看启动的容器状态:
[[email protected] docker-file]# docker run -d -p8082:80 centos_nginx:v3
1f88c98b49e4b94c10917a5cde59823ac15e3e7a3883e627347788b95093d563
[[email protected] docker-file]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f88c98b49e4 centos_nginx:v3 "/bin/sh -c '/bin/..." 18 seconds ago Up 18 seconds 0.0.0.0:8082->80/tcp boring_nobel
最后进行访问:
新增加的CMD /bin/sh -c 'nginx -g "daemon off;"'表示
当启动一个container时默认运行的命令,如果在启动container时赋予了command的化,那么
定义的CMD中的命令将不会被执行,而会去执行command的命令
添加entrypoint指令:
ENTRYPOINT ["nginx"]
当ENTRYPOINT和CMD连用时,CMD的命令是ENTRYPOINT命令的参数,两者连用相当于nginx -g "daemon off;"
而当一起连用的时候命令格式最好一致(这里选择的都是json格式的是成功的,如果都是sh模式可以试一下)
开始进行构建v4版本:
[[email protected] docker-file]# docker build -t centos_nginx:v4 /opt/docker-file/
查看新建的镜像:
[[email protected] docker-file]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v4 8c920e8be7fe 57 seconds ago 476 MB
为v4版本的镜像启动一个container并查看容器状态:
[[email protected] docker-file]# docker run -d -p8083:80 centos_nginx:v4
[[email protected] docker-file]# docker ps -l
这里增加一个案例,如果Dockerfile修改成下面:
[[email protected] docker-file]# vi Dockerfile
EXPOSE 80
#This docker file
#VERSION 1
#Author:yanyb
#Base image
FROM centos
#Maintainer
MAINTAINER yanyb [email protected]
#put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
ENTRYPOINT ["nginx"]
CMD ["-g","daemon on;"]
CMD的命令修改为了后台,我们知道如果容器内的进程在后台运行那么容器将不会运行,现在以此构建v5版本镜像:
[[email protected] docker-file]# docker build -t centos_nginx:v5 /opt/docker-file/
查看v5版本的新镜像:
[[email protected] docker-file]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v5 c29039d2a9b7 46 seconds ago 476 MB
现在利用v5版本镜像启动一个container,但是在启动的时候添加后面的command:
[[email protected] docker-file]# docker run -d -p85:80 centos_nginx:v5 -g "daemon off;"
601b338bd1f813468bb4f5f4bd21fd50723532cd256a990fefd0c6c8c8ad3624
可以看见在后面新增了-g "daemon off;",前面说过如果增加了命令那么Dockerfile中的CMD中的命令将不会生效
查看容器运行状态:
[[email protected] docker-file]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
601b338bd1f8 centos_nginx:v5 "nginx -g 'daemon ..." 9 seconds ago Up 8 seconds 0.0.0.0:85->80/tcp mystifying_bell
可以看见容器的运行丝毫没有问题,于是nginx的服务依然还是在前台运行,没有被CMD影响,而新增加的command(-g "daemon off;")
将作为ENTRYPOINT的新的参数以此为准,于是进行访问端口85:http://192.168.91.134:85/
添加VOLUME指令:
[[email protected] docker-file]# cat Dockerfile
#This docker file
#VERSION 1
#Author:yanyb
#Base image
FROM centos
#Maintainer
MAINTAINER yanyb [email protected]
#put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
ENTRYPOINT ["nginx"]
CMD ["-g"]
开始进行构建:
[[email protected] docker-file]# docker build -t centos_nginx:v6 /opt/docker-file/
查看v6版本的镜像:
[[email protected] docker-file]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx v6 59e9f641ea18 37 seconds ago 476 MB
利用该镜像启动一个container并查看启动的容器状态:
[[email protected] docker-file]# docker run -d -p 86:80 --name=nginx6 centos_nginx:v6 -g "daemon off;"
c6222d2833704b25cd7ad9f51a84905502fc9f0ca570200df9de71719308257e
[[email protected] docker-file]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6222d283370 centos_nginx:v6 "nginx -g 'daemon ..." 16 seconds ago Up 15 seconds 0.0.0.0:86->80/tcp nginx6
利用docker exec进入到container中,查看是否存在卷/data:
[[email protected] /]# ll
total 16
-rw-r--r--. 8 root root 15836 Sep 11 15:53 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Sep 11 15:51 bin -> usr/bin
drwxr-xr-x. 2 root root 6 Nov 2 01:42 data
这个/data挂载的目录对应本机host的这个目录:
[[email protected] docker-file]# pwd
/var/lib/docker/volumes/3490a9b7f9773b020343e82c1c4236d976b69eb6a80121eb80612d8c39e02820/_data
现在在本机host上的这个目录创建一个文件:
[[email protected] docker-file]# touch wadeson.sh
[[email protected] docker-file]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 1 21:45 wadeson.sh
然后切换到container中查看是否存在这个文件:
[[email protected] /]# ll /data/
total 0
-rw-r--r--. 1 root root 0 Nov 2 01:45 wadeson.sh
添加ONBUILD指令:
Dockerfile1中base image为A镜像,并在Dockerfile1中定义ONBUILD指令,构建成新镜像B镜像
Dockerfile2中base image为B镜像,构建成新镜像C
当使用镜像B启动的container1不会执行OBNUILD中定义的内容,而使用C镜像启动的container2则会执行ONBUILD定义的内容
[[email protected] docker-file]# cat Dockerfile
# base image
FROM centos
# MAINTAINER
MAINTAINER [email protected]163.com
# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# mount a dir to container
ONBUILD VOLUME ["/data"]
# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
# setup PATH
ENV PATH /usr/local/nginx/sbin:$PATH
# EXPOSE
EXPOSE 80
# the command of entrypoint
ENTRYPOINT ["nginx"]
CMD ["-g"]
使用上面Dockerfile构建镜像版本v7:
使用上面Dockerfile构建镜像版本v7:
[[email protected] docker_demo]# docker run -d -p 87:80 --name=nginx7 centos_nginx:v7 -g "daemon off;"
48f1fe5c71aefc0f9513e8085af8f5b7cdf14fa986fb3b11f2050f18ceefd26e
现在进入到容器内查看是否存在/data:
[[email protected] ~]# docker exec -it nginx7 /bin/bash
[[email protected] nginx-1.12.2]# ll /datals: cannot access /data: No such file or directory
现在修改上面Dockerfile的FROM基于的base image:
[[email protected] docker_demo]# cat Dockerfile
# base image
FROM centos_nginx:v7
# MAINTAINER
MAINTAINER yanyb
利用此Dockerfile构建镜像v8:
[[email protected] docker-file]#docker build -t centos_nginx:v8 /opt/docker-file
可以看见卷/data已经执行了操作,现在启动一个container:
[[email protected] docker-file]#docker run -d -p 88:80 --name=nginx8 centos_nginx:v8 -g "daemon off;"
6c2a847c5f6b59b02f91afecadbfc15c88d1217a477c0421a424bce6e5eb317a
查看容器状态,并进入到容器验证/data目录:
[[email protected] docker-file]#docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c2a847c5f6b centos_nginx:v8 "nginx -g 'daemon ..." 37 seconds ago Up 37 seconds 0.0.0.0:88->80/tcp nginx8
[[email protected] docker_demo]# docker exec -it nginx8 /bin/bash
[[email protected] nginx-1.12.2]# ll /data/
由此可见镜像v8包含了v7 的所有内容,并且增加了ONBUILD的内容