{"id":7733,"date":"2018-10-03T07:49:07","date_gmt":"2018-10-02T23:49:07","guid":{"rendered":"http:\/\/rmohan.com\/?p=7733"},"modified":"2018-10-03T07:49:07","modified_gmt":"2018-10-02T23:49:07","slug":"docker-to-build-a-tomcat-runtime-environment","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7733","title":{"rendered":"Docker to build a Tomcat runtime environment"},"content":{"rendered":"<p>1 Prepare the host system<\/p>\n<p>Prepare a CentOS 7 operating system with the following specific requirements:<\/p>\n<p>Must be a 64-bit operating system. The<br \/>\nkernel is above 3.8 to<br \/>\nview your CentOS kernel with the following command:<\/p>\n<p># uname -r<\/p>\n<p>2 Install Docker<\/p>\n<p># yum install docker<br \/>\nUse the following command to see if Docker is installed successfully:<\/p>\n<p># docker version<br \/>\nIf the version number of Docker is output, the installation is successful. You can start the Docker service with the following command:<\/p>\n<p># systemctl start docker.service<br \/>\nOnce the Docker service has started, you can start using Docker.<\/p>\n<p>3 download image<\/p>\n<p>Take CentOS as an example, download a CentOS image:<\/p>\n<p># docker pull centos After<br \/>\ndownloading, use the command to view the local mirror list:<\/p>\n<p># docker images<br \/>\nREPOSITORY TAG IMAGE ID CREATED SIZE<br \/>\ncentos latest e934aafc2206 2 weeks ago 199MB<\/p>\n<p>4 The host creates the \/root\/software\/ directory and places the installation package in this directory.<\/p>\n<p>5 start the container<\/p>\n<p>The container is run on a mirrored basis. Once the container is started, we can log into the container and install the software or applications we need.<\/p>\n<p>Start the container with the following command:<\/p>\n<p># docker run -i -t -v \/root\/software\/:\/mnt\/software\/ The e934 \/bin\/bash<br \/>\ncommand contains the following three sections:<\/p>\n<p>Docker run &lt;related parameters&gt; &lt;mirror ID&gt; &lt;initial command&gt;<br \/>\nAmong them, related parameters include:<\/p>\n<p>-i: indicates<br \/>\nthat the container is run in &#8220;interactive mode&#8221; -t: indicates that the container will enter its command line after startup<br \/>\n-v: indicates which directory needs to be mounted locally to the container, format: -v &lt;host directory&gt;:&lt;container Directory &gt; In<br \/>\nthis example, all installers are placed in the \/root\/software\/ directory of the host, and now you need to mount them in the \/mnt\/software\/ directory of the container.<\/p>\n<p># cd \/mnt\/software\/<br \/>\n# pwd<br \/>\n\/mnt\/software<\/p>\n<p># ls<br \/>\napache-tomcat-7.0.81.tar.gz jdk-8u121-linux-x64.tar.gz The<br \/>\ninitial command means that once the container is started, you need to run the command. Use &#8220;\/bin\/bash&#8221; to indicate that you directly enter the bash shell after booting.<\/p>\n<p>6 Installing the software<\/p>\n<p>In order to build the Java Web runtime environment, you need to install JDK and Tomcat. The following procedures are performed inside the container. In this example, select the \/opt\/ directory as the installation directory. You must first enter the directory using the cd \/opt\/ command.<\/p>\n<p>6.1 Installing the JDK<\/p>\n<p>First, unzip the JDK package:<\/p>\n<p># tar -zxf \/mnt\/software\/jdk-8u121-linux-x64.tar.gz -C .<\/p>\n<p>Then, move the JDK directory:<\/p>\n<p># mv jdk1.8.0_121\/ \/opt\/jdk\/<\/p>\n<p>6.2 Installing Tomcat<\/p>\n<p>First, extract the Tomcat package:<\/p>\n<p># tar -zxf \/mnt\/software\/apache-tomcat-7.0.81.tar.gz -C .<\/p>\n<p>Then, move the Tomcat directory:<\/p>\n<p># mv apache-tomcat-7.0.81\/ \/opt\/tomcat\/<\/p>\n<p>6.3 Writing a run script<\/p>\n<p>Write a run script that, when the container is started, runs the script and starts Tomcat.<\/p>\n<p>First, create a run script:<\/p>\n<p># touch \/root\/run.sh<\/p>\n<p># vi \/root\/run.sh<br \/>\nThen, edit the script as follows:<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>export JAVA_HOME=\/opt\/jdk\/<br \/>\nexport PATH=$JAVA_HOME\/bin:$PATH<\/p>\n<p>sh \/opt\/tomcat\/bin\/catalina.sh run<br \/>\nFinally, add execute permission for running the script:<\/p>\n<p># chmod u+x \/root\/run.sh<\/p>\n<p>7 Exit the container<\/p>\n<p>When the above steps are all completed, you can use the exit command to exit the container.<\/p>\n<p>You can then view the running container using the following command:<\/p>\n<p>Docker ps<br \/>\nAt this point, you should not see any running programs, because the container that you just exited with the exit command, the container is stopped, you can use the following command to view all containers:<\/p>\n<p># docker ps -a<br \/>\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES<br \/>\nd4e3a062ab05 e934 &#8220;\/bin\/bash&#8221; 38 minutes ago Exited (0) About a minute ago lucid_einstein<br \/>\nRemember the above CONTAINER ID (container ID), which will then be created through the container One can run a Tomcat image.<\/p>\n<p>8 Create a Tomcat image<\/p>\n<p>Use the following command to create a new &#8220;mirror&#8221; based on a &#8220;container ID&#8221;:<\/p>\n<p># Docker the commit d4e3 mytomcat: 1.0<br \/>\nSHA256: c5ef8dacbf3eead5ea2b9fc3c1050a395863c6db0abd0eb7d6dee8ed46a31ffd<\/p>\n<p># Docker Images<br \/>\nthe REPOSITORY the TAG the IMAGE ID CREATED SIZE<br \/>\nmytomcat 1.0 c5ef8dacbf3e 18 is seconds The ago Member 583MB<br \/>\nCentOS Latest e934aafc2206 2 weeks ago Member 199MB<br \/>\nID of this container is d4e3, image name created is &#8220;mytomcat: 1.0 &#8220;, then you can use the image to start the Tomcat container.<\/p>\n<p>9 Start the Tomcat container<\/p>\n<p>First, create a new \/root\/webapps\/ROOT directory,<\/p>\n<p>Cd # \/ root<br \/>\n# mkdir webapps<br \/>\n# cd webapps \/<br \/>\n# mkdir ROOT<br \/>\n# cd ROOT \/<br \/>\n# vi index.html<br \/>\nand create an index.html file in the directory, file reads as follows:<\/p>\n<p>&lt;html&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;h2&gt;Hello World!&lt;\/h2&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\nAs described above, the container can be started with the &#8220;mirror name&#8221; or &#8220;mirror ID&#8221;, and the last time it was started. The difference between the containers is that instead of entering the command line of the container, the Tomcat service inside the container is started directly. In this case, you need to use the following command:<\/p>\n<p># docker run -d -p 58080:8080 -v \/root\/webapps\/:\/opt\/tomcat\/webapps\/ &#8211;name mytomcat_1 mytomcat:1.0 \/root\/run.sh<br \/>\nwhere related parameters include:<\/p>\n<p>-d: Indicates that the \/root\/run.sh script is executed in &#8220;daemon mode&#8221;, at which point the Tomcat console does not appear on the output terminal.<br \/>\n-p: indicates the port mapping between the host and the container. At this time, the 8080 port inside the container is mapped to the port 58080 of the host. This exposes the port 58080 to the outside world. The Docker bridge can be used to access the port 8080 inside the container. .<br \/>\n-v: Indicates which local directory needs to be mounted to the container. Format: -v &lt;host directory&gt;: &lt;container directory&gt;<br \/>\n&#8211;name: indicates the container name, which can be named with a meaningful name.<br \/>\nIn the browser, enter the host IP and port number to access Tomcat:<\/p>\n<p>11 Stop the Tomcat container<\/p>\n<p># docker ps -a<br \/>\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES<br \/>\n54215923125b mytomcat:1.0 &#8220;\/root\/run.sh&#8221; 3 minutes ago Up 3 minutes 0.0.0.0:58080-&gt;8080\/tcp mytomcat_1<\/p>\n<p># docker stop 5421<\/p>\n<p>12 Remove the container<\/p>\n<p># docker ps -a<br \/>\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES<br \/>\n54215923125b mytomcat:1.0 &#8220;\/root\/run.sh&#8221; 3 minutes ago Exited (137) 2 seconds ago mytomcat_1<\/p>\n<p># docker rm 5421<br \/>\n5421<\/p>\n<p># docker ps -a<br \/>\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES<\/p>\n<p>13 Remove the image<\/p>\n<p># Docker Images<br \/>\nthe REPOSITORY the TAG the IMAGE ID CREATED SIZE<br \/>\nmytomcat 1.0 c5ef8dacbf3e 20 is minutes ago Member 583MB<br \/>\nCentOS Latest e934aafc2206 2 weeks ago Member 199MB<\/p>\n<p># Docker RMI c5ef<br \/>\nthe Untagged: mytomcat: 1.0<br \/>\nthe Untagged: mytomcat @ SHA256: d949cbb93a58de27eec4c911f27b9f09edeb3d3ce57cf5ce77d4745211c947f6<br \/>\nDeleted: SHA256: c5ef8dacbf3e7ce916f57c52c16de3892c724996b5e30ca0d141c81897d9a06c<br \/>\nDeleted: SHA256: 7e62d1c2f904e8de3fadc6b01edea96bcb324634f0df514cc9297b1bf11d2f06<\/p>\n<p># Docker images<br \/>\nREPOSITORY TAG IMAGE ID CREATED SIZE<br \/>\nCentos latest e934aafc2206 2 weeks ago 199MB<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 Prepare the host system<\/p>\n<p>Prepare a CentOS 7 operating system with the following specific requirements:<\/p>\n<p>Must be a 64-bit operating system. The kernel is above 3.8 to view your CentOS kernel with the following command:<\/p>\n<p># uname -r<\/p>\n<p>2 Install Docker<\/p>\n<p># yum install docker Use the following command to see if Docker is [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7733"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7733"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7733\/revisions"}],"predecessor-version":[{"id":7734,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7733\/revisions\/7734"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}