Using Docker to build a Java WEB runtime environment

Source: Internet
Author: User
Tags tag name unpack docker ps docker run

Original http://www.importnew.com/21798.html

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

This article makes a comparison between virtualization technology and Docker container technology, and then leads to some Docker terminology, such as containers, mirrors, and so on, then uses Docker to build a Java WEB runtime environment, and finally a summary of this article.

Let's go back to the architecture of traditional virtualization technologies:

As we can see, we have multiple virtual machines installed on the host's operating system, and in each virtual machine, through virtualization technology, a virtual operating system is implemented, and then, on that virtual operating system, you can install the application you need. It all seems very simple, but the technical details are quite inscrutable, and the big-god figures are not necessarily clear.

Anyone who has 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, you can establish a network connection with the host to ensure that the virtual machine is connected to the host. Different virtual machines are isolated from each other, that is, they do not know each other's existence, but each virtual machine occupies a host of 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 "Docker Engine"), on this service, we can open multiple Docker containers, 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 virtual machines, in addition to the technical implementation of a completely different, boot speed than the virtual machine has an essential leap, start a container only in the 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 for our developers.

Let's look at the Docker Logo again:

It is clear that this is a whale, which is holding many containers. We can treat the host as the whale, and the isolated containers as containers, each containing its own application. This Logo is simply too image!

It should be stressed that the author is not denying virtualization technology, but to let more readers understand how to use Docker technology, let everyone know that in addition to virtualization technology, there is another alternative technology, but also to isolate the application.

Below, we'll combine the deployment process of a Java Web application to describe how to "cook" Docker's delicacy. Are you ready? Let's get started now!

Pre-requisites for raw materials

First, you have to prepare a CentOS operating system, and the virtual machine is OK. In summary, the CentOS operating system can be accessed via the Linux Client tool.

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

CentOS specific requirements are as follows:

    1. Must be a 64-bit operating system
    2. Recommended kernel is above 3.8

View your CentOS kernel using the following command:

1 uname-r

If the kernel version number of the output is less than 3.8 after executing 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:

123 rpm --importhttps://www.elrepo.org/RPM-GPG-KEY-elrepo.orgrpm -ivh http://www.elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpmyum -y --enablerepo=elrepo-kernel install kernel-lt

Then, edit the following configuration file:

1 vi /etc/grub.conf

will be default=1 modified to default=0 .

Finally, restart the reboot operating system by command.

If there is no accident after restarting, check the kernel again and your CentOS kernel will be displayed as 3.10.

If you come here, you and we expect the result is consistent. Congratulations to you! Now we're going to install Docker together.

Installing Docker

You can install Docker software with the following command:

12 rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpmyum -y install docker-io

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

1 docker version

If the version number of the Docker is output, then the installation is successful and we can start using Docker below.

The Docker service can be started with the following command:

1 ervice docker start
Practice

Just like installing software, we first need to have a disc that burns the software, and if you are using a virtual optical drive, you need to run a file called "mirroring" to install the software. In the Docker world, there is also a thing called "Mirror", has installed our required operating system, we generally become "Docker mirror", this article referred to as "mirror".

So here's the question, where do we download the image?

The Docker website does provide all the mirrors, but it's not accessible at home. Fortunately, the domestic goodwill people have provided a Docker Chinese web site where we can download the Docker images we need.

Download image

We might as well use CentOS as an example to download a CentOS image by following the steps below.

First, visit the Docker Chinese web, search the first page of the image named "CentOS", in the results of the search, there is an "official image", it is what we need.

Then, go to the official CentOS image page, in the "Pull this repository" input box, there is a command, copy it, run the command on its own command line, and then download the image immediately.

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

1 docker images

When the download is complete, you should see:

12 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 call it a warehouse (Repository), which has a label named "Centos6", and a tag called " 25c5298b1a36 "Image ID (perhaps the image ID you see is inconsistent with this one, which is normal because the number is randomly generated). In addition, we can see that the image is only 215.8 MB, very small, and not as large as the virtual machine's image file.

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

Start 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. Now that the image has been downloaded locally, how can you start the container?

Just use the following command to start the container:

1 docker run -i -t -v /root/software/:/mnt/software/ 25c5298b1a36 /bin/bash

This command is longer, we break it down a little bit, actually contains the following three parts:

1 docker run <相关参数> <镜像 ID> <初始命令>

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 >

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

