Docker Deployment Apache-tomcat

Source: Internet
Author: User
Tags docker ps docker run

Docker Deployment Apache-tomcat

Lab Environment:
Centos 7
Phpstudy (provide wget download the following two packages to CentOS)
Required Packages:
Jdk-8u11-linux-x64.tar.gz
Apache-tomcat-7.0.84.tar.gz
Provide download link: Https://pan.baidu.com/s/1miZh9wO Password: 8d5f

Docker is an open-source application container engine that allows developers to package their applications and dependencies into a portable container, and then publish them to any popular Linux machine or virtualize them. Containers are completely sandbox-aware and do not have any interfaces with each other.

We can open multiple Docker containers, and each Docker container can run its own application, and the Docker containers are isolated from each other, as well as the hardware and network resources of the host.

First,. Configure the Yum source
Refer to: http://blog.51cto.com/12445563/2065914 The following code after configuration.

[[email protected] ~]# setenforce 0                                       临时关闭selinux[[email protected] ~]# systemctl stop firewalld.service         关闭firewalld防火墙[[email protected] ~]# yum install docker -y                         安装docker[[email protected] ~]# systemctl start docker.service           启动docker服务

Ii.. Download image
Search for available Docker images: Docker Search Name
will be from the official search matching to the image can also go to configure the domestic source download relatively faster

[[email protected] ~]# docker pull centos:latest  下载最新Centos镜像Trying to pull repository docker.io/library/centos ... latest: Pulling from docker.io/library/centosaf4b0a2388c6: Pull complete Digest: sha256:2671f7a3eea36ce43609e9fe7435ade83094291055f1c96d9d1d1d7c0b986a5d[[email protected] ~]# docker images                   看本地所有的镜像REPOSITORY          TAG                 IMAGE ID            CREATED             SIZEdocker.io/centos       latest              ff426288ea90        2 weeks ago         207.2 MBdocker的镜像并不大 这个最新的才207.2MB

Iii.. Download two Packages
Put two packages in the root directory of the Phpstudy website first

[[email protected] ~]# mkdir/root/cany Create a storage directory and so on can be mounted inside the container [[email protected] ~]# cd/root/cany/ Put the package in this folder [[email protected] cany]# wget http://10.2.4.144/apache-tomcat-7.0.84.tar.gz--2018-01-29 04:47:43 --http://10.2.4.144/apache-tomcat-7.0.84.tar.gzConnecting to 10.2.4.144:80 ... Connected. HTTP request sent, awaiting response ... oklength:9032792 (8.6M) [application/x-gzip]saving to: ' Apache-tomcat-7.0.84.tar.gz ' 100%[======================   ========================================================================================>] 9,032,792--.-K/s In 0.07s 2018-01-29 04:47:43 (+ MB/s)-' apache-tomcat-7.0.84.tar.gz ' saved [9032792/9032792][[email protected] C any]# wget http://10.2.4.144/jdk-8u11-linux-x64.tar.gz--2018-01-29 04:47:58--http://10.2.4.144/ Jdk-8u11-linux-x64.tar.gzconnecting to 10.2.4.144:80 ... Connected. HTTP request sent, awaiting response ... oklength:159019376 (152M) [application/x-gzip]saving to: ' jdk-8u11-linux-x64.tar.gz ' 100%[===================================================================================== =========================>] 159,019,376 23.2mb/s in 12s 2018-01-29 04:48:10 (12.5 mb/s)-' Jdk-8u11-linux-x64.tar. GZ ' saved [159019376/159019376]

Iv.. Creating a container
Usage: Docker run < related parameters > < mirroring id> < initial commands >

                                                                   IMAGE IDdocker run -i -t -v /root/cany/:/mnt/cany/ ff426288ea90 /bin/bash

Relevant parameters include:

-I: Indicates that the container is running in "interactive mode"
-T: Indicates that the container will enter its command line when it is started
-V: Indicates which directory needs to be mounted to the container, format:-v < host directory >:< container directory >
Use/bin/bash at this point to indicate that nothing is done, just go to the command line

V.. installation of service-related environments and software
Build Apache-tomcat service requires JDK and Tomcat installation (I choose/opt/Directory as the installation directory)
The following procedures are performed inside the container:

[[email protected] /]# cd /mnt/cany/                                  进入挂载的目录[[email protected] cany]# tar -zxf                                      看到了我们下载的两个软件包 apache-tomcat-7.0.84.tar.gz  jdk-8u11-linux-x64.tar.gz           [[email protected] cany]# tar -zxf apache-tomcat-7.0.84.tar.gz        进行解压[[email protected] cany]# tar -zxf jdk-8u11-linux-x64.tar.gz           [[email protected] cany]# mv apache-tomcat-7.0.84/ /opt/tomcat/   把解压后的文件夹转移到我们选择的安装目录[[email protected] cany]# mv jdk1.8.0_11/ /opt/jdk/  

