Dockerize Apache Tomcat 서블릿

10601 단어 apachetomcatdockerjava

1단계 - 종속성


Java EEJDKDocker의 호환 버전이 설치되어 있는지 확인하십시오.Java EE가 설치되어 있지 않거나 OpenJDK가 있는 경우 this link에서 다운로드한 Java EE jar 파일을 사용할 수 있습니다.

2단계 - 소스 코드 컴파일



this link의 Java 소스 코드를 사용하고 다음을 사용하여 컴파일해 봅시다.

javac -cp .:<path to javaee jar> -target 1.7 -source 1.7 TestingServlet.java


If you're using windows, you have to use ; instead of : in classpath
The -target is the version number of the runtime environment of tomcat. Similarly for -source



3단계 - 환경 설정



here에서 Dockerfile를 사용하고 몇 가지 항목을 추가해 보겠습니다.

FROM tomcat:8.0-alpine
LABEL maintainer="[email protected]"

COPY ./web.xml /usr/local/tomcat/webapps/ROOT/WEB-INF
COPY ./TestingServlet.class /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/TestingServlet.class
COPY ./tomcat-users.xml /usr/local/tomcat/conf/tomcat-users.xml

EXPOSE 8080
CMD ["catalina.sh", "run"]


We define web.xml and tomcat-users.xml later. The file TestingServlet.class is important and we'll remember the name for later.



4단계 - 구성


web.xml를 다음과 같이 정의해 보겠습니다.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

  <servlet>
   <servlet-name>Testing</servlet-name>
   <servlet-class>TestingServlet </servlet-class>
  </servlet>

 <servlet-mapping>
 <servlet-name>Testing</servlet-name>
 <url-pattern>/servlet/TestingServlet</url-pattern>
 </servlet-mapping>
</web-app>



servlet-class is the name of the .class file and url-pattern is the HTTP url you want to access it by.


tomcat-users.xml를 다음과 같이 정의해 보겠습니다.

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
-->
<!--
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->

  <role rolename="manager"/>
  <role rolename="manager-gui"/>
  <user username="admin" password="admin" roles="manager-gui"/>
</tomcat-users>


Apache Tomcat defines a list of roles here



5단계 - 배포


Docker 이미지를 빌드해 보겠습니다.

docker build -t tomcat .


그리고 그것을 실행

docker run -d -p 8080:8080 --name tomcat tomcat


Apache Tomcat runs on port 8080 in the container



이제 http://localhost:8080/servlet/TestingServlet를 방문하여 다음 메시지를 볼 수 있습니다.


6단계 - 디버깅



다음과 같은 오류가 표시되는 경우

즉, 대상 런타임의 버전에 따라 더 높거나 낮은 서로 다른 targetsource 버전을 선택해야 합니다. 페이지 하단의 Tomcat UI을 통해 Java Runtime 버전을 확인할 수 있습니다.


또는 실행 중인 Docker 컨테이너 내부에 있을 때 bash version.sh에서 /usr/local/tomcat/bin를 실행할 수 있습니다.



참조


  • Tomcat Docker
  • Java cross compile
  • Servlet setup
  • Servlet setup 2
  • Apache Tomcat Roles
  • 좋은 웹페이지 즐겨찾기