使用docker命令行构建一个tomcat容器

下载centos

[root@localhost ~]# docker search centos
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                          The official build of CentOS.                   2740      [OK]       
ansible/centos7-ansible         Ansible on Centos7                              89                   [OK]
jdeathe/centos-ssh              CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8...   41                   [OK]
jdeathe/centos-ssh-apache-php   CentOS-6 6.8 x86_64 / Apache / PHP / PHP M...   21                   [OK]
....
[root@localhost ~]# docker pull centos

查看我们已经下载的镜像文件

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
tomcat              latest              93a46cca8a9d        3 days ago          355.3 MB
centos              7.2.1511            bcdaf234d72f        6 weeks ago         194.6 MB

下载oneinstack或者你自己需要的tomcat和jdk

这里是用oneinstack的原因是作者习惯,而且它本身已经对部署软件包括配置进行了优化

[root@localhost ~]# cd /usr/local/src/
[root@localhost ~]# wget http://mirrors.linuxeye.com/oneinstack-full.tar.gz

docker run 运行交互shell

docker run -i -t -v /usr/local/src:/usr/local/src bcdaf234d72f /bin/bash
  • 稍微说一下,这里的路径是作者下载的路径,对应可以根据需要调整或者修改
  • 如果启动不了,建议是rm -rf /var/lib/docker/* 重新下载images,具体的错误没有记录下来。

安装tomcat和jdk

[root@b7994eebec11 oneinstack]# yum install wget
[root@b7994eebec11 oneinstack]# ./install.sh
  • 下载wget原因是因为 install.sh需要
  • 直接安装选择tomcat 和jdk
[root@b7994eebec11 bin]# find / -name java
/usr/java
/usr/java/jdk1.8.0_102/jre/bin/java
/usr/java/jdk1.8.0_102/bin/java
/etc/pki/java
/etc/pki/ca-trust/extracted/java
[root@b7994eebec11 java]# find / -name tomcat
/usr/local/tomcat

编写运行脚本

[root@b7994eebec11 bin] cd /root
[root@b7994eebec11 ~] vim run.sh
#!/bin/bash

export JAVA_HOME=/usr/java/jdk1.8.0_102/
export PATH=$JAVA_HOME/bin:$PATH

//这里用run是让运行脚本阻塞
/usr/local/tomcat/bin/catalina.sh run

[root@b7994eebec11 ~] chmod +x run.sh
[root@b7994eebec11 ~] exit

创建tomcat镜像

[root@localhost src]# docker commit b7994eebec11 mytomcat:1.0
4b4295f05046713704d6bde30dba357408f32cd480aeb63dc9119263f86eac26

启动tomcat docker容器

[root@localhost src]# docker run -d -p 8080:8080 -v /www/java/:/www/java/ --name mytomcat_1 mytomcat:1.0 /root/run.sh
32d9c2edfe04b2a8fee6bcd5ce8fb0b3e9b577d7e9c3917e39d6b9f86f1d2b94
[root@localhost src]# docker logs 32d9c2edfe04
31-Oct-2016 12:33:26.918 WARNING [main] org.apache.catalina.startup.SetAllPropertiesRule.begin [SetAllPropertiesRule]{Server/Service/Connector} Setting property 'debug' to '0' did not find a matching property.
31-Oct-2016 12:33:27.005 WARNING [main] org.apache.tomcat.util.digester.SetPropertiesRule.begin [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to 'true' did not find a matching property.
31-Oct-2016 12:33:27.181 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.1.33 using APR version 1.4.8.
31-Oct-2016 12:33:27.181 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
31-Oct-2016 12:33:27.219 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.1e 11 Feb 2013)
31-Oct-2016 12:33:27.273 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
31-Oct-2016 12:33:27.286 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1023 ms
31-Oct-2016 12:33:27.322 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
31-Oct-2016 12:33:27.323 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Tomcat
31-Oct-2016 12:33:29.925 INFO [oauth.mailejifen.com-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
31-Oct-2016 12:33:29.951 INFO [oauth.mailejifen.com-startStop-1] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
31-Oct-2016 12:33:29.979 INFO [oauth.mailejifen.com-startStop-1] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
31-Oct-2016 12:33:29.979 INFO [oauth.mailejifen.com-startStop-1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization started
31-Oct-2016 12:33:30.081 INFO [oauth.mailejifen.com-startStop-1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing Root WebApplicationContext: startup date [Mon Oct 31 12:33:30 CST 2016]; root of context hierarchy
31-Oct-2016 12:33:30.127 INFO [oauth.mailejifen.com-startStop-1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from file [/www/java/oauth.mailejifen.com/ROOT/WEB-INF/classes/spring-hibernate.xml]
31-Oct-2016 12:33:30.360 INFO [oauth.mailejifen.com-startStop-1] org.springframework.context.support.PropertySourcesPlaceholderConfigurer.loadProperties Loading properties file from class path resource [mysql-dev.properties]
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
31-Oct-2016 12:33:30.397 INFO [oauth.mailejifen.com-startStop-1] org.springframework.jdbc.datasource.DriverManagerDataSource.setDriverClassName Loaded JDBC driver: com.mysql.jdbc.Driver
31-Oct-2016 12:33:32.689 INFO [oauth.mailejifen.com-startStop-1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization completed in 2710 ms

[root@localhost src]# netstat -lnpt | grep 8080

results matching ""

    No results matching ""