It is necessary to note that you do not have to use "mirror ID" or "warehouse Name: Tag name", for example: DOCKER.CN/DOCKER/CENTOS:CENTOS6.

The initial command indicates the command to run once the container is started, using "/bin/bash" at this time to indicate nothing, just go to the command line.

Install related software

In order to build the Java WEB environment, we need to install the JDK and Tomcat, the following procedures are carried out inside the container. We might as well choose /opt/ the directory as the installation directory, first through cd /opt/ the command to enter the directory.

Installing the JDK

First, unpack the JDK package:

1 tar-zxf /mnt/software/jdk-7u67-linux-x64.tar.gz -C .

Then, rename the JDK directory:

1 mvjdk1.7.0_67/ jdk/
Installing Tomcat

First, unpack the Tomcat package:

1 tar-zxf /mnt/software/apache-tomcat-7.0.55.tar.gz -C .

Then, rename the Tomcat directory:

1 mvapache-tomcat-7.0.55/ tomcat/
Setting environment variables

First, edit the .bashrc file

1 vi~/.bashrc

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

12 exportJAVA_HOME=/opt/jdkexportPATH=$PATH:$JAVA_HOME

Finally, you need to use source the command to make the environment variable effective:

1 source~/.bashrc
Writing Run scripts

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

First, create a run script:

1 vi/root/run.sh

Then, edit the script content as follows:

123 #!/bin/bashsource~/.bashrcsh /opt/tomcat/bin/catalina.sh run

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

Finally, add execute permissions to run the script:

1 chmodu+x /root/run.sh
Exit container

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

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

1 docker ps

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

1 docker ps-a

The output is as follows:

12 CONTAINER ID        IMAGE                             COMMAND             CREATED             STATUS                      PORTS               NAMES57c312bbaad1        docker.cn/docker/centos:centos6   "/bin/bash"27 minutes ago      Exited (0) 19 seconds ago                       naughty_goldstine

Remember the above CONTAINER ID (container ID), and then we'll create a mirror that runs the Java Web through the container.

Create a Java Web image

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

1 docker commit 57c312bbaad1 huangyong/javaweb:0.1

The container's ID is "57c312bbaad1", the image name created is "huangyong/javaweb:0.1", and the Java Web container can then be started using mirroring.

Start the Java Web container

It is necessary to first use the docker images command to view all current mirrors:

123 REPOSITORY                TAG                 IMAGE ID            CREATED             VIRTUAL SIZEhuangyong/javaweb0.1                 fc826a4706af        38 seconds ago      562.8 MBdocker.cn/docker/centoscentos6             25c5298b1a36        7 weeks ago         215.8 MB

Visible, you now see the newly created mirror "huangyong/javaweb:0.1" with the image ID "Fc826a4706af". As described above, we can start the container with a "mirror name" or "Mirror ID", unlike the last time the container was started, we are now no longer entering the container's command line, but instead directly start the TOMCAT service inside the container. At this point, you need to use the following command:

1 docker run -d -p 58080:8080 --name javaweb huangyong/javaweb:0.1 /root/run.sh

A little explanation:

    • -d: Indicates that the script is executed in "daemon mode" /root/run.sh , 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.
    • --name: Represents the container name and is named with a meaningful name.

The content of the Docker bridge needs to be supplemented with instructions. Actually, Docker builds a bridge between the host and the container, and we can use the host IP address and port number to map the IP address and port number inside the container.

After a series of parameters is the "Mirror name" or "Mirror ID", how convenient to come. The last is the "initial command", which is a running script written above that encapsulates the commands that load environment variables and start the Tomcat service.

When the above command is run, a long list of "container IDs" is immediately output, and we can docker ps view the currently running containers by command.

12 CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS                     NAMES82f47923f926        huangyong/javaweb:0.1   "/root/run.sh" 4 seconds ago       Up 3 seconds        0.0.0.0:58080->8080/tcpjavaweb
Taste

In the browser, enter the following address to access the Tomcat home page:

1 http://192.168.65.132:58080/

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

Summarize

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

In addition to this way of manually generating Docker images, there is a way to create Docker images automatically, much like writing code. Just need us to write a Dockerfile file, then use docker build the command to do all of the above manual operation.

Don't be too surprised, it's all in silence ...

Using Docker to build a Java Web Runtime Environment (RPM)

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.