Explain using Docker to build a Java Web running environment _docker

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

Docker is a "lightweight" container technology that almost shakes the status of traditional virtualization technology, and now more and more companies are starting to use Docker to replace existing virtualization platforms. As a Java programmer, it's time for us to learn Docker together!

This article will be a comparison of virtualization technology and Docker container technology, and then lead to some Docker terminology, such as: containers, mirrors, etc., will then use Docker to build a Java Web running environment, and finally a summary of this article.

Let's take a look at the architecture of traditional virtualization technologies:

Visible, we can install multiple virtual machines on the host's operating system, and in each virtual machine, through virtualization technology, the implementation of a virtual operating system, then, on the virtual operating system, you can install their own required applications. It all seems very simple, but the technical details are quite inscrutable, and the great God-level figures are not necessarily clear.

Anyone who has ever used a virtual machine should know that starting a virtual machine is like starting a computer, the initialization process is quite slow, we need to wait a long time to see the login interface. Once the virtual machine is started, it is possible to establish a network connection with the host to ensure interoperability between the virtual machines and host hosts. The different virtual machines are isolated from each other, that is to say, they do not know each other's existence, but each virtual machine occupies the host's hardware and network resources.

Let's compare the architecture of Docker technology:

Visible, on the host's operating system, there is a Docker service running (or called "Docker Engine"), where we can open multiple Docker containers, and each Docker container can run its own required applications, Docker containers are isolated from each other, Similarly, the host's hardware and network resources are occupied.

Docker containers In contrast to the virtual machine, in addition to the technical implementation of the completely different, starting faster than the virtual machine has an essential leap, start a container only in a blink of an eye. Whether they are virtual machines or docker containers, they are designed to isolate the application's operating environment, save our hardware resources, and provide benefits to our developers.

It should be emphasized that the author is not denying virtualization technology, but rather through this article to let more readers know how to use the Docker technology, let you know that in addition to virtualization technology, there is another alternative technology, but also to isolate the application.

Below, we will combine the deployment process of a Java Web application to describe how to "cook" Docker this delicacy. Are you ready? We'll start now!

Raw materials

Prerequisite conditions

First, you want to prepare a CentOS operating system, and virtual machines are fine. In short, you can access the CentOS operating system through the Linux client tools.

It should be explained that Ubuntu or other Linux operating systems can also play Docker, but this article chose to take CentOS as an example, that's all.

CentOS specific requirements are as follows:

    • Must be a 64-bit operating system
    • Recommended kernel is above 3.8

View your CentOS kernel with the following command:

Uname-r

If the kernel version number of the output is less than 3.8 after you execute the above command, please refer to the following method to upgrade your Linux kernel.

For CentOS 6.5, the kernel version defaults to 2.6. First, you can install the latest kernel with the following command:

RPM--import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
RPM-IVH http://www.elrepo.org/ elrepo-release-6-5.el6.elrepo.noarch.rpm
yum-y--enablerepo=elrepo-kernel Install Kernel-lt

Then, edit the following configuration file:

Vi/etc/grub.conf

Modify the Default=1 to default=0.

Finally, reboot the operating system with the reboot command.

After restarting, if not unexpectedly, look at the kernel again, your CentOS kernel will display as 3.10.

If you come here, you are in agreement with the results we expect. Congratulations to you! Here we are together to install the Docker.

Install Docker

The Docker software can be installed simply by using the following command:

RPM-UVH http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
yum-y Install Docker-io

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 we can start using Docker below.

You can start the Docker service by using the following command:

Service Docker start

Practice

As with the software, we first need a disc that burned the software, and if you are using a virtual optical drive, you need to run a file called "Mirror" to install the software. In the Docker world, there is also a "mirror" thing, has installed the operating system we need, we generally become "Docker mirror", this article is referred to as "mirror."

So the question is, where do we download the mirrors?

Docker website has indeed provided all the mirror download address, but unfortunately in the country is not accessible. Fortunately, the domestic kind-hearted people to provide a Docker Chinese network, the site can download the Docker image we need.

Download Mirror

We might as well take CentOS as an example and download a CentOS mirror with the following steps.

First of all, visit the Docker Chinese network, in the first page to search for a mirror named "CentOS", in the search results, there is an "official mirror", it is what we need.

Then, into the CentOS official mirror page, in the "Pull This repository" input box, there is a command that copies it, runs it on its own command line, and then downloads the mirror immediately.

Finally, use the following command to view all local mirrors:

Docker images

When the download is complete, you should see:

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Docker.cn/docker/centos centos6 25c5298b1a36 7 weeks ago 215.8 MB

If you see the above output, you can use the "docker.cn/docker/centos" image, or refer to it as a warehouse (Repository), which has a label named "CENTOS6" (tag), in addition to the name " 25c5298b1a36 Mirror ID (you may see a mirror ID that is not consistent with this here, which is a normal behavior because the number is randomly generated). In addition, we can see that the mirror is only 215.8 MB, very small, and not as large as the virtual machine's mirrored file.

Now that the mirror is already there, we need to use the mirror below to start the container.

Start 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. Now that the mirrors have been downloaded locally, how do you start the container?

You can start the container by using the following command:

Docker Run-i-t-v/root/software/:/mnt/software/25c5298b1a36/bin/bash

This is a long order, we break it down a bit, and it actually contains the following three parts:

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 >

Assuming that all of our installers are placed in the/root/software/directory of the host, we now need to mount them in the container's/mnt/software/directory.

Note that you do not have to use the mirror ID or the warehouse name: label name, for example: DOCKER.CN/DOCKER/CENTOS:CENTOS6.

The initial command indicates that once the container is started, the command that needs to be run, "/bin/bash" is used to indicate nothing, just go to the command line.

Install related software

In order to build a Java WEB runtime environment, we need to install JDK and Tomcat, and the following procedures are within the container. We may wish to select the/opt/directory as the installation directory, first need to access the directory through the cd/opt/command.

Install JDK (download address)

First, unzip the JDK package:

Tar-zxf/mnt/software/jdk-7u67-linux-x64.tar.gz-c.

Then, rename the JDK directory:

MV jdk1.7.0_67/jdk/

Install Tomcat

First, extract the Tomcat package:

Tar-zxf/mnt/software/apache-tomcat-7.0.55.tar.gz-c.

Then, rename the Tomcat directory:

MV apache-tomcat-7.0.55/tomcat/

Setting environment variables

First, edit the. bashrc file

VI ~/.BASHRC

Then, add the following configuration at the end of the file:

Export JAVA_HOME=/OPT/JDK
export path= $PATH: $JAVA _home

Finally, you need to use the source command to have the environment variable take effect:

SOURCE ~/.BASHRC

Writing Run scripts

We need to write a run script that, when the container is started, run the script and start Tomcat as follows:

First, create the run script:

vi/root/run.sh

Then, edit the script contents as follows:

#!/bin/bash
Source ~/.BASHRC
sh/opt/tomcat/bin/catalina.sh Run

Note: You must first load the environment variables and then use Tomcat's run script to start the Tomcat service.

Finally, add execute permissions for the run script:

chmod u+x/root/run.sh

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

The output is as follows:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57c312bbaad1 docker.cn/docker/centos:centos6 "/bin/bash" minutes ago exited (0) seconds ago Naughty_goldstine

Remember the above container ID (container ID), and then we will create a mirror running the Java Web through the container.

Creating a Java Web image

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

Docker commit 57c312bbaad1 huangyong/javaweb:0.1

The ID of the container is "57c312bbaad1" and the Mirror name created is "huangyong/javaweb:0.1", which can then be used to start the Java Web container.

Start the Java Web container

It is necessary to first use the Docker Images command to view all current mirrors:

repository                TAG                  IMAGE id             created              VIRTUAL SIZE
huangyong/javaweb         0.1                 fc826a4706af        seconds ago      562.8 MB
docker.cn/docker/centos   centos6              25c5298b1a36        7 weeks ago          215.8 MB

Visible, you have seen the most recently created mirror "huangyong/javaweb:0.1" with a mirror ID of "FC826A4706AF". As described above, we can start the container with "mirror name" or "Mirror ID", unlike the last boot container, where we now stop entering the container's command line and start the Tomcat service directly inside the container. At this point, you need to use the following command:

Docker run-d-P 58080:8080--name javaweb huangyong/javaweb:0.1/root/run.sh

To give a little explanation:

    • -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.
    • --name: Represents the container name, with a meaningful name.

About the content of Docker Network Bridge, need to add a note. In fact, Docker between the host and the container, built a network communication bridge, we can through the host IP address and port number to map the container's internal IP address and port number,

After a series of parameters is "mirror name" or "Mirror ID", how convenient to come. Finally, the "Initial command", which is the run script written above, encapsulates the command to load environment variables and start the Tomcat service.

When the above command is run, a long list of "container IDs" is immediately exported, and we can view the currently running container through the Docker PS command.

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82f47923f926 huangyong/javaweb:0.1 "/root/run.sh" 4 seconds ago up 3 seconds 0.0.0.0:58080->8080/tcp JAV Aweb

Taste

In the browser, you can access the Tomcat home page by entering the following address:

http://192.168.65.132:58080/

Note: The host's IP address is used here, with the externally exposed port number 58080, which maps the port number 8080 inside the container.

Summarize

Through this article, we have learned what Docker is? What is the difference between it and the virtual machine? And how to install Docker? How do I download docker mirrors? How do I run the Docker container? How do I install an application within a container? How do I create a mirror on a container? How do I start a container in a service manner? All this seems simple, but the operation is also quite cumbersome, but practice makes perfect, we need to keep practicing.

In addition to this way of manually generating Docker mirrors, there is a way to automatically create Docker mirrors, much like writing code. We only need to write a dockerfile file and then use the Docker Build command to do all of the above manual operations.

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.