DockerFile
1.DockerFile简介
Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本
构建三步骤:编写Dockerfile文件->docker build->docker run
范例:DockerFile示例
//github.com/CentOS/sig-cloud-instance-images/blob/f32666d2af356ed6835942ed753a4970e18bca94/docker/Dockerfile |
2.DockerFile构建过程
总结
3.DockerFile指令
[1].实例1
Docker Hub 中 99% 的镜像都是通过在 base 镜像中安装和配置需要的软件构建出来的
|
[2].实例2
自定义mycentos目的使我们自己的镜像具备如下:登陆后的默认路径|vim编辑器| 查看网络配置ifconfig支持
(1).编写DockerFile
[root@pluto mydocker]# pwd /mydocker [root@pluto mydocker]# vim Dockerfile2
FROM centos:centos6.8
ENV MYPATH /usr/local WORKDIR $MYPATH
RUN yum -y install vim RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH CMD echo “success————–ok” CMD /bin/bash |
(2).构建build
语法:docker build -t 新镜像名字:TAG .
[root@pluto mydocker]# pwd /mydocker [root@pluto mydocker]# ll 总用量 8 -rw-r–r–. 1 root root 144 8月 18 15:48 Dockerfile -rw-r–r–. 1 root root 197 8月 19 03:42 Dockerfile2 [root@pluto mydocker]# docker build -f /mydocker/Dockerfile2 -t centos:centos6.81 . [root@pluto mydocker]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos centos6.81 c325fa072969 About a minute ago 380.9 MB |
(3).运行
语法:docker run -it 新镜像名字:TAG
[root@6cea293ae495 local]# vim host [root@6cea293ae495 local]# pwd /usr/local |
添加vim编辑器成功,我们可以通过这种方式获得自己的工具库
(4).镜像历史
语法:docker history 镜像名
[root@pluto mydocker]# docker images centos REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos centos6.81 c325fa072969 7 minutes ago 380.9 MB
[root@pluto mydocker]# docker history centos:centos6.81 IMAGE CREATED CREATED BY SIZE COMMENT c325fa072969 7 minutes ago /bin/sh -c #(nop) CMD [“/bin/sh” “-c” “/bin/b 0 B 610a93db295c 7 minutes ago /bin/sh -c #(nop) CMD [“/bin/sh” “-c” “echo \ 0 B c01a5a2195d7 7 minutes ago /bin/sh -c #(nop) CMD [“/bin/sh” “-c” “echo $ 0 B adf973b1e485 7 minutes ago /bin/sh -c #(nop) EXPOSE 80/tcp 0 B 9598516913a1 7 minutes ago /bin/sh -c yum -y install net-tools 23.32 MB 3e60ce0af3d5 8 minutes ago /bin/sh -c yum -y install vim 163.1 MB 583e9beff987 9 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0 B db4a1be6d6dc 9 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0 B efa3cf7ee1f9 17 months ago /bin/sh -c #(nop) CMD [“/bin/bash”] 0 B 2e9485c70cb7 17 months ago /bin/sh -c #(nop) LABEL name=CentOS Base Ima 0 B 24cb4957fdbf 17 months ago /bin/sh -c #(nop) ADD file:14c084af88ccd62ad7 194.5 MB 3690474eb5b4 22 months ago /bin/sh -c #(nop) MAINTAINER The CentOS Proj 0 B |
[3].实例3
(1).CMD
[root@pluto mydocker]# docker run -it -p 8888:8080 tomcat ls -l [root@pluto mydocker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
因为ls -l替换了CMD [“catalina.sh”, “run”],所以我们通过docker ps查看的时候tomcat并未启动起来
(2).ENTRYPOINT
docker run 之后的参数会被当做参数传递给 ENTRYPOINT,之后形成新的命令组合.
①.CMD版IP查询
[root@pluto mydocker]# pwd /mydocker [root@pluto mydocker]# vim Dockerfile3
FROM centos:centos6.8 RUN yum install -y curl CMD [“curl”,”-s”,”//www.cip.cc”]
[root@pluto mydocker]# docker build -f /mydocker/Dockerfile3 -t centos:ip . [root@pluto mydocker]# docker run -it centos:ip |
②ENTRYPOINT版IP查询
如果我们希望显示 HTTP 头信息,就需要加上 -i 参数,在CMD时会出现错误
[root@pluto mydocker]# docker run -it centos:ip -i exec: “-i”: executable file not found in $PATH Error response from daemon: Cannot start container a436fb6d142dd601abdb44ced5ec0749df43c65db22f770a50f7519814574380: [8] System error: exec: “-i”: executable file not found in $PATH |
[root@pluto mydocker]# pwd /mydocker [root@pluto mydocker]# vim Dockerfile4
FROM centos:centos6.8 RUN yum install -y curl ENTRYPOINT [“curl”,”-s”,”//cip.cc”]
[root@pluto mydocker]# docker build -f /mydocker/Dockerfile4 -t centos:ip1 . |
[4].实例4
(1).FROM
[root@pluto mydocker]# vim Dockerfile5
FROM centos:centos6.8 RUN yum install -y curl ENTRYPOINT [“curl”,”-s”,”//cip.cc”] ONBUILD RUN echo “father images onbuild —-“
[root@pluto mydocker]# docker build -f /mydocker/Dockerfile5 -t centos:myip_father . [root@pluto mydocker]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos myip_father 747a0f27ad7e 46 seconds ago 300.6 MB
[root@pluto mydocker]# vim Dockerfile6 FROM centos:myip_father RUN yum install -y curl ENTRYPOINT [“curl”,”-s”,”//cip.cc”] ONBUILD RUN echo “father images onbuild —-“
[root@pluto mydocker]# docker build -f /mydocker/Dockerfile6 -t centos:father_son . |
(2).自定义Tomcat
①Dockerfile
[root@pluto /]# mkdir -p /pluto/mydockerfile/tomcat2020 [root@pluto tomcat2020]# pwd /pluto/mydockerfile/tomcat2020 [root@pluto tomcat2020]# touch c.txt [root@pluto tomcat2020]# cp /opt/apache-tomcat-7.0.70.tar.gz . [root@pluto tomcat2020]# cp /opt/jdk-7u79-linux-x64.gz . [root@pluto tomcat2020]# ls apache-tomcat-7.0.70.tar.gz c.txt jdk-7u79-linux-x64.gz [root@pluto tomcat2020]# vim Dockerfile
FROM centos:centos6.8 MAINTAINER pluto<[email protected]> #把宿主机当前上下文的c.txt拷贝到容器/usr/local/路径下 COPY c.txt /usr/local/cincontainer.txt #把java与tomcat添加到容器中 ADD jdk-7u79-linux-x64.gz /usr/local/ ADD apache-tomcat-7.0.70.tar.gz /usr/local/ #安装vim编辑器 RUN yum -y install vim #设置工作访问时候的WORKDIR路径,登录落脚点 ENV MYPATH /usr/local WORKDIR $MYPATH #配置java与tomcat环境变量 ENV JAVA_HOME /usr/local/jdk1.7.0_79 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.70 ENV CATALINA_BASE /usr/local/apache-tomcat-7.0.70 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin #容器运行时监听的端口 EXPOSE 8080 #启动时运行tomcat # ENTRYPOINT [“/usr/local/apache-tomcat-7.0.70/bin/startup.sh” ] # CMD [“/usr/local/apache-tomcat-7.0.70/bin/catalina.sh”,”run”] CMD /usr/local/apache-tomcat-7.0.70/bin/startup.sh && tail -F /usr/local/apache-tomcat-7.0.70/bin/logs/catalina.out |
注:上面的apache-tomcat-7.0.70和jdk1.7.0_79需要注意,自己的版本需要对应,最简单获得的方法就是解压后就可以知道这两个是怎么填写。如
[root@pluto apache-tomcat-7.0.70]# pwd /opt/apache-tomcat-7.0.70 [root@pluto jdk1.7.0_79]# pwd /opt/jdk1.7.0_79 |
就可以将这两个参数填写到上面中黑体字部分
②构建
[root@pluto tomcat2020]# docker build -f /pluto/mydockerfile/tomcat2020/Dockerfile -t centos:tomcat . |
③运行
[root@pluto tomcat2020]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos tomcat 9fe67481da23 2 minutes ago 677.5 MB
[root@pluto tomcat2020]# docker run -d -p 9080:8080 –name mytomcat -v /pluto/mydockerfile/tomcat7/test:/usr/local/apache-tomcat-7.0.70/webapps/test -v /pluto/mydockerfile/tomcat7/tomcat7logs/:/usr/local/apache-tomcat-7.0.70/logs –privileged=true centos:tomcat 8cb50f97b47735e0c10189993670d324a12b16a007f50caba2e9576b14a90fa0
[root@pluto tomcat2020]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8cb50f97b477 centos:tomcat “/bin/sh -c ‘/usr/lo 17 seconds ago Up 17 seconds 0.0.0.0:9080->8080/tcp mytomcat
[root@pluto tomcat2020]# docker exec 8cb50f97b477 ls -l |
注:若访问失败可能是防火墙的原因,关闭防火墙或者添加规则即可
若出现“Error response from daemon: Conflict. The name “mytomcat” is already in use by container 676291e51f29. You have to delete (or rename) that container to be able to reuse that name.”则可以通过以下方法解决
//blog.csdn.net/qq_37189082/article/details/100051374 |
④发布web服务
[root@pluto test]# mkdir WEB-INF [root@pluto test]# cd WEB-INF/ [root@pluto WEB-INF]# pwd /pluto/mydockerfile/tomcat2020/test/WEB-INF [root@pluto WEB-INF]# vim web.xml
<?xml version=”1.0″ encoding=”UTF-8″?> <web-app xmlns:xsi=”//www.w3.org/2001/XMLSchema-instance” xmlns=”//java.sun.com/xml/ns/javaee” xsi:schemaLocation=”//java.sun.com/xml/ns/javaee //java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=”WebApp_ID” version=”2.5″>
<display-name>test</display-name>
</web-app>
[root@pluto test]# cd .. [root@pluto test]# vim a.jsp
<%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%> <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “//www.w3.org/TR/html4/loose.dtd”> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″> <title>Insert title here</title> </head> <body> ———–welcome———— <%=”i am in docker tomcat self “%> <br> <br> <% System.out.println(“=============docker tomcat self”);%> </body> </html>
[root@pluto test]# docker restart mytomcat |
⑤查看日志
[root@pluto tomcat7logs]# pwd [root@pluto tomcat7logs]# ll [root@pluto tomcat7logs]# tail -n 5 catalina.out |
网页刷新几次就会出现几次“docker tomcat self”
4.总结