Docker 使用Dockerfile构建tomcat镜像
Dockerfile概念:
镜像的定制实际上就是定制每一层所添加的配置、文件。如果我们可以把每一层修改、安装、构建、操作的命令都写入一个脚本,用这个脚本来构建、定制镜像,那么之前提及的无法重复的问题、镜像构建透明性的问题、体积的问题就都会解决。这个脚本就是 Dockerfile。
Dockerfile 是一个文本文件,其内包含了一条条的指令(Instruction),每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。
代码:
Centos7.2中安装jdk和tomcat
FROM centos:7.2.1511 MAINTAINER hongdada "hongdaqi159505@gmail.com" # 设置当前工具目录 # 该命令不会新增镜像层 WORKDIR /home # 安装必要的工具 RUN yum install -y wget && \ rpm --rebuilddb && \ yum install -y tar && \ wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz && \ tar -xvzf jdk-8u131-linux-x64.tar.gz && \ wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.32/bin/apache-tomcat-8.5.32.tar.gz && \ tar -xvzf apache-tomcat-8.5.32.tar.gz && \ mv apache-tomcat-8.5.32/ tomcat && \ rm -f jdk-8u131-linux-x64.tar.gz && \ rm -f apache-tomcat-8.5.32.tar.gz && \ yum clean all # 设置环境变量 ENV JAVA_HOME /home/jdk1.8.0_131 ENV CATALINA_HOME /home/tomcat ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin # 暴露tomcat 8080端口 EXPOSE 8080 ENTRYPOINT /home/tomcat/bin/startup.sh && tail -f /home/tomcat/logs/catalina.out # 创建容器启动tomcat CMD ["/home/tomcat/bin/startup.sh"]
利用这个Dockerfile构建镜像命令:
[root@localhost tomcat]# docker build -t centos:v1 .
注意后面有个点 .
我们这里使用的是直接下载,但是一般生产不会这样,会利用已经下载好的安装包,这样在build的过程中会报错
找不到tomcat或者jdk路径,这事可以在build时用指定Dockerfile路径试试。
docker build -t tomcat:test1 /home/zyt/tomcat-docker/Dockerfile .
上面的构建命令执行结果:
[root@localhost tomcat]# docker build -t centos:v1 . Sending build context to Docker daemon 3.072 kB Step 1/10 : FROM centos:7.2.1511 Trying to pull repository docker.io/library/centos ... sha256:7c47810fd05ba380bd607a1ece3b4ad7e67f5906b1b981291987918cb22f6d4d: Pulling from docker.io/library/centos f2d1d709a1da: Pull complete Digest: sha256:7c47810fd05ba380bd607a1ece3b4ad7e67f5906b1b981291987918cb22f6d4d Status: Downloaded newer image for docker.io/centos:7.2.1511 ---> 0a2bad7da9b5 Step 2/10 : MAINTAINER hongdada "hongdaqi159505@gmail.com" ---> Running in c6da5f881cc8 ---> 69b28335aece Removing intermediate container c6da5f881cc8 Step 3/10 : WORKDIR /home ---> 7c45fd2b7596 Removing intermediate container d1d433a16ef9 Step 4/10 : RUN yum install -y wget && rpm --rebuilddb && yum install -y tar && wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz && tar -xvzf jdk-8u131-linux-x64.tar.gz && wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.32/bin/apache-tomcat-8.5.32.tar.gz && tar -xvzf apache-tomcat-8.5.32.tar.gz && mv apache-tomcat-8.5.32/ tomcat && rm -f jdk-8u131-linux-x64.tar.gz && rm -f apache-tomcat-8.5.32.tar.gz && yum clean all ---> Running in 7256e59ff024 ....... ....... ....... Loaded plugins: fastestmirror Cleaning repos: base extras updates Cleaning up everything Cleaning up list of fastest mirrors ---> 10720ac1a433 Removing intermediate container 7256e59ff024 Step 5/10 : ENV JAVA_HOME /home/jdk1.8.0_131 ---> Running in 9dda120b45bc ---> 3c5390a1e18c Removing intermediate container 9dda120b45bc Step 6/10 : ENV CATALINA_HOME /home/tomcat ---> Running in 55b9ca9b46f6 ---> 23dd4d3c4e81 Removing intermediate container 55b9ca9b46f6 Step 7/10 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin ---> Running in 8b79ea2a53bc ---> 1403401143c8 Removing intermediate container 8b79ea2a53bc Step 8/10 : EXPOSE 8080 ---> Running in c1faa9022bf7 ---> bde367d955e3 Removing intermediate container c1faa9022bf7 Step 9/10 : ENTRYPOINT /home/tomcat/bin/startup.sh && tail -f /home/tomcat/logs/catalina.out ---> Running in e0f7216cdefc ---> ff984964a08f Removing intermediate container e0f7216cdefc Step 10/10 : CMD /home/tomcat/bin/startup.sh ---> Running in 27eb2056c888 ---> 86bb3ec89384 Removing intermediate container 27eb2056c888 Successfully built 86bb3ec89384
可以看出一共分成了10步,最后Successfully
查看镜像:
[root@localhost tomcat]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE centos v1 86bb3ec89384 3 minutes ago 595 MB <none> <none> a19d7e86dfb2 33 minutes ago 191 MB docker.io/nginx latest 5699ececb21c 8 days ago 109 MB docker.io/ubuntu 16.04 5e8b97a2a082 4 weeks ago 114 MB docker.io/centos latest 49f7960eb7e4 4 weeks ago 200 MB hongdada/nginx v3 c5cf58738d6b 4 weeks ago 109 MB
可以看出第一个就是刚刚构建的centos
根据该镜像启动一个容器:
[root@localhost tomcat]# docker run -d -p 8088:8080 centos:v1 b445ecc8eb60634481404a88f3d9c017c88b122c431f1b70f3887dcce284eb18
查看:
[root@localhost tomcat]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b445ecc8eb60 centos:v1 "/bin/sh -c '/home..." 4 seconds ago Up 4 seconds 0.0.0.0:8088->8080/tcp suspicious_bassi
查看容器启动日志:
root@localhost tomcat]# docker logs b4 Tomcat started. tail: unrecognized file system type 0x794c7630 for '/home/tomcat/logs/catalina.out'. please report this to bug-coreutils@gnu.org. reverting to polling 05-Jul-2018 13:55:21.422 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.32 05-Jul-2018 13:55:21.444 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 20 2018 19:50:35 UTC 05-Jul-2018 13:55:21.444 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.32.0 05-Jul-2018 13:55:21.445 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux 05-Jul-2018 13:55:21.445 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-693.21.1.el7.x86_64 05-Jul-2018 13:55:21.445 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 05-Jul-2018 13:55:21.445 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /home/jdk1.8.0_131/jre 05-Jul-2018 13:55:21.445 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_131-b11 05-Jul-2018 13:55:21.445 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation 05-Jul-2018 13:55:21.445 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /home/tomcat 05-Jul-2018 13:55:21.445 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /home/tomcat 05-Jul-2018 13:55:21.446 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/home/tomcat/conf/logging.properties 05-Jul-2018 13:55:21.446 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 05-Jul-2018 13:55:21.446 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 05-Jul-2018 13:55:21.446 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources 05-Jul-2018 13:55:21.446 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 05-Jul-2018 13:55:21.446 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs= 05-Jul-2018 13:55:21.446 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/home/tomcat 05-Jul-2018 13:55:21.446 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/home/tomcat 05-Jul-2018 13:55:21.446 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/home/tomcat/temp 05-Jul-2018 13:55:21.446 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 05-Jul-2018 13:55:21.862 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"] 05-Jul-2018 13:55:21.899 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 05-Jul-2018 13:55:21.912 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"] 05-Jul-2018 13:55:21.913 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 05-Jul-2018 13:55:21.926 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1665 ms 05-Jul-2018 13:55:22.000 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina] 05-Jul-2018 13:55:22.000 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.32 05-Jul-2018 13:55:22.037 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/home/tomcat/webapps/ROOT] 05-Jul-2018 13:55:22.928 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/home/tomcat/webapps/ROOT] has finished in [890] ms 05-Jul-2018 13:55:22.941 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/home/tomcat/webapps/docs] 05-Jul-2018 13:55:22.988 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/home/tomcat/webapps/docs] has finished in [47] ms 05-Jul-2018 13:55:22.989 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/home/tomcat/webapps/examples] 05-Jul-2018 13:55:23.572 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/home/tomcat/webapps/examples] has finished in [584] ms 05-Jul-2018 13:55:23.572 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/home/tomcat/webapps/host-manager] 05-Jul-2018 13:55:23.645 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/home/tomcat/webapps/host-manager] has finished in [73] ms 05-Jul-2018 13:55:23.646 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/home/tomcat/webapps/manager] 05-Jul-2018 13:55:23.700 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/home/tomcat/webapps/manager] has finished in [54] ms 05-Jul-2018 13:55:23.709 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 05-Jul-2018 13:55:23.751 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"] 05-Jul-2018 13:55:23.755 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1828 ms
最后查看8088端口是否可以访问tomcat,我这边是可以的,这边就不贴效果图了。
进入容器内部进行查看:
[root@localhost tomcat]# docker exec -it b44 /bin/bash [root@b445ecc8eb60 home]# ll total 4 drwxr-xr-x. 8 10 143 4096 Mar 15 2017 jdk1.8.0_131 drwxr-xr-x. 1 root root 39 Jul 5 13:44 tomcat
参考:
https://blog.csdn.net/wo18237095579/article/details/80540571
https://ithelp.ithome.com.tw/articles/10191016
https://yeasy.gitbooks.io/docker_practice/image/build.html
http://oomusou.io/docker/dockerfile-dockercompose/
https://blog.csdn.net/boonya/article/details/74626431
https://steffan.cn/2017/02/10/how-to-build-a-Tomcat-image-with-Dockerfile-and-deploy-war/