首页 > Linux > Docker 安装tomcat

Docker 安装tomcat

三、tomcat镜像

1、准备run.sh

之前的tomcat镜像不具备ssh远程发布功能,另外使用jenkins构建项目后会重启tomcat,而tomcat所在的docker容器pid为1会导致容器重启,从而导致ssh连接断开Jenkins build is unstable。

解决办法就是,开启sshd服务并使用密钥登陆容器,并将tomcat的进程pid设置不为1。

/home/data/sshd_jenkins/run.sh

#!/bin/bash
mkdir -p /var/run/sshd
/opt/tomcat/bin/catalina.sh run &
/usr/sbin/sshd -D

2、tomcat镜像

/home/data/sshd_jenkins/tomcat-install.sh

#!/bin/bash
#################tomcat源码安装##################
rm -rf /docker_tmp/*
cp `pwd`/run.sh /docker_tmp/run.sh
cp `pwd`/auto_deploy.sh /docker_tmp/auto_deploy.sh
#wget http://apache.fayea.com/tomcat/tomcat-8/v8.0.44/bin/apache-tomcat-8.0.44.tar.gz
tar -zxf apache-tomcat-8.0.44.tar.gz -C /docker_tmp
mv /docker_tmp/apache-tomcat-8.0.44/ /docker_tmp/tomcat
cd /docker_tmp
cat >Dockerfile <<HERE
FROM ubuntu:14.04
MAINTAINER shuiguang "shuiguang@github.com"
LABEL Description="Apache tomcat" Vendor="Jlive" Version="8"
ADD . /opt
# Define ENV
ENV LANG=C.UTF-8
ENV CATALINA_HOME=/opt/tomcat
ENV PATH=\$CATALINA_HOME/bin:\$PATH
# Update
RUN echo "nameserver 114.114.114.114" > /etc/resolv.conf
RUN echo "deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse" > /etc/apt/sources.list
RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y install openssh-server
RUN sed -ri 's/session    required     pam_loginuid.so/#session    required     pam_loginuid.so/g' /etc/pam.d/sshd
RUN mkdir -p /root/.ssh/
RUN chmod 755 /opt/run.sh
RUN chmod 755 /opt/auto_deploy.sh
# Exporse ports
EXPOSE 8080
EXPOSE 22 
# Volume
VOLUME ["\$CATALINA_HOME/conf","\$CATALINA_HOME/logs","\$CATALINA_HOME/webapps","\$CATALINA_HOME/work"]
# Define entrypoint
ENTRYPOINT ["/opt/run.sh"]
HERE
docker build -t tomcat8 .

执行sh tomcat-install.sh结果如下

[root@localhost sshd_jenkins]# sh tomcat-install.sh
Sending build context to Docker daemon  13.93MB
Step 1/26 : FROM ubuntu:14.04
 ---> 4a2820e686c4
Step 2/26 : MAINTAINER shuiguang "shuiguang@github.com"
 ---> Using cache
 ---> 82505cb4edaa
Step 3/26 : LABEL Description "Apache tomcat" Vendor "Jlive" Version "8"
 ---> Using cache
 ---> fd90f571d730
Step 4/26 : ADD . /opt
 ---> 64dc6685d752
Removing intermediate container 7c223b138f46
Step 5/26 : ENV LANG C.UTF-8
 ---> Running in 63cc6850fd33
 ---> 0659c2cdf19d
Removing intermediate container 63cc6850fd33
Step 6/26 : ENV CATALINA_HOME /opt/tomcat
 ---> Running in 63a6be3bba14
 ---> 7f1e56ab7831
Removing intermediate container 63a6be3bba14
Step 7/26 : ENV PATH $CATALINA_HOME/bin:$PATH
 ---> Running in ca02ee7b0c63
 ---> c160c960489c
Removing intermediate container ca02ee7b0c63
Step 8/26 : RUN echo "nameserver 114.114.114.114" > /etc/resolv.conf
 ---> Running in 3aa66e7c556a
 ---> 69554f093da3
Removing intermediate container 3aa66e7c556a
Step 9/26 : RUN echo "deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse" > /etc/apt/sources.list
 ---> Running in cc94fa1679a7
 ---> c1006346e7e9
Removing intermediate container cc94fa1679a7
Step 10/26 : RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse" >> /etc/apt/sources.list
 ---> Running in 5a62bbbfdef1
 ---> 2ea58be33bce
Removing intermediate container 5a62bbbfdef1
Step 11/26 : RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse" >> /etc/apt/sources.list
 ---> Running in 81a6b6cf9be3
 ---> c4c6661cfaa6
Removing intermediate container 81a6b6cf9be3
Step 12/26 : RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse" >> /etc/apt/sources.list
 ---> Running in 43125ba59a58
 ---> b67b78f4088e
Removing intermediate container 43125ba59a58
Step 13/26 : RUN echo "deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse" >> /etc/apt/sources.list
 ---> Running in ce0a038c7c69
 ---> 87483d75cd9c
Removing intermediate container ce0a038c7c69
Step 14/26 : RUN echo "deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse" >> /etc/apt/sources.list
 ---> Running in 58b3d757af89
 ---> ace2932639d7
Removing intermediate container 58b3d757af89
Step 15/26 : RUN echo "deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse" >> /etc/apt/sources.list
 ---> Running in 347f0d139744
 ---> 45e72cf1f7c4
Removing intermediate container 347f0d139744
Step 16/26 : RUN echo "deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse" >> /etc/apt/sources.list
 ---> Running in d026cbec07d3
 ---> a6e6faa5207a
Removing intermediate container d026cbec07d3
Step 17/26 : RUN echo "deb-src http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse" >> /etc/apt/sources.list
 ---> Running in 839c20528ff0
 ---> f7bc43bb8471
Removing intermediate container 839c20528ff0
Step 18/26 : RUN echo "deb-src http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse" >> /etc/apt/sources.list
 ---> Running in bf2aff224eab
 ---> 926b2cadcafc
Removing intermediate container bf2aff224eab
Step 19/26 : RUN apt-get update
 ---> Running in 1e0756ee8fe0
Ign http://mirrors.163.com trusty InRelease
Get:1 http://mirrors.163.com trusty-security InRelease [65.9 kB]
Get:2 http://mirrors.163.com trusty-updates InRelease [65.9 kB]
Get:3 http://mirrors.163.com trusty-proposed InRelease [65.9 kB]
Get:4 http://mirrors.163.com trusty-backports InRelease [65.9 kB]
Get:5 http://mirrors.163.com trusty Release.gpg [933 B]
Get:6 http://mirrors.163.com trusty-security/main Sources [169 kB]
Get:7 http://mirrors.163.com trusty-security/restricted Sources [5068 B]
Get:8 http://mirrors.163.com trusty-security/universe Sources [71.2 kB]
Get:9 http://mirrors.163.com trusty-security/multiverse Sources [3022 B]
Get:10 http://mirrors.163.com trusty-security/main amd64 Packages [781 kB]
Get:11 http://mirrors.163.com trusty-security/restricted amd64 Packages [17.9 kB]
Get:12 http://mirrors.163.com trusty-security/universe amd64 Packages [221 kB]
Get:13 http://mirrors.163.com trusty-security/multiverse amd64 Packages [4018 B]
Get:14 http://mirrors.163.com trusty-updates/main Sources [495 kB]
Get:15 http://mirrors.163.com trusty-updates/restricted Sources [6470 B]
Get:16 http://mirrors.163.com trusty-updates/universe Sources [234 kB]
Get:17 http://mirrors.163.com trusty-updates/multiverse Sources [7661 B]
Get:18 http://mirrors.163.com trusty-updates/main amd64 Packages [1245 kB]
Get:19 http://mirrors.163.com trusty-updates/restricted amd64 Packages [21.2 kB]
Get:20 http://mirrors.163.com trusty-updates/universe amd64 Packages [540 kB]
Get:21 http://mirrors.163.com trusty-updates/multiverse amd64 Packages [15.6 kB]
Get:22 http://mirrors.163.com trusty-proposed/main Sources [137 kB]
Get:23 http://mirrors.163.com trusty-proposed/restricted Sources [703 B]
Get:24 http://mirrors.163.com trusty-proposed/universe Sources [19.5 kB]
Get:25 http://mirrors.163.com trusty-proposed/multiverse Sources [40 B]
Get:26 http://mirrors.163.com trusty-proposed/main amd64 Packages [117 kB]
Get:27 http://mirrors.163.com trusty-proposed/restricted amd64 Packages [685 B]
Get:28 http://mirrors.163.com trusty-proposed/universe amd64 Packages [13.1 kB]
Get:29 http://mirrors.163.com trusty-proposed/multiverse amd64 Packages [40 B]
Get:30 http://mirrors.163.com trusty Release [58.5 kB]
Get:31 http://mirrors.163.com trusty-backports/main Sources [10.4 kB]
Get:32 http://mirrors.163.com trusty-backports/restricted Sources [40 B]
Get:33 http://mirrors.163.com trusty-backports/universe Sources [41.2 kB]
Get:34 http://mirrors.163.com trusty-backports/multiverse Sources [1751 B]
Get:35 http://mirrors.163.com trusty-backports/main amd64 Packages [14.8 kB]
Get:36 http://mirrors.163.com trusty-backports/restricted amd64 Packages [40 B]
Get:37 http://mirrors.163.com trusty-backports/universe amd64 Packages [52.6 kB]
Get:38 http://mirrors.163.com trusty-backports/multiverse amd64 Packages [1396 B]
Get:39 http://mirrors.163.com trusty/main Sources [1335 kB]
Get:40 http://mirrors.163.com trusty/restricted Sources [5335 B]
Get:41 http://mirrors.163.com trusty/universe Sources [7926 kB]
Get:42 http://mirrors.163.com trusty/multiverse Sources [211 kB]
Get:43 http://mirrors.163.com trusty/main amd64 Packages [1743 kB]
Get:44 http://mirrors.163.com trusty/restricted amd64 Packages [16.0 kB]
Get:45 http://mirrors.163.com trusty/universe amd64 Packages [7589 kB]
Get:46 http://mirrors.163.com trusty/multiverse amd64 Packages [169 kB]
Fetched 23.6 MB in 1min 7s (350 kB/s)
Reading package lists...
 ---> 4213082d490a
Removing intermediate container 1e0756ee8fe0
Step 20/26 : RUN apt-get -y install openssh-server
 ---> Running in 61ba0333e467
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  ca-certificates krb5-locales libck-connector0 libedit2 libgssapi-krb5-2
  libidn11 libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib libwrap0 libx11-6
  libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 ncurses-term
  openssh-client openssh-sftp-server openssl python python-chardet
  python-minimal python-requests python-six python-urllib3 python2.7
  python2.7-minimal ssh-import-id tcpd wget xauth
Suggested packages:
  krb5-doc krb5-user ssh-askpass libpam-ssh keychain monkeysphere rssh
  molly-guard ufw python-doc python-tk python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
  ca-certificates krb5-locales libck-connector0 libedit2 libgssapi-krb5-2
  libidn11 libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib libwrap0 libx11-6
  libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 ncurses-term
  openssh-client openssh-server openssh-sftp-server openssl python
  python-chardet python-minimal python-requests python-six python-urllib3
  python2.7 python2.7-minimal ssh-import-id tcpd wget xauth
0 upgraded, 38 newly installed, 0 to remove and 4 not upgraded.
Need to get 7615 kB of archives.
After this operation, 35.3 MB of additional disk space will be used.
Get:1 http://mirrors.163.com/ubuntu/ trusty/main libedit2 amd64 3.1-20130712-2 [86.7 kB]
Get:2 http://mirrors.163.com/ubuntu/ trusty-updates/main libkrb5support0 amd64 1.12+dfsg-2ubuntu5.3 [30.7 kB]
Get:3 http://mirrors.163.com/ubuntu/ trusty-updates/main libk5crypto3 amd64 1.12+dfsg-2ubuntu5.3 [79.6 kB]
Get:4 http://mirrors.163.com/ubuntu/ trusty/main libkeyutils1 amd64 1.5.6-1 [7318 B]
Get:5 http://mirrors.163.com/ubuntu/ trusty-updates/main libkrb5-3 amd64 1.12+dfsg-2ubuntu5.3 [262 kB]
Get:6 http://mirrors.163.com/ubuntu/ trusty-updates/main libgssapi-krb5-2 amd64 1.12+dfsg-2ubuntu5.3 [114 kB]
Get:7 http://mirrors.163.com/ubuntu/ trusty-security/main libidn11 amd64 1.28-1ubuntu2.1 [94.4 kB]
Get:8 http://mirrors.163.com/ubuntu/ trusty/main libxau6 amd64 1:1.0.8-1 [8376 B]
Get:9 http://mirrors.163.com/ubuntu/ trusty/main libxdmcp6 amd64 1:1.1.1-1 [12.8 kB]
Get:10 http://mirrors.163.com/ubuntu/ trusty/main libxcb1 amd64 1.10-2ubuntu1 [38.0 kB]
Get:11 http://mirrors.163.com/ubuntu/ trusty/main libx11-data all 2:1.6.2-1ubuntu2 [111 kB]
Get:12 http://mirrors.163.com/ubuntu/ trusty/main libx11-6 amd64 2:1.6.2-1ubuntu2 [560 kB]
Get:13 http://mirrors.163.com/ubuntu/ trusty-security/main libxext6 amd64 2:1.3.2-1ubuntu0.0.14.04.1 [28.8 kB]
Get:14 http://mirrors.163.com/ubuntu/ trusty/main libxmuu1 amd64 2:1.1.1-1 [11.0 kB]
Get:15 http://mirrors.163.com/ubuntu/ trusty/main libck-connector0 amd64 0.4.5-3.1ubuntu2 [10.5 kB]
Get:16 http://mirrors.163.com/ubuntu/ trusty/main libwrap0 amd64 7.6.q-25 [46.2 kB]
Get:17 http://mirrors.163.com/ubuntu/ trusty-security/main libpython2.7-minimal amd64 2.7.6-8ubuntu0.3 [307 kB]
Get:18 http://mirrors.163.com/ubuntu/ trusty-security/main python2.7-minimal amd64 2.7.6-8ubuntu0.3 [1187 kB]
Get:19 http://mirrors.163.com/ubuntu/ trusty-security/main openssl amd64 1.0.1f-1ubuntu2.22 [489 kB]
Get:20 http://mirrors.163.com/ubuntu/ trusty-security/main ca-certificates all 20160104ubuntu0.14.04.1 [190 kB]
Get:21 http://mirrors.163.com/ubuntu/ trusty-updates/main krb5-locales all 1.12+dfsg-2ubuntu5.3 [13.5 kB]
Get:22 http://mirrors.163.com/ubuntu/ trusty-security/main openssh-client amd64 1:6.6p1-2ubuntu2.8 [564 kB]
Get:23 http://mirrors.163.com/ubuntu/ trusty-security/main wget amd64 1.15-1ubuntu1.14.04.2 [271 kB]
Get:24 http://mirrors.163.com/ubuntu/ trusty/main xauth amd64 1:1.0.7-1ubuntu1 [25.8 kB]
Get:25 http://mirrors.163.com/ubuntu/ trusty-security/main libpython2.7-stdlib amd64 2.7.6-8ubuntu0.3 [1873 kB]
Get:26 http://mirrors.163.com/ubuntu/ trusty/main libpython-stdlib amd64 2.7.5-5ubuntu3 [7012 B]
Get:27 http://mirrors.163.com/ubuntu/ trusty/main ncurses-term all 5.9+20140118-1ubuntu1 [243 kB]
Get:28 http://mirrors.163.com/ubuntu/ trusty-security/main openssh-sftp-server amd64 1:6.6p1-2ubuntu2.8 [34.2 kB]
Get:29 http://mirrors.163.com/ubuntu/ trusty-security/main openssh-server amd64 1:6.6p1-2ubuntu2.8 [322 kB]
Get:30 http://mirrors.163.com/ubuntu/ trusty-security/main python2.7 amd64 2.7.6-8ubuntu0.3 [197 kB]
Get:31 http://mirrors.163.com/ubuntu/ trusty/main python-minimal amd64 2.7.5-5ubuntu3 [27.5 kB]
Get:32 http://mirrors.163.com/ubuntu/ trusty/main python amd64 2.7.5-5ubuntu3 [134 kB]
Get:33 http://mirrors.163.com/ubuntu/ trusty/main python-chardet all 2.0.1-2build2 [106 kB]
Get:34 http://mirrors.163.com/ubuntu/ trusty-updates/main python-six all 1.5.2-1ubuntu1 [8238 B]
Get:35 http://mirrors.163.com/ubuntu/ trusty-updates/main python-urllib3 all 1.7.1-1ubuntu4 [39.5 kB]
Get:36 http://mirrors.163.com/ubuntu/ trusty-updates/main python-requests all 2.2.1-1ubuntu0.3 [43.1 kB]
Get:37 http://mirrors.163.com/ubuntu/ trusty/main tcpd amd64 7.6.q-25 [23.0 kB]
Get:38 http://mirrors.163.com/ubuntu/ trusty/main ssh-import-id all 3.21-0ubuntu1 [9624 B]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin: 
Fetched 7615 kB in 22s (341 kB/s)
Selecting previously unselected package libedit2:amd64.
(Reading database ... 11569 files and directories currently installed.)
Preparing to unpack .../libedit2_3.1-20130712-2_amd64.deb ...
Unpacking libedit2:amd64 (3.1-20130712-2) ...
Selecting previously unselected package libkrb5support0:amd64.
Preparing to unpack .../libkrb5support0_1.12+dfsg-2ubuntu5.3_amd64.deb ...
Unpacking libkrb5support0:amd64 (1.12+dfsg-2ubuntu5.3) ...
Selecting previously unselected package libk5crypto3:amd64.
Preparing to unpack .../libk5crypto3_1.12+dfsg-2ubuntu5.3_amd64.deb ...
Unpacking libk5crypto3:amd64 (1.12+dfsg-2ubuntu5.3) ...
Selecting previously unselected package libkeyutils1:amd64.
Preparing to unpack .../libkeyutils1_1.5.6-1_amd64.deb ...
Unpacking libkeyutils1:amd64 (1.5.6-1) ...
Selecting previously unselected package libkrb5-3:amd64.
Preparing to unpack .../libkrb5-3_1.12+dfsg-2ubuntu5.3_amd64.deb ...
Unpacking libkrb5-3:amd64 (1.12+dfsg-2ubuntu5.3) ...
Selecting previously unselected package libgssapi-krb5-2:amd64.
Preparing to unpack .../libgssapi-krb5-2_1.12+dfsg-2ubuntu5.3_amd64.deb ...
Unpacking libgssapi-krb5-2:amd64 (1.12+dfsg-2ubuntu5.3) ...
Selecting previously unselected package libidn11:amd64.
Preparing to unpack .../libidn11_1.28-1ubuntu2.1_amd64.deb ...
Unpacking libidn11:amd64 (1.28-1ubuntu2.1) ...
Selecting previously unselected package libxau6:amd64.
Preparing to unpack .../libxau6_1%3a1.0.8-1_amd64.deb ...
Unpacking libxau6:amd64 (1:1.0.8-1) ...
Selecting previously unselected package libxdmcp6:amd64.
Preparing to unpack .../libxdmcp6_1%3a1.1.1-1_amd64.deb ...
Unpacking libxdmcp6:amd64 (1:1.1.1-1) ...
Selecting previously unselected package libxcb1:amd64.
Preparing to unpack .../libxcb1_1.10-2ubuntu1_amd64.deb ...
Unpacking libxcb1:amd64 (1.10-2ubuntu1) ...
Selecting previously unselected package libx11-data.
Preparing to unpack .../libx11-data_2%3a1.6.2-1ubuntu2_all.deb ...
Unpacking libx11-data (2:1.6.2-1ubuntu2) ...
Selecting previously unselected package libx11-6:amd64.
Preparing to unpack .../libx11-6_2%3a1.6.2-1ubuntu2_amd64.deb ...
Unpacking libx11-6:amd64 (2:1.6.2-1ubuntu2) ...
Selecting previously unselected package libxext6:amd64.
Preparing to unpack .../libxext6_2%3a1.3.2-1ubuntu0.0.14.04.1_amd64.deb ...
Unpacking libxext6:amd64 (2:1.3.2-1ubuntu0.0.14.04.1) ...
Selecting previously unselected package libxmuu1:amd64.
Preparing to unpack .../libxmuu1_2%3a1.1.1-1_amd64.deb ...
Unpacking libxmuu1:amd64 (2:1.1.1-1) ...
Selecting previously unselected package libck-connector0:amd64.
Preparing to unpack .../libck-connector0_0.4.5-3.1ubuntu2_amd64.deb ...
Unpacking libck-connector0:amd64 (0.4.5-3.1ubuntu2) ...
Selecting previously unselected package libwrap0:amd64.
Preparing to unpack .../libwrap0_7.6.q-25_amd64.deb ...
Unpacking libwrap0:amd64 (7.6.q-25) ...
Selecting previously unselected package libpython2.7-minimal:amd64.
Preparing to unpack .../libpython2.7-minimal_2.7.6-8ubuntu0.3_amd64.deb ...
Unpacking libpython2.7-minimal:amd64 (2.7.6-8ubuntu0.3) ...
Selecting previously unselected package python2.7-minimal.
Preparing to unpack .../python2.7-minimal_2.7.6-8ubuntu0.3_amd64.deb ...
Unpacking python2.7-minimal (2.7.6-8ubuntu0.3) ...
Selecting previously unselected package openssl.
Preparing to unpack .../openssl_1.0.1f-1ubuntu2.22_amd64.deb ...
Unpacking openssl (1.0.1f-1ubuntu2.22) ...
Selecting previously unselected package ca-certificates.
Preparing to unpack .../ca-certificates_20160104ubuntu0.14.04.1_all.deb ...
Unpacking ca-certificates (20160104ubuntu0.14.04.1) ...
Selecting previously unselected package krb5-locales.
Preparing to unpack .../krb5-locales_1.12+dfsg-2ubuntu5.3_all.deb ...
Unpacking krb5-locales (1.12+dfsg-2ubuntu5.3) ...
Selecting previously unselected package openssh-client.
Preparing to unpack .../openssh-client_1%3a6.6p1-2ubuntu2.8_amd64.deb ...
Unpacking openssh-client (1:6.6p1-2ubuntu2.8) ...
Selecting previously unselected package wget.
Preparing to unpack .../wget_1.15-1ubuntu1.14.04.2_amd64.deb ...
Unpacking wget (1.15-1ubuntu1.14.04.2) ...
Selecting previously unselected package xauth.
Preparing to unpack .../xauth_1%3a1.0.7-1ubuntu1_amd64.deb ...
Unpacking xauth (1:1.0.7-1ubuntu1) ...
Selecting previously unselected package libpython2.7-stdlib:amd64.
Preparing to unpack .../libpython2.7-stdlib_2.7.6-8ubuntu0.3_amd64.deb ...
Unpacking libpython2.7-stdlib:amd64 (2.7.6-8ubuntu0.3) ...
Selecting previously unselected package libpython-stdlib:amd64.
Preparing to unpack .../libpython-stdlib_2.7.5-5ubuntu3_amd64.deb ...
Unpacking libpython-stdlib:amd64 (2.7.5-5ubuntu3) ...
Selecting previously unselected package ncurses-term.
Preparing to unpack .../ncurses-term_5.9+20140118-1ubuntu1_all.deb ...
Unpacking ncurses-term (5.9+20140118-1ubuntu1) ...
Selecting previously unselected package openssh-sftp-server.
Preparing to unpack .../openssh-sftp-server_1%3a6.6p1-2ubuntu2.8_amd64.deb ...
Unpacking openssh-sftp-server (1:6.6p1-2ubuntu2.8) ...
Selecting previously unselected package openssh-server.
Preparing to unpack .../openssh-server_1%3a6.6p1-2ubuntu2.8_amd64.deb ...
Unpacking openssh-server (1:6.6p1-2ubuntu2.8) ...
Selecting previously unselected package python2.7.
Preparing to unpack .../python2.7_2.7.6-8ubuntu0.3_amd64.deb ...
Unpacking python2.7 (2.7.6-8ubuntu0.3) ...
Selecting previously unselected package python-minimal.
Preparing to unpack .../python-minimal_2.7.5-5ubuntu3_amd64.deb ...
Unpacking python-minimal (2.7.5-5ubuntu3) ...
Selecting previously unselected package python.
Preparing to unpack .../python_2.7.5-5ubuntu3_amd64.deb ...
Unpacking python (2.7.5-5ubuntu3) ...
Selecting previously unselected package python-chardet.
Preparing to unpack .../python-chardet_2.0.1-2build2_all.deb ...
Unpacking python-chardet (2.0.1-2build2) ...
Selecting previously unselected package python-six.
Preparing to unpack .../python-six_1.5.2-1ubuntu1_all.deb ...
Unpacking python-six (1.5.2-1ubuntu1) ...
Selecting previously unselected package python-urllib3.
Preparing to unpack .../python-urllib3_1.7.1-1ubuntu4_all.deb ...
Unpacking python-urllib3 (1.7.1-1ubuntu4) ...
Selecting previously unselected package python-requests.
Preparing to unpack .../python-requests_2.2.1-1ubuntu0.3_all.deb ...
Unpacking python-requests (2.2.1-1ubuntu0.3) ...
Selecting previously unselected package tcpd.
Preparing to unpack .../tcpd_7.6.q-25_amd64.deb ...
Unpacking tcpd (7.6.q-25) ...
Selecting previously unselected package ssh-import-id.
Preparing to unpack .../ssh-import-id_3.21-0ubuntu1_all.deb ...
Unpacking ssh-import-id (3.21-0ubuntu1) ...
Processing triggers for ureadahead (0.100.0-16) ...
Processing triggers for mime-support (3.54ubuntu1.1) ...
Setting up libedit2:amd64 (3.1-20130712-2) ...
Setting up libkrb5support0:amd64 (1.12+dfsg-2ubuntu5.3) ...
Setting up libk5crypto3:amd64 (1.12+dfsg-2ubuntu5.3) ...
Setting up libkeyutils1:amd64 (1.5.6-1) ...
Setting up libkrb5-3:amd64 (1.12+dfsg-2ubuntu5.3) ...
Setting up libgssapi-krb5-2:amd64 (1.12+dfsg-2ubuntu5.3) ...
Setting up libidn11:amd64 (1.28-1ubuntu2.1) ...
Setting up libxau6:amd64 (1:1.0.8-1) ...
Setting up libxdmcp6:amd64 (1:1.1.1-1) ...
Setting up libxcb1:amd64 (1.10-2ubuntu1) ...
Setting up libx11-data (2:1.6.2-1ubuntu2) ...
Setting up libx11-6:amd64 (2:1.6.2-1ubuntu2) ...
Setting up libxext6:amd64 (2:1.3.2-1ubuntu0.0.14.04.1) ...
Setting up libxmuu1:amd64 (2:1.1.1-1) ...
Setting up libck-connector0:amd64 (0.4.5-3.1ubuntu2) ...
Setting up libwrap0:amd64 (7.6.q-25) ...
Setting up libpython2.7-minimal:amd64 (2.7.6-8ubuntu0.3) ...
Setting up python2.7-minimal (2.7.6-8ubuntu0.3) ...
Linking and byte-compiling packages for runtime python2.7...
Setting up openssl (1.0.1f-1ubuntu2.22) ...
Setting up ca-certificates (20160104ubuntu0.14.04.1) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
Setting up krb5-locales (1.12+dfsg-2ubuntu5.3) ...
Setting up openssh-client (1:6.6p1-2ubuntu2.8) ...
Setting up wget (1.15-1ubuntu1.14.04.2) ...
Setting up xauth (1:1.0.7-1ubuntu1) ...
Setting up libpython2.7-stdlib:amd64 (2.7.6-8ubuntu0.3) ...
Setting up libpython-stdlib:amd64 (2.7.5-5ubuntu3) ...
Setting up ncurses-term (5.9+20140118-1ubuntu1) ...
Setting up openssh-sftp-server (1:6.6p1-2ubuntu2.8) ...
Setting up openssh-server (1:6.6p1-2ubuntu2.8) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
Creating SSH2 ECDSA key; this may take some time ...
Creating SSH2 ED25519 key; this may take some time ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up python2.7 (2.7.6-8ubuntu0.3) ...
Setting up python-minimal (2.7.5-5ubuntu3) ...
Setting up python (2.7.5-5ubuntu3) ...
Setting up python-chardet (2.0.1-2build2) ...
Setting up python-six (1.5.2-1ubuntu1) ...
Setting up python-urllib3 (1.7.1-1ubuntu4) ...
Setting up python-requests (2.2.1-1ubuntu0.3) ...
Setting up tcpd (7.6.q-25) ...
Setting up ssh-import-id (3.21-0ubuntu1) ...
Processing triggers for libc-bin (2.19-0ubuntu6.13) ...
Processing triggers for ca-certificates (20160104ubuntu0.14.04.1) ...
Updating certificates in /etc/ssl/certs... 173 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.
Processing triggers for ureadahead (0.100.0-16) ...
 ---> 8bafeb68e501
Removing intermediate container 61ba0333e467
Step 21/26 : RUN mkdir -p /root/.ssh/
 ---> Running in f2ee44faa65c
 ---> 99af6d9b5888
Removing intermediate container f2ee44faa65c
Step 22/26 : RUN chmod 755 /opt/run.sh
 ---> Running in 6b4302fc36bc
 ---> ae1559041209
Removing intermediate container 6b4302fc36bc
Step 23/26 : EXPOSE 8080
 ---> Running in d25d0ff2bfae
 ---> 0db9bfeb4bcf
Removing intermediate container d25d0ff2bfae
Step 24/26 : EXPOSE 22
 ---> Running in 26f2adf7ae2e
 ---> 0df4912b6844
Removing intermediate container 26f2adf7ae2e
Step 25/26 : VOLUME $CATALINA_HOME/conf $CATALINA_HOME/logs $CATALINA_HOME/webapps $CATALINA_HOME/work
 ---> Running in 9f17dca330fa
 ---> 74f750a18995
Removing intermediate container 9f17dca330fa
Step 26/26 : ENTRYPOINT /opt/run.sh
 ---> Running in 63f68082d99c
 ---> 07f22f321fe5
Removing intermediate container 63f68082d99c
Successfully built 07f22f321fe5
Successfully tagged tomcat8:latest

3、运行tomcat容器,并将tomcat_jenkins的公钥设置到tomcat的/root/.ssh/authorized_keys添加可信认证。

docker run -tid --restart=always \
    -e JAVA_HOME=/opt/jdk \
    -e JAVA_OPTS="-Xms64m -Xmx128m -XX:MaxPermSize=16m" \
    -p 8080:8080 \
    -p 22:22 \
    --memory-swappiness=1 \
    --ulimit nofile=20480:40960 \
    --volumes-from jdk1.8 \
    --volumes-from shell \
    -v /opt/tomcat/conf:/opt/tomcat/conf \
    -v /opt/tomcat/logs:/opt/tomcat/logs \
    --name tomcat_container \
    tomcat8

进入jenkins_container,查看公钥内容

root@1cf585b58399:/# cat /root/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC69dZaEqPGayGMdEy/AunL11TzpaVmHyygqmaynnaZgW+I1/VgZzW6QZxqiFXZYN5KpXj40y0G+2jW+AvwwV6id6UOGVzbiXGHMEUS0MuH1PTrGl8gq2TaXchFoN2PfTrW8vgSGFph9ZP8qm1X1TOGkGQRV7BrhNppj+t6jU5HuAGv5JaEP3H+io0pazQ8W1V9jLGB8MI0Vt5FI4VENji6LLvNZdBbTNgAcP3APlpanJEbnDJt4Nbv3FzwCKWBCP+SfQNCrUJcmPFUEfuryFvT/YDZhdmj3JeC619qJjoM/s0IH2XplgSUH3RNyIiWTGYUfOED9gvaAhiJJFRzMRTN root@431f134c09ff
root@1cf585b58399:/#

进入tomcat_container,添加可信认证

echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC69dZaEqPGayGMdEy/AunL11TzpaVmHyygqmaynnaZgW+I1/VgZzW6QZxqiFXZYN5KpXj40y0G+2jW+AvwwV6id6UOGVzbiXGHMEUS0MuH1PTrGl8gq2TaXchFoN2PfTrW8vgSGFph9ZP8qm1X1TOGkGQRV7BrhNppj+t6jU5HuAGv5JaEP3H+io0pazQ8W1V9jLGB8MI0Vt5FI4VENji6LLvNZdBbTNgAcP3APlpanJEbnDJt4Nbv3FzwCKWBCP+SfQNCrUJcmPFUEfuryFvT/YDZhdmj3JeC619qJjoM/s0IH2XplgSUH3RNyIiWTGYUfOED9gvaAhiJJFRzMRTN root@431f134c09ff" > /root/.ssh/authorized_keys

之前在配置jenkins时用了远程目录,也就是tomcat_container需要存在/home/data/jenkins目录用来临时存放war包。

mkdir -p /home/data/jenkins

这时可以将tomcat_container提交为tomcat8镜像了

docker commit tomcat_container tomcat8

编写tomcat-start.sh一键重置脚本:

#!/bin/bash
docker rm -f tomcat_container
docker run -tid --restart=always \
    -e JAVA_HOME=/opt/jdk \
    -e JAVA_OPTS="-Xms64m -Xmx128m -XX:MaxPermSize=16m" \
    -p 8080:8080 \
    -p 22:22 \
    --memory-swappiness=1 \
    --ulimit nofile=20480:40960 \
    --volumes-from jdk1.8 \
    --volumes-from shell \
    -v /opt/tomcat/conf:/opt/tomcat/conf \
    -v /opt/tomcat/logs:/opt/tomcat/logs \
    --name tomcat_container \
    tomcat8

编写jenkins-start.sh一键重置脚本(该容器关联了tomat_container,必须得tomat_container启动之后执行):

#!/bin/bash
docker rm -f jenkins_container
docker rm -f tomcat_jenkins
docker run -tid --restart=always --name jenkins_container jenkins
docker run -tid --restart=always \
    -e JAVA_HOME=/opt/jdk \
    -e JAVA_OPTS="-Xms64m -Xmx128m -XX:MaxPermSize=16m" \
    -p 8090:8080 \
     --link tomcat_container:tomcat
    --memory-swappiness=1 \
    --ulimit nofile=20480:40960 \
    --volumes-from shell \
    --volumes-from jdk1.8 \
    --volumes-from jenkins_container \
	-v /root/.m2:/root/.m2 \
    -v /opt/tomcat/conf:/opt/tomcat/conf \
    -v /opt/tomcat/logs:/opt/tomcat/logs \
    --name tomcat_jenkins \
    tomcat_jenkins

重新执行jenkins-start.sh之后,可以进jenkins_container通过ssh root@tomcat远程登陆试试

[root@localhost sshd_jenkins]# docker commit tomcat_container tomcat8
sha256:b2031aec3b91a285d4063d38f3473c4579d63cbeb9fda0a3668461e09960d9fc
[root@localhost sshd_jenkins]# sh jenkins-start.sh 
jenkins_container
tomcat_jenkins
66efeb5cf822341b26dba4d176d6780c067788aac8cc8507f9704d869d691c32
d3047ea5e6474e5217c1214c4e1f2934242de5b6cd7440035bd6ef4bee96ef6a
[root@localhost sshd_jenkins]# docker exec -ti tomcat_jenkins /bin/bash
root@d3047ea5e647:/# ssh root@tomcat
The authenticity of host 'tomcat (172.17.0.6)' can't be established.
ECDSA key fingerprint is 3c:f8:5c:0c:b8:31:15:b7:52:a4:2a:7e:2b:56:54:72.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'tomcat,172.17.0.6' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 14.04 LTS (GNU/Linux 4.4.0-79-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@4f7250f0fe6c:~#

登陆成功才保证jenkins的远程发布生效,接着尝试在jenkins界面通过构建的方式测试是否通过。


参考脚本:

run.sh

#!/bin/bash
mkdir -p /var/run/sshd
source /etc/profile
/usr/sbin/sshd -D


auto_deploy.sh(发布war包到tomcat)

#!/bin/bash
#Time
log_time=`date +[%Y-%m-%d]%H:%M:%S`
#project
pname=$1

###manual_properties###
tomcat_basehome=/opt/tomcat
tomcat_port=8080
shell_environment=/bin/bash
war_Dir=/home/data/jenkins/${pname}/target
war_Name=ucenter.war
###manual_properties###

#update server environment
echo "**********************************  ${log_time} *************************************"
echo "updating server  environment start"
export JAVA_HOME=/opt/jdk
export JRE_HOME=/opt/jdk/jre
export PATH=$JAVA_HOME/bin:$PATH 
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar/
export CATALINA_2_HOME=/opt/tomcat
export CATALINA_2_BASE=/opt/tomcat
export TOMCAT_2_HOME=/opt/tomcat
sleep 3
echo "updating server  environment  end"

#build check funcation
echo "check tomcat status..."
check_tomcat_status(){
      netstat -ant|grep ${tomcat_port} > /dev/null 
      t=$?
       if [ $t -eq 0 ]; then
           echo "tomcat is running....port is ${tomcat_port}"
           echo "shutdown tomcat....."
           echo ">>>>>>>shutdown tomcat begin<<<<<<<<"
            ${shell_environment} ${tomcat_basehome}/bin/shutdown.sh
           echo ">>>>>>>shutdown tomcat end <<<<<<<<"
           sleep 5
       elif [ $t -ne 0 ];then 
             echo "tomcat is poweroff"
              ${shell_environment} ${tomcat_basehome}/bin/shutdown.sh
             sleep 5
       fi
} 

#check tomcat status invoke function
check_tomcat_status


#transfer  application package
deploy_Loaction=${tomcat_basehome}/webapps/

if [ ! -d $war_Dir ];then
    mkdir -p ${war_Dir}
fi

war_Dir_Data=`ls ${war_Dir}`
echo "--------------  begin  transfer  war package to tomcat webapps -------------------"

if [ -z $war_Dir ];then
     echo "Folder ${war_Dir} is empty.please check war package in this folder!"
     exit 1 
else
     echo "Find ${war_Dir} exist war package ${war_Name}"
    # echo "deleteing old  package ${war_Name} in ${war_Dir}"
    # rm ${war_Dir}/${war_Name}
     echo "deleteing old  package ${war_Name} in ${deploy_Loaction}" 
     rm -rf ${deploy_Loaction}${war_Name}
     echo "start  transfer ${war_Name} to ${deploy_Loaction}"
     cp ${war_Dir}/${war_Name}  ${deploy_Loaction}
     sleep 3 
fi
echo "--------------  transfer  war package to tomcat webapps  end -------------------"
#reboot tomcat 
echo " >>>>>>>  rebooting  tomcat begin <<<<<<<<"
${shell_environment} ${tomcat_basehome}/bin/startup.sh
echo " >>>>>>>  rebooting  tomcat end <<<<<<<<"
echo "the log you can read in canalina.out"
echo "************************ deploy war package into container Successlly  **********************************"


本文地址:http://blog.zhengshuiguang.com/linux/docker-tomcat.html

转载随意,但请附上文章地址:-)

标签:docker jenkins tomcat

相关文章

评论已关闭