Use Docker to build TOMCAT runtime environment

Source: Internet
Author: User
Tags unpack centos java web port number tomcat docker ps docker run

Due to display formatting problems, recommended to the original blog read: https://my.oschina.net/sunchp/blog/616526

1 Docker vs. virtual machines

2 Building process

2.1 Preparing the host system

Prepare a CentOS 7 operating system with the following requirements:

Must be a 64-bit operating system

Recommended kernel is above 3.8

View your CentOS kernel using the following command:

# Uname-r

2.2 Installing Docker

# Yum Install Docker

You can use the following command to see if Docker is installed successfully:

# Docker version

If the version number of the Docker is output, the installation is successful and the Docker service can be started with the following command:

# Systemctl Start Docker.service

Once the Docker service is up and ready, you can start using Docker.

2.3 Download Image

Take CentOS, for example, to download a CentOS image:

# Docker Pull centos:7.2.1511

When the download is complete, use the command to view the local mirror list:

# Docker Images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/centos 7.2.1511 83ee614b834e 9 weeks ago 194.6 MB

2.4 Starting the container

Containers are run on the basis of mirroring, and once the container is started, we can log in to the container and install the software or application we need.

Use the following command to start the container:

# docker Run-i-t-v/root/software/:/mnt/software/83ee/bin/bash

The command consists of the following three sections:

Docker run < related parameters > < mirroring id> < initial commands >

Among them, the relevant parameters include:

-I: Indicates that the container is running in "interactive mode"

-T: Indicates that the container will enter its command line when it is started

-V: Indicates which directory needs to be mounted to the container, format:-v < host directory >:< container directory >

In this example, all the installers are placed in the/root/software/directory of the host and now need to be mounted to the container's/mnt/software/directory.

# pwd
/root/software

# ls
apache-tomcat-7.0.67.tar.gz jdk1.7.0_79.tar.gz

The initial command indicates the command to run once the container is started, using "/bin/bash" at this point, which means that the bash shell is entered directly after launch.

2.5 Installing the Software

In order to build the Java Web environment, the JDK and Tomcat need to be installed, and the following procedures are performed inside the container. In this example, selecting the/opt/directory as the installation directory requires first entering the directory through the cd/opt/command.

2.5.1 Installing the JDK

First, unpack the JDK package:

# tar-zxf/mnt/software/jdk1.7.0_79.tar.gz-c.

Then, move the JDK directory:

# MV jdk1.7.0_79//opt/jdk/

2.5.2 Installing Tomcat

First, unpack the Tomcat package:

# tar-zxf/mnt/software/apache-tomcat-7.0.67.tar.gz-c.

Then, move the Tomcat directory:

# MV apache-tomcat-7.0.67//opt/tomcat/

2.5.3 Writing Run scripts

Write a run script that, when the container is started, runs the script and starts Tomcat.

First, create a run script:

# touch/root/run.sh

# vi/root/run.sh

Then, edit the script content as follows:

#!/bin/bash

export java_home=/opt/jdk/
export path= $JAVA _home/bin: $PATH

sh/opt/tomcat/bin/ Catalina.sh Run

Finally, add execute permissions to run the script:

# chmod u+x/root/run.sh

2.6 Exiting the container

When all of the above steps are complete, you can use the Exit command to exit the container.

You can then use the following command to view the running container:

# docker PS

At this point, you should not see any programs that are running, because the container that you just exited with the Exit command is in a stopped state, and you can view all the containers by using the following command:

# docker ps-a
CONTAINER ID IMAGE COMMAND CREATED STATUS
02bebc3f546a 83ee "/bi N/bash "Minutes ago Exited (0) 7 seconds ago

Remember the above container ID (container ID), which will then be used to create a running Tomcat image.

2.7 Creating a Tomcat image

Use the following command to create a new "mirror" based on a "container ID":

# Docker commit 02be mytomcat:1.0
65c88ec597e04812ec3b06b7749578bebcae3aa3d735b565ed25db6818d9d7f3

# Docker Images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Mytomcat 1.0 65c8 8ec597e0 about a minute ago 514.4 MB
Docker.io/centos 7.2.1511 83ee614b834e 9 weeks ago 194.6 MB

The container's ID is 02be, the image name created is "mytomcat:1.0", and then the Tomcat container can be started using mirroring.

2.8 Launching the Tomcat container

First, create a new/root/webapps/root directory and a index.html file in that directory with the following file contents:

 

As described above, the container can be started with a "mirror name" or "Mirror ID", unlike the last boot container, which now no longer enters the container's command line, but instead directly launches the TOMCAT service inside the container. At this point, you need to use the following command:

# Docker run-d-P 58080:8080-v/root/webapps/:/opt/tomcat/webapps/--name mytomcat_1 mytomcat:1.0/root/run.sh

Among them, the relevant parameters include:

-D: The/root/run.sh script is executed in "daemon mode", at which point the Tomcat console does not appear on the output terminal.

-P: Represents the port mapping of the host to the container, at which point the 8080 port inside the container is mapped to the 58080 port of the host, exposing 58080 ports to the outside, and can access the 8080 ports inside the container via the Docker bridge.

-V: Indicates which directory needs to be mounted to the container, format:-v < host directory >:< container directory >

--name: Represents the container name and is named with a meaningful name.

In the browser, you can access Tomcat by entering the host IP and port number:


2.9 Final schematic:

2.10 Stop Tomcat Container

# docker ps-a
CONTAINER ID IMAGE COMMAND CREATED STATUS
f23598b6544d mytomcat:1 .0 "/root/run.sh" 6 minutes ago up 6 minutes

# Docker Stop f235

2.11 Removing a container

# docker ps-a
CONTAINER ID IMAGE COMMAND CREATED STATUS
f23598b6 544d mytomcat:1.0 "/root/run.sh" 8 minutes ago Exited (137)

# docker RM f235
f235

# docker ps-a
CONTA Iner ID IMAGE COMMAND CREATED STATUS

2.12 Removing a mirror

# docker images repository          tag         IMAGE ID       CREATED          virtual size mytomcat             1.0        65c88ec597e0   &NBSP;31&NBSP;MINUTES&NBSP;AGO&NBSP;&NBSP;514.4&NBSP;MB docker.io/centos    7.2.1511  &NBSP;&NBSP;83EE614B834E&NBSP;&NBSP;&NBSP;9&NBSP;WEEKS&NBSP;AGO&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;194.6&NBSP;MB #  docker rmi 65c8 untagged: mytomcat:1.0 deleted:  65C88EC597E04812EC3B06B7749578BEBCAE3AA3D735B565ED25DB6818D9D7F3 # docker images REPOSITORY           tag        image  id       creAted         virtual size docker.io/centos     7.2.1511   83ee614b834e   9 weeks ago      194.6 mb


Article turned from: https://my.oschina.net/sunchp/blog/616526

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.