Home  >  Article  >  Java  >  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

高洛峰
高洛峰Original
2017-02-03 13:29:491334browse

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

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

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

After the installation is completed, see the effect

docker run learn/tutorial /bin/echo hello world

Interactively enter the newly installed virtual machine

docker run -i -t learn/tutorial /bin/bash

You will see:

root@51774a81beb3:/#

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 #退出

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

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

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

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

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

ssh root@127.0.0.1 -p 49154

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

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

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:
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