Home  >  Q&A  >  body text

如何在docker下搭建一个JAVA Tomcat运行环境,有试过的么?

最新在学习Docker,想搭建一个java tomcat环境,有知道的么?越详细越好,先谢了。

PHPzPHPz2761 days ago749

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-04-21 10:58:11

    I think this blog explains your problem very clearly, you can read it carefully,
    http://www.blogjava.net/yongboy/archive/2013/12/12/407498.html

    Docker aims to provide an automated deployment solution for applications. It can quickly create a container (lightweight virtual machine) on a Linux system and deploy and run applications. It can also easily realize automated installation of applications through configuration files. Deployment and upgrade are very convenient. 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 method 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, etc.

    Subjective impression: Docker is written in Go language, uses cgroups 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.

    Relevant implementation principles, related theories, application scenarios, etc. will be written later in this series. Here is a brief taste of it, completely manual, building 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.

    Install Docker

    Docker version 0.7 requires Linux kernel 3.8 support and the 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 whether 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 permissions 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 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 yet, restart the system and 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, take the learn/tutorial image used in the official demonstration as an example):

    docker pull learn/tutorial
    After the installation is complete, take a look at 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:/#
    Indicates that the interactive environment has been entered.

    Install SSH terminal server to facilitate our external use of 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 #Enter the user password, I set it to 123456 here to facilitate SSH client login
    exit #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 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, 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 is 49154:

    ssh root@127.0.0.1 -p 49154
    After entering the password, can I 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:

    Install oracle jdk 7 on ubuntu 12.04

    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

    Download 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

    Unzip and run

    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, -p 80:8080 was specified. The ubuntu mirror instance/container opens port 8080, which is mapped 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 just a demonstration.

    Summary

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

    reply
    0
  • Cancelreply