Method to build an JAVA Tomcat runtime environment under Docker

  • 2020-06-03 08:58:09
  • OfStack

preface

Docker is designed to provide an automated deployment solution for an application. It is very convenient to quickly create a container (lightweight virtual machine) on an Linux system and deploy and run the application. It is also easy to automate the installation, deployment and upgrade of the application through configuration files. Because of the use of containers, it is easy to separate the production environment from the development environment, which is one of the most common ways of playing docker. Additional gameplay options include large-scale web applications, database deployment, continuous deployment, clustering, test environments, service-oriented cloud computing, virtual desktop VDI, and more.

Subjective impression: Docker is written in Go language, resource isolation is implemented in cgroup, and container technology USES LXC. It provides a lightweight virtualization solution that can run the Unix process independently. It provides a way to automatically deploy software in a secure, repeatable environment. The LXC command is a bit complicated. If you are interested, here is an article I wrote previously based on LXC (build a simple version of JAVA PAAS cloud platform from scratch), you can review 1 in advance.

The implementation principles, related theories and application scenarios will be written later in this series. Here is a brief introduction and a manual operation environment based on Docker. First come out with a decent Demo, you can see the effect, may let us go further 1.

The environment

In this paper, es30EN-13.10-server-amd64 is run on VMware WorkStation in all environments. Note that it is a 64-bit system, and other virtual machines are also completely feasible in theory.

Install Docker

Version 0.7 of Docker requires linux kernel 3.8 support, as well as the AUFS file system.


#  check 1 Under the AUFS Whether installed or not 
sudo apt-get update
sudo apt-get install linux-image-extra-`uname -r`
#  add Docker repository key
sudo sh -c "wget -qO- https://get.docker.io/gpg | apt-key add -" 
#  add Docker repository And install 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
#  check Docker Whether the installation was successful 
sudo docker version
#  Terminal output  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

To get rid of sudo

Under Ubuntu, when executing Docker, you have to enter sudo and password every time, which is very tiring. Here you can make 1 adjustment to add the current user execution permission to the corresponding docker user group.


#  add 1 A new one docker User groups 
sudo groupadd docker
#  Add the current user to docker In the user group, notice this yongboy for ubuntu server Login username 
sudo gpasswd -a yongboy docker
#  restart Docker Background monitoring process 
sudo service docker restart
#  After restarting, try 1 Next, does it take effect 
docker version
# If it does not take effect, the system restarts and takes effect 
sudo reboot

Install 1 Docker running instance -ubuntu virtual machine

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


docker pull learn/tutorial

After the installation is complete, see what happens


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

Indicates that you are in an interactive environment.

Install SSH terminal server to facilitate our external login and access using SSH client


apt-get update
apt-get install openssh-server
which sshd
/usr/sbin/sshd
mkdir /var/run/sshd
passwd # Enter the user password, which I set here as 123456 To facilitate SSH Client login is used 
exit # exit 

Gets the instance container ID that you just operated on


#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 current container ID is: 51774a81beb3. Note that 1 once all operations are performed, they need to be submitted and saved for the convenience of SSH login:


docker commit 51774a81beb3 learn/tutorial

Run this mirror instance for a long time in the future platform process mode:


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

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

At this point, check 1 to see if it runs successfully.


#  add 1 A new one docker User groups 
sudo groupadd docker
#  Add the current user to docker In the user group, notice this yongboy for ubuntu server Login username 
sudo gpasswd -a yongboy docker
#  restart Docker Background monitoring process 
sudo service docker restart
#  After restarting, try 1 Next, does it take effect 
docker version
# If it does not take effect, the system restarts and takes effect 
sudo reboot
0

Note here that the assigned random SSH connect port number is 49154:


#  add 1 A new one docker User groups 
sudo groupadd docker
#  Add the current user to docker In the user group, notice this yongboy for ubuntu server Login username 
sudo gpasswd -a yongboy docker
#  restart Docker Background monitoring process 
sudo service docker restart
#  After restarting, try 1 Next, does it take effect 
docker version
# If it does not take effect, the system restarts and takes effect 
sudo reboot
1

Enter password, is it ok to enter? Once you have control of SSH, the rest is simple: install JDK, install tomcat, whatever you want. The following is the installation script:


#  add 1 A new one docker User groups 
sudo groupadd docker
#  Add the current user to docker In the user group, notice this yongboy for ubuntu server Login username 
sudo gpasswd -a yongboy docker
#  restart Docker Background monitoring process 
sudo service docker restart
#  After restarting, try 1 Next, does it take effect 
docker version
# If it does not take effect, the system restarts and takes effect 
sudo reboot
2

By default, tomcat will occupy port 8080. When the mirror instance was started, -p 80:8080, ubuntu mirror instance/container was specified, opening port 8080, and mapping to the host port is 80. Know the host IP address and you are free to access it. On the host, pass curl test 1:


curl http://192.168.190.131

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

In reality, tomcat may not be allowed to open port 80 directly to the outside world. 1 will generally be behind nginx/apache or the firewall, as shown above.

summary

With the help of Docker, we set up an Tomcat runtime environment, which is generally very simple, so we saw the figure of PAAS. True, using Docker as the underlying PAAS service is not complicated in itself. We will have time to talk about how to build a mirror instance using a script file and how to implement Docker.


Related articles: