반응형
0. 사전준비
도커 설치 및 War파일이 준비가 되어있다는 가정하에 시작한다.
1. Dockerfile작성
Dockerfile
다음과 같은 도커파일을 작성한다. 이때 server.xml과 war파일은 도커파일이 존재하는 디렉토리 내부에 존재해야한다.
FROM tomcat:8.5-alpine #톰캣8.5 사용
ENV TZ=Asia/Seoul
Run rm /usr/local/tomcat/conf/server.xml #기본으로 있는 server.xml을 지움
COPY server.xml /usr/local/tomcat/conf/ #현재 디렉토리에 있는 server.xml을 복사
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone #시간을 서울로 설정
RUN value=`cat conf/server.xml` && echo "${value//8080/1111}" >| conf/server.xml #기본포트가 8080인데 1111로 변경 필요한사람은 쓰고 8080쓸거면 지워도됌
RUN rm -Rf /usr/local/tomcat/webapps/ROOT #기본으로 있는 ROOT를 지움
COPY keti_main.war /usr/local/tomcat/webapps #현재 Dockerfile이 작성되는 디렉토리에 있는 war파일을 컨테이너 내부로 복사
ENV JAVA_OPTS="-Dserver.type=dev" #자바 옵션 설정
Server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed withthis 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 withthe License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License. -->
<!-- Note: A "Server" is not itself a "Container", so you may notdefine subcomponents such as "Valves" at this level.Documentation at /docs/config/server.html -->
-<Server shutdown="SHUTDOWN" port="8005">
<Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
<!-- Security listener. Documentation at /docs/config/listeners.html<Listener className="org.apache.catalina.security.SecurityListener" /> -->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on"/>
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<!-- Global JNDI resourcesDocumentation at /docs/jndi-resources-howto.html -->
-<GlobalNamingResources>
<!-- Editable user database that can also be used byUserDatabaseRealm to authenticate users -->
<Resource pathname="conf/tomcat-users.xml" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" description="User database that can be updated and saved" type="org.apache.catalina.UserDatabase" auth="Container" name="UserDatabase"/>
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that sharea single "Container" Note: A "Service" is not itself a "Container",so you may not define subcomponents such as "Valves" at this level.Documentation at /docs/config/service.html -->
-<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"maxThreads="150" minSpareThreads="4"/> -->
<!-- A "Connector" represents an endpoint by which requests are receivedand responses are returned. Documentation at :Java HTTP Connector: /docs/config/http.htmlJava AJP Connector: /docs/config/ajp.htmlAPR (HTTP/AJP) Connector: /docs/apr.htmlDefine a non-SSL/TLS HTTP/1.1 Connector on port 1111 -->
<Connector port="8080" redirectPort="8443" connectionTimeout="20000" protocol="HTTP/1.1"/>
<!-- A "Connector" using the shared thread pool-->
<!--<Connector executor="tomcatThreadPool"port="1111" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" /> -->
<!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443This connector uses the NIO implementation. The defaultSSLImplementation will depend on the presence of the APR/nativelibrary and the useOpenSSL attribute of theAprLifecycleListener.Either JSSE or OpenSSL style configuration may be used regardless ofthe SSLImplementation selected. JSSE style configuration is used below. -->
<!--<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"maxThreads="150" SSLEnabled="true"><SSLHostConfig><Certificate certificateKeystoreFile="conf/localhost-rsa.jks"type="RSA" /></SSLHostConfig></Connector> -->
<!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2This connector uses the APR/native implementation which always usesOpenSSL for TLS.Either JSSE or OpenSSL style configuration may be used. OpenSSL styleconfiguration is used below. -->
<!--<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"maxThreads="150" SSLEnabled="true" ><UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" /><SSLHostConfig><Certificate certificateKeyFile="conf/localhost-rsa-key.pem"certificateFile="conf/localhost-rsa-cert.pem"certificateChainFile="conf/localhost-rsa-chain.pem"type="RSA" /></SSLHostConfig></Connector> -->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" redirectPort="8443" protocol="AJP/1.3"/>
<!-- An Engine represents the entry point (within Catalina) that processesevery request. The Engine implementation for Tomcat stand aloneanalyzes the HTTP headers included with the request, and passes themon to the appropriate Host (virtual host).Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> -->
-<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:/docs/cluster-howto.html (simple how to)/docs/config/cluster.html (reference documentation) -->
<!--<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> -->
<!-- Use the LockOutRealm to prevent attempts to guess user passwordsvia a brute-force attack -->
-<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDIresources under the key "UserDatabase". Any editsthat are performed against this UserDatabase are immediatelyavailable for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
-<Host name="localhost" autoDeploy="true" unpackWARs="true" appBase="webapps">
<Context reloadable="true" docBase="${catalina.home}/webapps/keti_main" path="/"/>
<!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html -->
<!--<Valve className="org.apache.catalina.authenticator.SingleSignOn" /> -->
<!-- Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" pattern="%h %l %u %t "%r" %s %b" suffix=".txt" prefix="localhost_access_log" directory="logs"/>
</Host>
</Engine>
</Service>
</Server>
이미지 만들기
sudo docker build -t 이미지이름:버전
예시
sudo docker build -t test:0.1
이미지 확인
빌드가 완료되면 이미지가 생성되고 다음과 같이 확인할 수 있다.
sudo docker images
2. 이미지 실행
1번을 통해 만든 이미지를 다음과 같이 실행한다
sudo docker run 이미지명:버전
sudo docker run -p 80:1111 이미지명:버전
아래와 같은 -p는 80포트로 오는 요청을 1111포트로 전달해주겠다는 의미이다.
다음과 같이 실행하였다면 war파일이 제대로 배포되어있는것을 확인할 수 있다.
3. 도커 코드
실행중인 컨테이너 상태 보기
sudo docker ps
실행중인 컨테이너 IP보기
sudo docker inspect -f "{{ .NetworkSettings.IPAddress }}" Container ID
반응형
'프로그래밍 > 도커 & 쿠버네티스' 카테고리의 다른 글
[쿠버네티스] #4. 컨트롤러란? Deployment란? (0) | 2020.03.22 |
---|---|
[쿠버네티스] #3. 컨트롤러란? ReplicaSet이란? Template, Replicas, Selector란? (0) | 2020.03.10 |
[Docker & Kubernetes] 도커를 이용하여 Express 배포하기 (0) | 2020.03.09 |
[쿠버네티스] #2. 서비스란? Service란? (0) | 2020.02.25 |
[쿠버네티스] #1. Pod란? 파드란? (0) | 2020.02.22 |