Build a Docker-based Tomcat runtime environment in a few simple steps!

【Foreword】:

Docker aims to provide an automated deployment solution for applications, quickly create a container (lightweight virtual machine) on a Linux system, deploy and run applications, and easily automate the installation and deployment of applications through configuration files. And upgrades are very convenient. Because of the use of containers, it is very convenient to separate the production environment from the development environment without affecting each other. This is the most common way of docker. More ways to play include large-scale web applications, database deployment, continuous deployment, clusters, test environments, service-oriented cloud computing, virtual desktop VDI, and more.

 

Subjective impression: Docker is written in Go language, uses cgroup to achieve resource isolation, and container technology uses LXC. It provides a lightweight virtualization solution that can run Unix processes 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 before based on LXC, (from scratch, build a simple version of the JAVA PAAS cloud platform), you can review it in advance.

 

The implementation principles, related theories, application scenarios, etc. will be written later in this series. Here, let's take a brief, completely manual, and build a Tomcat operating environment based on Docker. Come up with a decent demo first, you can see the effect, it may let us go further.

 

environment

In all environments in 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.

 

Install Docker

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

  • # Check if AUFS is installed
  • 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 if Docker has been installed successfully
  • 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

 

 

remove sudo

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

  • # Add a new docker user group
  • sudo groupadd docker
  • # Add the current user to the docker user group, note that the yongboy here is the ubuntu server login username
  • sudo gpasswd -a yongboy docker
  • # Restart the Docker background monitoring process
  • sudo service docker restart
  • # After restarting, try it to see if it works
  • docker version
  • #If it has not taken effect, the system restarts, it will take effect
  • sudo reboot

 

 

Install a Docker running instance - ubuntu virtual machine

After Docker is installed, the background process is automatically started, and you can install a virtual machine instance (here directly take the learn/tutorial image used in the official demo as an example):

  • docker pull learn/tutorial

 

After the installation is complete, 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

 

will see:

  • root@51774a81beb3:/#

 

The description has entered the interactive environment.

 

Install the SSH terminal server, which is convenient for us to use the SSH client to log in and access externally

  • apt-get update
  • apt-get install openssh-server
  • which sshd
  • /usr/sbin/sshd
  • mkdir /var/run/sshd
  • passwd #Enter the user password, I set it to 123456 here, which is convenient for SSH client login and use
  • exit #exit

 

 

Get the instance container ID of the operation just now

 

  • #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, you need to submit and save, which is convenient for SSH login:

 

  • docker commit 51774a81beb3 learn/tutorial

 

Run this image 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, specified by -p 22. -p 80:8080 means that our ubuntu will run tomcat on port 8080, but the port mapped to the outside world (outside the container) is 80.

At this point, check to see if 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 random SSH connection port number assigned here is 49154:

 

 

Enter the password, is it possible to enter? Once you have SSH in control, the rest is easy, install JDK, install tomcat, etc. as you wish. The following is the installation script:

 

 

By default, tomcat will occupy port 8080. When starting the image instance just now, you specified -p 80:8080, ubuntu image instance/container, open port 8080, and the port mapped to the host is 80. If you know the IP address of the host, you can access it freely. On the host machine, you can test it with curl:

 

 

 

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

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

 

【summary】

Building a Tomcat runtime environment with the help of Docker is generally very simple, allowing us to see the presence of PAAS. Yes, using Docker as the underlying service of PAAS is not complicated in itself. When there is time in the future, I will talk about how to use the script file to build an image instance, and also talk about the implementation principle and mechanism of Docker.

 

cSphere

One-stop Docker container management platform

https://csphere.cn/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325349205&siteId=291194637