Home Java javaTutorial Detailed explanation of Docker study notes to build a JAVA Tomcat operating environment

Detailed explanation of Docker study notes to build a JAVA Tomcat operating environment

Feb 03, 2017 pm 01:29 PM

Preface

Docker aims to provide an automated deployment solution for applications, quickly creating a container (lightweight virtual machine) on a Linux system and deploying and running applications, and through configuration files It is very convenient to automate the installation, deployment and upgrade of applications. Because of the use of containers, the production environment and development environment can be easily separated without affecting each other. This is the most common way of playing docker. More ways to play include large-scale web applications, database deployment, continuous deployment, clusters, test environments, service-oriented cloud computing, virtual desktop VDI, etc.

Subjective impression: Docker is written in Go language, uses cgroup to achieve resource isolation, and the container technology uses LXC. It provides a lightweight virtualization solution that can run Unix processes independently. It provides a way to automate the deployment of software in a secure, repeatable environment. LXC commands are a bit complicated. If you are interested, here is an article I wrote before based on LXC (building a simple version of JAVA PAAS cloud platform from scratch). You can review it in advance.

The implementation principles, related theories, application scenarios, etc. will be written later in this series. Here is a quick taste, completely manual, to build a Tomcat running environment based on Docker. First come out a decent Demo, we can see the effect, which may help us go further.

Environment

In all environments of this article, ubuntu-13.10-server-amd64 is running on VMware WorkStation. Note that it is a 64-bit system. In theory, other virtual machines are also completely feasible.

Installing Docker

Docker version 0.7 requires linux kernel 3.8 support and the AUFS file system.

# 检查一下AUFS是否已安装
sudo apt-get update
sudo apt-get install linux-image-extra-`uname -r`
# 添加Docker repository key
sudo sh -c "wget -qO- https://get.docker.io/gpg | apt-key add -"
# 添加Docker repository,并安装Docker
sudo sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
sudo apt-get update
sudo apt-get install lxc-docker
# 检查Docker是否已安装成功
sudo docker version
# 终端输出 Client version: 0.7.1
Go version (client): go1.2
Git commit (client): 88df052
Server version: 0.7.1
Git commit (server): 88df052
Go version (server): go1.2
Last stable version: 0.7.1
Copy after login

Remove sudo

Under Ubuntu, when executing Docker, you have to enter sudo and password every time. It is very tiring. Let’s fine-tune it here and give the current user execution permissions. Add it to the corresponding docker user group.

# 添加一个新的docker用户组
sudo groupadd docker
# 添加当前用户到docker用户组里,注意这里的yongboy为ubuntu server登录用户名
sudo gpasswd -a yongboy docker
# 重启Docker后台监护进程
sudo service docker restart
# 重启之后,尝试一下,是否生效
docker version
#若还未生效,则系统重启,则生效
sudo reboot
Copy after login

Install a Docker running instance-ubuntu virtual machine

After Docker is installed, the background process is automatically started, and the virtual machine instance can be installed (here is the learn/tutorial image used in the official demonstration) For example):

docker pull learn/tutorial
Copy after login

After the installation is completed, see the effect

docker run learn/tutorial /bin/echo hello world
Copy after login

Interactively enter the newly installed virtual machine

docker run -i -t learn/tutorial /bin/bash
Copy after login

You will see:

root@51774a81beb3:/#
Copy after login

It means you have entered the interactive environment.

Install the SSH terminal server to facilitate our external use of the SSH client to log in and access

apt-get update
apt-get install openssh-server
which sshd
/usr/sbin/sshd
mkdir /var/run/sshd
passwd #输入用户密码,我这里设置为123456,便于SSH客户端登陆使用
exit #退出
Copy after login

Get the instance container ID of the instance you just operated

#docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
51774a81beb3 learn/tutorial:latest /bin/bash 3 minutes ago Exit 0 thirsty_pasteur
Copy after login

You can see that the container ID of the current operation is: 51774a81beb3. Note that once all operations are performed, they need to be submitted and saved for easy SSH login:

docker commit 51774a81beb3 learn/tutorial
Copy after login

Run this mirror instance as a background process for a long time:

docker run -d -p 22 -p 80:8080 learn/tutorial /usr/sbin/sshd -D
Copy after login

The SSH Server running in the ubuntu container occupies port 22, and -p 22 is specified. -p 80:8080 means that our ubuntu will run tomcat on port 8080, but the port mapped externally (outside the container) is 80.

At this time, check whether it runs successfully.

#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
871769a4f5ea learn/tutorial:latest /usr/sbin/sshd -D About a minute ago Up About a minute 0.0.0.0:49154->22/tcp, 0.0.0.0:80->8080/tcp focused_poincare
Copy after login

Note that the randomly assigned SSH connection port number here is 49154:

ssh root@127.0.0.1 -p 49154
Copy after login

Enter the password, can you enter? Once you control SSH, the rest is very simple, install JDK, install tomcat, etc., it's up to you. The following is the installation script:

# 在ubuntu 12.04上安装oracle jdk 7
apt-get install python-software-properties
add-apt-repository ppa:webupd8team/java
apt-get update
apt-get install -y wget
apt-get install oracle-java7-installer
java -version
# 下载tomcat 7.0.47
wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.47/bin/apache-tomcat-7.0.47.tar.gz
# 解压,运行
tar xvf apache-tomcat-7.0.47.tar.gz
cd apache-tomcat-7.0.47
bin/startup.sh
Copy after login

By default, tomcat will occupy port 8080. When starting the mirror instance just now, -p 80:8080, ubuntu mirror instance/container, was specified. Open port 8080 and map it to the host port 80. If you know the host IP address, you can access it freely. On the host machine, just test it through curl:

curl http://192.168.190.131
Copy after login

Of course, you can also use a browser to access it.

In real situations, tomcat may not be allowed to directly open port 80 to the outside world. It is usually located behind nginx/apache or a firewall. The above is only a demonstration.

Summary

Building a Tomcat runtime environment with the help of Docker is very simple in general and allows us to see the presence of PAAS. Yes, using Docker as the underlying service of PAAS is not complicated in itself. There is time now to talk about how to use script files to build an image instance, and also talk about the implementation principles and mechanisms of Docker.

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

For more detailed explanations of Docker study notes and related articles on building a JAVA Tomcat operating environment, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? Apr 19, 2025 pm 11:18 PM

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

See all articles