Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker to build Java Web running Environment

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how to use Docker to build Java Web operating environment related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

Let's first review the architecture of traditional virtualization technologies:

It can be seen that we can install multiple virtual machines on the host operating system, and in each virtual machine, a virtual operating system is realized through virtualization technology, and then, on the virtual operating system, install the applications you need. All this seems very simple, but the technical details are so unfathomable that the great gods 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, and we need to wait a long time to see the login interface. Once the virtual machine is started, a network connection can be established with the host to ensure that the virtual machine and the host are interconnected. Different virtual machines are isolated from each other, that is to say, they do not know the existence of each other, but each virtual machine takes up the hardware and network resources of the host.

Let's compare the architecture of docker technology again:

It can be seen that on the operating system of the host, there is a docker service running (or "docker engine"). On this service, we can open multiple docker containers, and each docker container can run the applications we need. The docker containers are also isolated from each other. Similarly, they all occupy the hardware and network resources of the host.

Compared with the virtual machine, the docker container has an essential leap in startup speed compared with the virtual machine, except in the technical implementation, starting a container only in the blink of an eye. Both virtual machines and docker containers are designed to isolate the environment in which applications run, save our hardware resources, and provide benefits for our developers.

It needs to be emphasized that the author is not denying virtualization technology, but I want to let more readers know how to use docker technology through this article, and let everyone know that in addition to virtualization technology, there is another alternative technology that can also isolate applications.

Raw material

prerequisite

First, you need to prepare an operating system for centos, or a virtual machine. In short, you can access the centos operating system through the linux client tool.

It should be noted that ubuntu or other linux operating systems can also play docker, but this article chose to take centos as an example, that's all.

Specific requirements for centos are as follows:

Must be a 64-bit operating system

Kernel above 3.8 is recommended.

View your centos kernel with the following command:

Uname-r

If the output kernel version number is lower 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:

Rpm-import https://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:

Vi / etc/grub.conf

Change default=1 to default=0.

Finally, restart the operating system with the reboot command.

If nothing happens after the reboot, look at the kernel again and your centos kernel will show 3.10.

If you come here, you and we expect the same result. Congratulations! Let's install docker together.

Install docker

You can install the docker software simply by using the following command:

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:

Docker version

If you output the version number of docker, it means that the installation is successful, and we can start using docker.

The docker service can be started with the following command:

Service docker start

Practice

Just like we used to install software, we first need to have a CD that burns the software, and if you are using a virtual optical drive, you need to run a file called "Image" to install the software. In the world of docker, there is also a thing called "image", which has installed the operating system we need. We are usually called "docker image", which is referred to as "image" in this article.

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

It is true that the official website of docker has provided all the download addresses of images, but it is not accessible in China. Fortunately, domestic kind-hearted people have provided a docker Chinese website, on which we can download the docker images we need.

Download the image

We might as well take centos as an example and download an image of centos through the following steps.

First of all, visit the docker Chinese website and search the home page for an image named "centos". In the search results, there is an "official image", which is what we need.

Then, go to the centos official image page, and in the "pull this repository" input box, there is a command to copy it, run the command on your own command line, and then download the image 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 image "docker.cn/docker/centos", or call it repository, which has a label called "centos6" (tag), and an image id called "25c5298b1a36" (maybe the image id you see is not the same as here, which is normal, because the number is randomly generated). In addition, we can see that the image is only 215.8 mb, which is very small and not as large as the image file of the virtual machine.

Now that we have an image, we need to use it to start the container.

Start the container

The container runs on the basis of an image, and once the container is started, we can log in to the container and install the software or applications we need. Now that the image has been downloaded locally, how can I start the container?

You can start the container simply by using the following command:

Docker run-I-t-v / root/software/:/mnt/software/ 25c5298b1a36 / bin/bash

This command is relatively long, let's break it down a little bit, but it actually contains the following three parts:

Docker run

The relevant parameters include:

-I: means to run the container in "interactive mode"

-t: indicates that the container will enter its command line after startup

-v: indicates which local directory needs to be mounted to the container. Format:-v:

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

To be clear, it is not necessary to use "mirror id", but you can also use "repository name: tag", for example: docker.cn/docker/centos:centos6.

The initial command indicates the command that needs to be run once the container is started, and use "/ bin/bash" at this time to do nothing, just enter the command line.

Install related softwar

In order to build the java web running environment, we need to install jdk and tomcat. The following procedures are carried out inside the container. We might as well choose the / opt/ directory as the installation directory, and first we need to enter it through the cd / opt/ command.

Install jdk ()

First, extract 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/

Set environment variabl

First, edit the .bashrc file

Vi / .bashrc

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

Export java_home=/opt/jdkexport path=$path:$java_home

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

Source / .bashrc

Write and run scripts

We need to write a running script, and when we start the container, run the script and start tomcat, as follows:

First, create a run script:

Vi / root/run.sh

Then, edit the script as follows:

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

Note: you must load the environment variables here, and then start the tomcat service using tomcat's run script.

Finally, add execution permissions to run the script:

Chmod uplix / root/run.sh

Exit the container

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

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

Docker ps

At this point, you should not see any running programs, because you have just exited the container using the exit command, and the container is stopped. You can use the following command to view all containers:

Docker ps-a

The output is as follows:

Container id image command created status ports names

57c312bbaad1 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 will use this container to create an image that can run java web.

Create a java web image

Create a new "mirror" based on a "container id" using the following command:

Docker commit 57c312bbaad1 huangyong/javaweb:0.1

The id of the container is "57c312bbaad1", the image created is "huangyong/javaweb:0.1", and the image can then be used to start the javaweb container.

Start the java web container

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

Repository tag image id created virtual size

Huangyong/javaweb 0.1 fc826a4706af 38 seconds ago 562.8 mb

Docker.cn/docker/centos centos6 25c5298b1a36 7 weeks ago 215.8 mb

As you can see, you have seen the newly created mirror "huangyong/javaweb:0.1" and its mirror id is "fc826a4706af". As described above, we can start the container by "image name" or "image id". Unlike the last time we started the container, we no longer enter the container's command line, but start the tomcat service inside the container directly. At this point, you need to use the following command:

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

A brief 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: indicates the port mapping between the host and the container. At this time, port 8080 inside the container is mapped to port 58080 of the host, so port 58080 is exposed to the outside world, and the port 8080 inside the container can be accessed through the docker bridge.

-- name: represents the name of the container and can be named with a meaningful name.

With regard to the content of the docker bridge, it needs to be supplemented. In fact, docker builds a network communication bridge between the host and the container. We can map the ip address and port number inside the container through the host ip address and port number.

After a series of parameters is the "image name" or "image id", which can be obtained as conveniently as possible. Finally, there is the "initial command", which is the run script written above, which encapsulates the command to load environment variables and start the tomcat service.

After running the above command, a long list of "container id" will be output immediately, 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 seconds ago up 58080-> 8080/tcp javaweb

Taste

In the browser, enter the following address to access the tomcat home page

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

These are all the contents of the article "how to use Docker to build a Java Web runtime environment". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report