Docker the method of setting up the TOMCAT operating environment _docker

Source: Internet
Author: User
Tags java web docker ps docker run

Docker is an open source application container engine that allows developers to package their apps and dependencies into a portable container, and then publish it to any popular Linux machine or virtualization. Containers are completely using the sandbox mechanism, and there will be no interface between each other.

1 Docker and virtual machines


2 Build Process

2.1 Preparing the host system

Prepare a CentOS 7 operating system with the following specific requirements:

Must be a 64-bit operating system

Recommended kernel is above 3.8

View your CentOS kernel with the following command:

# Uname-r

2.2 Installation Docker

# Yum Install Docker

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

# Docker version

If the Docker version number is exported, the installation is successful and the Docker service can be started by using the following command:

# Systemctl Start Docker.service

Once the Docker service is started, you can start using Docker.

2.3 Download Mirrors

Take CentOS as an 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 19 4.6 MB

2.4 Startup Container

The container runs on the basis of mirroring, and once the container is started, we can log in to the container and install the software or application that we need.

You can start the container by using the following command:

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

The command contains the following three sections:

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

Among them, the related parameters include:

-I: means to run the container in interactive mode

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

-V: Indicates which directory you want to mount to the container in the format:-v < host directory >:< container directory >

In this case, all of 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 that once the container is started, the command that needs to be run is used "/bin/bash", which means that it goes directly into the bash shell when it starts.

2.5 Installing software

To build a Java Web run environment, you need to install JDK and Tomcat, and the following procedures are within the container. In this example, select the/opt/directory as the installation directory, first you need to access the directory through the cd/opt/command.

2.5.1 Install JDK

First, unzip 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 Install Tomcat

First, extract 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 Write Run Script

Write a run script, run the script when you start the container, and start Tomcat.

First, create the run script:

# touch/root/run.sh
# vi/root/run.sh

Then, edit the script contents 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 for the run script:

# chmod u+x/root/run.sh

2.6 Exit Container

When all the above steps are complete, you can exit the container using the Exit command.

You can then view the running container by using the following command:

# docker PS

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

# docker ps-a
CONTAINER ID IMAGE COMMAND CREATED STATUS 
02bebc3f546a 83ee "/bin/bash" minutes ago exited (0) # 7 Seconds ago

Remember the above container ID (container ID), and then through the container, you will create a running Tomcat mirror.

2.7 Creating a Tomcat mirror

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

# Docker commit 02be mytomcat:1.0
65c88ec597e04812ec3b06b7749578bebcae3aa3d735b565ed25db6818d9d7f3
# Docker Images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Mytomcat 1.0 65c88ec597e0 about a minute ago 514.4 MB
doc Ker.io/centos 7.2.1511 83ee614b834e 9 weeks ago 194.6 MB

The ID of the container is 02BE, the mirror name you create is "mytomcat:1.0", and you can then use mirroring to start the Tomcat container.

2.8 Start the Tomcat container

First, create a new/root/webapps/root directory with a index.html file in the directory that reads as follows:

 
 

As described above, the container can be started by "mirror name" or "Mirror ID", unlike the last boot container, which now no longer enters the container's command line, but rather 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 related parameters include:

-D: Indicates that the/root/run.sh script is executed in "daemon mode", when the Tomcat console does not appear on the output terminal.

-P: Represents the port mapping of the host and container, at which point 8080 ports inside the container are mapped to 58080 ports of the host, exposing 58080 ports to the outside, and accessing the 8080 ports inside the container through the Docker Network Bridge.

-V: Indicates which directory you want to mount to the container in the format:-v < host directory >:< container directory >

--name: Represents the container name, with a meaningful name.

In the browser, enter the host IP and port number to access Tomcat:

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 Removal Container

# docker ps-a
CONTAINER ID IMAGE COMMAND CREATED STATUS 
f23598b6544d mytomcat:1.0 "/root/run.sh" 8 minutes ago Ex ited (137)
# docker RM f235
f235
# docker ps-a
CONTAINER ID IMAGE COMMAND CREATED STATUS

2.12 Removing mirrors

# Docker Images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Mytomcat 1.0 65c88ec597e0 to minutes ago 514.4 mb
   
    docker.io/centos 7.2.1511 83ee614b834e 9 weeks ago 194.6 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
   

The above is a small set to introduce the Docker to build Tomcat operating environment method, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.