Setting environment variables
1. Edit the. bashrc file

[[email protected] cany]# vi ~/.bashrc

Then, add the following configuration at the end of the file:

export JAVA_HOME=/opt/jdkexport PATH=$PATH:$JAVA_HOME

[[email protected] cany]# source ~/.bashrc   用source命令让环境变量生效

2. Write the container to start the run script
After all, container mirroring is less native and lacks many environments to write scripts
When you start the container, run the script and start Tomcat with the following steps:

[[email protected] cany]# vi /root/cany.sh#!/bin/bashsource ~/.bashrc                                    必须先加载环境变量 sh /opt/tomcat/bin/catalina.sh run          然后使用 Tomcat 的运行脚本来启动[[email protected] cany]# chmod u+x /root/cany.sh   对当前目录下的cany.sh文件的所有者增加可执行权限

When all of the above steps are complete, you can use the Exit command to exit the container.

Vi. creating a Tomcat image
For the previous configuration we can create a mirror to facilitate direct use or transfer to another machine can also be used in the following specific operations:

[[email protected] cany]# docker ps              查看正在运行的容器    怎么会没有呢  我们刚才不是已经创建了一个吗?CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES[[email protected] cany]# docker ps -a          查看所以容器看看究竟  发现还在CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMESc6a61597a46d        ff426288ea90        "/bin/bash"         36 minutes ago      Exited (0) 41 seconds ago                       kickass_hopper

Because the container has just been exited with the exit command, the container is in a stopped state, so every time you go into the container and change things, remember to open the container.

Remember the above container ID (container ID), and so on we create a mirror that can run Tomcat through the container.

The relevant commands are as follows:

docker stop  <容器名or ID>          停止某个容器docker start  <容器名or ID>          启动某个容器docker kill    <容器名or ID>           杀掉某个容器docker rm    <容器名or ID>           删除某个容器docker rmi IMAGE                        删除镜像docker ps -a -q                             查看所有容器IDdocker rm $(docker ps -a -q)        删除所有的容器:docker attach <容器名or ID>        attach到运行中的容器

Make a new image of what we just configured

[[email protected] ~]# docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMESc6a61597a46d        ff426288ea90        "/bin/bash"         58 minutes ago      Exited (0) 5 minutes ago                       kickass_hopper[[email protected] ~]# docker commit c6a61597a46d cany/tomcat:0.1sha256:7cbbe3742372edc2a19ba37ea5684a573c92d2e08be437a9d6028688371b5152[[email protected] ~]# docker images REPOSITORY          TAG                 IMAGE ID            CREATED             SIZEcany/tomcat         0.1                 7cbbe3742372        18 seconds ago      544.9 MBdocker.io/centos    latest              ff426288ea90        2 weeks ago         207.2 MB

Did you see it? The image is named Cany/tomcat

Vii. starting the Tomcat mirror container
Unlike the last boot container, we now no longer enter the container's command line, but instead directly start the TOMCAT service inside the container

[[email protected] ~]# docker run -d -p 80:8080 --name web cany/tomcat:0.1 /root/cany.sh401ddf6c97128e377df9ae8f3992ef2b065d053475387e1fac6f49e2b7390414[[email protected] ~]# docker psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES401ddf6c9712        cany/tomcat:0.1     "/root/cany.sh"     32 seconds ago      Up 29 seconds       0.0.0.0:80->8080/tcp   web

-D: The/root/cany.sh script is executed in "daemon mode", at which point the Tomcat console does not appear on the output terminal.
-P: Represents the port mapping of the host to the container, at which point the 8080 port inside the container is mapped to the 80 port of the host, exposing 80 ports to the outside, and can access the 8080 ports inside the container via the Docker bridge.
--name: Represents the container name and is named with a meaningful name.

Docker Bridge: Maps the IP address and port number inside the container with the host IP address and port number

Eight, testing

http://10.2.4.88/   这里使用的是宿主机的 IP 地址 容器的8080端口映射到了80端口 因为http默认是80 所以不需要打端口 直接访问

How is Hello Cany?
A: Because I went to the/opt/tomcat/webapps/root directory to delete all the files created index.html but you can ignore this step, so I didn't write it up.

Summary: When I started to learn about Docker, I went online to look at other great God articles found quite a lot of talk is not very full and did not provide the relevant software package to consult the data finally made through this experiment and uploaded my personal experiment package I hope this article can help just get started Docker friends!!!

Docker Deployment Apache-tomcat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.