Pre
Docker Review - dockerfile 入门篇
Docker Review - dockerfile 实战_给基础镜像增加功能
Docker 官方镜像 Dockerfile
https://github.com/docker-library/docs
以tomcat为例
看着里面的地址,
或者直接点击如下的链接
dockerfile制作tomcat镜像
准备软件
tomcat + JDK
编写Dockerfile文件
FROM centos MAINTAINER artisan COPY readme.txt /usr/local/readme.txt ADD jdk-8u161-linux-x64.tar.gz /usr/local/ ADD apache-tomcat-9.0.44.tar.gz /usr/local/ ENV MYPATH /usr/local WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_161 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.44 ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.44 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin EXPOSE 8080 CMD /usr/local/apache-tomcat-9.0.44/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.44/bin/logs/catalina.out
ADD 有能力自动解压文件。如果<src>
参数是一个可识别的压缩格式(tar, gzip, bzip2, etc)的本地文件 ,就会被解压到指定容器文件系统的路径<dest>
。
dockerfile构建镜像
[root@VM-0-7-centos jdk]# docker build -f tomcat-dockerfile -t xxxtomcat . Sending build context to Docker daemon 380.8MB Step 1/14 : FROM centos ---> 5d0da3dc9764 Step 2/14 : MAINTAINER artisan ---> Using cache ---> f19684f6e082 Step 3/14 : COPY readme.txt /usr/local/readme.txt ---> Using cache ---> be6de2a66c66 Step 4/14 : ADD jdk-8u161-linux-x64.tar.gz /usr/local/ ---> Using cache ---> 4a3259261dea Step 5/14 : ADD apache-tomcat-9.0.44.tar.gz /usr/local/ ---> Using cache ---> 287bd3c3c17d Step 6/14 : ENV MYPATH /usr/local ---> Using cache ---> 9f7b7e781819 Step 7/14 : WORKDIR $MYPATH ---> Using cache ---> 8c6faa53c274 Step 8/14 : ENV JAVA_HOME /usr/local/jdk1.8.0_161 ---> Using cache ---> a5eddd2652a9 Step 9/14 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ---> Using cache ---> 7dbc598b3f58 Step 10/14 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.44 ---> Using cache ---> 2b7eb2d574f9 Step 11/14 : ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.44 ---> Using cache ---> cff7c91e7b1d Step 12/14 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin ---> Using cache ---> c19425f9f62d Step 13/14 : EXPOSE 8080 ---> Using cache ---> 067a0f5eb770 Step 14/14 : CMD /usr/local/apache-tomcat-9.0.44/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.44/bin/logs/catalina.out ---> Using cache ---> c5b9494ea9b1 Successfully built c5b9494ea9b1 Successfully tagged xxxtomcat:latest
启动镜像
端口映射 + 挂载目录
[root@VM-0-7-centos jdk]# docker run -d -p 7979:8080 --name xgjtomcat -v /root/tomcat/test:/usr/local/apache-tomcat-9.0.44/webapps/test -v /root/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.44/logs artisantomcat
观察本地的宿主机的目录 /root/tomcat/test 和 /root/tomcat/tomcatlogs ,会自动创建。
测试访问tomcat
根据映射
-v /root/tomcat/test:/usr/local/apache-tomcat-9.0.44/webapps/test -v /root/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.44/logs
到挂载test的目录下 ,创建一个简单的项目,看看能不能正常运行
新建一个WEB-INF目录,然后创建一个web.xml文件,接着创建一个index.jsp文件
[root@VM-0-7-centos tomcat]# pwd /root/tomcat [root@VM-0-7-centos tomcat]# [root@VM-0-7-centos tomcat]# ls test tomcatlogs [root@VM-0-7-centos tomcat]# cd test/ [root@VM-0-7-centos test]# [root@VM-0-7-centos test]# ll total 0 [root@VM-0-7-centos test]# mkdir WEB-INF [root@VM-0-7-centos test]# ll total 8 -rw-r--r-- 1 root root 150 Oct 14 23:41 index.jsp drwxr-xr-x 2 root root 4096 Oct 14 23:43 WEB-INF
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> </web-app>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tomcat</title> </head> <body> <h1> hello,artisan </h1> </body> </html>
映射后,和宿主机的目录是实时的,我们连到容器里去看看
OK , 一个tomcat镜像成功的创建成功了。