当前位置: 移动技术网 > 网络运营>服务器>虚拟主机 > 基于alpine用dockerfile创建的tomcat镜像的实现

基于alpine用dockerfile创建的tomcat镜像的实现

2019年04月17日  | 移动技术网网络运营  | 我要评论
1、下载alpine镜像 [root@docker43 ~]# docker pull alpine using default tag: latest

1、下载alpine镜像

[root@docker43 ~]# docker pull alpine
using default tag: latest
trying to pull repository docker.io/library/alpine ...
latest: pulling from docker.io/library/alpine
4fe2ade4980c: pull complete
digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528
status: downloaded newer image for docker.io/alpine:latest
[root@docker43 ~]# docker images
repository tag image id created size
docker.io/alpine latest 196d12cf6ab1 3 weeks ago 4.41 mb 

2、通过dockerfile构建镜像

在其他的资料中很多都是下载包然后在dockerfile 使用copy进行拷贝,这里我就不这样做了,我们分开写(都是通过dockerfile进行构建镜像)

2.1.构建jdk镜像

创建jdk目录

[root@docker43 ~]# cd /opt/
[root@docker43 opt]# mkdir -p alpine_jre && cd alpine_jre && touch dockerfile
[root@docker43 alpine_jre]# ll
总用量 0
-rw-r--r-- 1 root root 0 10月 6 17:39 dockerfile 

编写dockerfile文件

# 基础镜像
from alpine
 
# 作者信息
maintainer jre docker maintainers "1024331014@qq.com"
 
# 修改源
run echo "http://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories && \
  echo "http://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories
 
# 安装需要的软件
run apk update && \
  apk add --no-cache ca-certificates && \
  apk add --no-cache curl bash tree tzdata && \
  cp -rf /usr/share/zoneinfo/asia/shanghai /etc/localtime
 
# 定义环境变量
env path /usr/local/bin:${path}
 
# 安装jre
run apk add --nocache openjdk8-jre-base && \
  rm -rf /var/cache/apk/*
 
run { \
    echo '#!/bin/sh'; \
    echo 'set -e'; \
    echo; \
    echo 'dirname "$(dirname "$(readlink -f "$(which javac || which java)")")"'; \
   } > /usr/local/bin/docker-java-home \
  && chmod +x /usr/local/bin/docker-java-home
 
env java_home /usr/lib/jvm/default-jvm
env path ${path}:${java_home}/bin:${java_home}/jre/bin
env java_version 8u71
env java_alpine_version 8.171.11-r0
run set -x \
  \
  && apk add --no-cache \
  \
  openjdk8-jre="$java_alpine_version" 

创建镜像

[root@docker43 alpine_jre]# docker build -t alpine_jre . 

查看镜像

[root@docker43 alpine_jre]# docker images
repository     tag         image id      created       size
alpine_jre     latest       614bc57ab66e    33 seconds ago   91.1 mb
docker.io/alpine  latest       196d12cf6ab1    3 weeks ago     4.41 mb 

2.2.构建tomcat镜像

创建tomcat目录

[root@docker43 ~]# cd /opt/
[root@docker43 opt]# mkdir -p jre_tomcat && cd jre_tomcat && touch dockerfile
[root@docker43 jre_tomcat]# ll
总用量 0
-rw-r--r-- 1 root root 0 10月 6 17:46 dockerfile 

编写dockerfile(基于上面的alpine_jre镜像)

#基础镜像
from alpine_jre
 
# 作者信息
maintainer tomcat docker maintainers "1024331014@qq.com"
 
# 定义tomcat变量(如果有其他的可以在这里加)
env catalina_home /usr/local/tomcat
env path $catalina_home/bin:$path
run mkdir -p "$catalina_home"
workdir $catalina_home
 
# let "tomcat native" live somewhere isolated
env tomcat_native_libdir $catalina_home/native-jni-lib
env ld_library_path ${ld_library_path:+$ld_library_path:}$tomcat_native_libdir
 
run apk add --no-cache gnupg
 
# see http://archive.apache.org/dist/tomcat/tomcat-$tomcat_major/keys
# see also "update.sh" (https://github.com/docker-library/tomcat/blob/master/update.sh)
env gpg_keys 05ab33110949707c93a279e3d3efe6b686867ba6 07e48665a34dcafae522e5e6266191c37c037d42 47309207d818ffd8dcd3f83f1931d684307a10a5 541fbe7d8f78b25e055ddee13c370389288584e7 61b832ac2f1c5a90f0f9b00a1c506407564c17a3 713da88be50911535fe716f5208b0ab1d63011c7 79f7026c690baa50b92cd8b66a3ad3f4f22c4fed 9ba44c2621385cb966eba586f72c284d731fabee a27677289986db50844682f8acb77fc2e86e29ac a9c5df4d22e99998d9875a5110c01c5a2f6059e7 dcfd35e0bf8ca7344752de8b6fb21e8933c60243 f3a04c595db5b6a5f1eca43e3b7bbb100d811bbe f7da48bb64bcb84ecba7ee6935cd23c10d498e23
run set -ex; \
    for key in $gpg_keys; do \
        gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
    done
 
# 定义tomcat版本
env tomcat_major 8
env tomcat_version 8.0.53
 
# 下载
# https://issues.apache.org/jira/browse/infra-8753?focusedcommentid=14735394#comment-14735394
env tomcat_tgz_url https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-$tomcat_major/v$tomcat_version/bin/apache-tomcat-$tomcat_version.tar.gz
# not all the mirrors actually carry the .asc files :'(
env tomcat_asc_url http://archive.apache.org/dist/tomcat/tomcat-$tomcat_major/v$tomcat_version/bin/apache-tomcat-$tomcat_version.tar.gz.asc
 
# 安装
run set -x \
    \
    && apk add --no-cache --virtual .fetch-deps \
        ca-certificates \
        tar \
        openssl \
    && wget -o tomcat.tar.gz "$tomcat_tgz_url" \
    && wget -o tomcat.tar.gz.asc "$tomcat_asc_url" \
    && gpg --batch --verify tomcat.tar.gz.asc tomcat.tar.gz \
    && tar -xvf tomcat.tar.gz --strip-components=1 \
    && rm bin/*.bat \
    && rm tomcat.tar.gz* \
    \
    && nativebuilddir="$(mktemp -d)" \
    && tar -xvf bin/tomcat-native.tar.gz -c "$nativebuilddir" --strip-components=1 \
    && apk add --no-cache --virtual .native-build-deps \
        apr-dev \
        dpkg-dev dpkg \
        gcc \
        libc-dev \
        make \
        "openjdk${java_version%%[-~bu]*}"="$java_alpine_version" \
        openssl-dev \
    && ( \
        export catalina_home="$pwd" \
        && cd "$nativebuilddir/native" \
        && gnuarch="$(dpkg-architecture --query deb_build_gnu_type)" \
        && ./configure \
            --build="$gnuarch" \
            --libdir="$tomcat_native_libdir" \
            --prefix="$catalina_home" \
            --with-apr="$(which apr-1-config)" \
            --with-java-home="$(docker-java-home)" \
            --with-ssl=yes \
        && make -j$(getconf _nprocessors_onln) \
        && make install \
    ) \
    && rundeps="$( \
        scanelf --needed --nobanner --recursive "$tomcat_native_libdir" \
            | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
            | sort -u \
            | xargs -r apk info --installed \
            | sort -u \
    )" \
    && apk add --virtual .tomcat-native-rundeps $rundeps \
    && apk del .fetch-deps .native-build-deps \
    && rm -rf "$nativebuilddir" \
    && rm bin/tomcat-native.tar.gz
 
# verify tomcat native is working properly
run set -e \
    && nativelines="$(catalina.sh configtest 2>&1)" \
    && nativelines="$(echo "$nativelines" | grep 'apache tomcat native')" \
    && nativelines="$(echo "$nativelines" | sort -u)" \
    && if ! echo "$nativelines" | grep 'info: loaded apr based apache tomcat native library' >&2; then \
        echo >&2 "$nativelines"; \
        exit 1; \
    fi
 
# 开发8080端口
expose 8080
# 执行命令
cmd ["catalina.sh", "run"] 

创建镜像

[root@docker43 jre_tomcat]# docker build -t tomcat:1.0 . 

查看镜像

[root@docker43 jre_tomcat]# docker images
repository     tag         image id      created       size
tomcat       1.0         64c9cec4375d    7 seconds ago    124 mb
alpine_jre     latest       614bc57ab66e    8 minutes ago    91.1 mb
docker.io/alpine  latest       196d12cf6ab1    3 weeks ago     4.41 mb 

2.3.构建tomcat_web镜像

创建tomcat_web目录(包含dockerfile和启动文件)

[root@docker43 ~]# cd /opt/
[root@docker43 opt]# mkdir tomcat_web && cd tomcat_web && touch dockerfile && touch start.sh
[root@docker43 tomcat_web]# ll
总用量 0
-rw-r--r-- 1 root root 0 10月 6 17:53 dockerfile
-rw-r--r-- 1 root root 0 10月 6 17:53 start.sh 

编写start.sh启动脚本

#!/bin/sh
# licensed to the apache software foundation (asf) under one or more
# contributor license agreements. see the notice file distributed with
# this work for additional information regarding copyright ownership.
# the asf licenses this file to you under the apache license, version 2.0
# (the "license"); you may not use this file except in compliance with
# the license. you may obtain a copy of the license at
#
#   http://www.apache.org/licenses/license-2.0
#
# unless required by applicable law or agreed to in writing, software
# distributed under the license is distributed on an "as is" basis,
# without warranties or conditions of any kind, either express or implied.
# see the license for the specific language governing permissions and
# limitations under the license.
 
# -----------------------------------------------------------------------------
# control script for the catalina server
#
# environment variable prerequisites
#
#  do not set the variables in this script. instead put them into a script
#  setenv.sh in catalina_base/bin to keep your customizations separate.
#
#  catalina_home  may point at your catalina "build" directory.
#
#  catalina_base  (optional) base directory for resolving dynamic portions
#          of a catalina installation. if not present, resolves to
#          the same directory that catalina_home points to.
#
#  catalina_out  (optional) full path to a file where stdout and stderr
#          will be redirected.
#          default is $catalina_base/logs/catalina.out
#
#  catalina_opts  (optional) java runtime options used when the "start",
#          "run" or "debug" command is executed.
#          include here and not in java_opts all options, that should
#          only be used by tomcat itself, not by the stop process,
#          the version command etc.
#          examples are heap size, gc logging, jmx ports etc.
#
#  catalina_tmpdir (optional) directory path location of temporary directory
#          the jvm should use (java.io.tmpdir). defaults to
#          $catalina_base/temp.
#
#  java_home    must point at your java development kit installation.
#          required to run the with the "debug" argument.
#
#  jre_home    must point at your java runtime installation.
#          defaults to java_home if empty. if jre_home and java_home
#          are both set, jre_home is used.
#
#  java_opts    (optional) java runtime options used when any command
#          is executed.
#          include here and not in catalina_opts all options, that
#          should be used by tomcat and also by the stop process,
#          the version command etc.
#          most options should go into catalina_opts.
#
#  java_endorsed_dirs (optional) lists of of colon separated directories
#          containing some jars in order to allow replacement of apis
#          created outside of the jcp (i.e. dom and sax from w3c).
#          it can also be used to update the xml parser implementation.
#          defaults to $catalina_home/endorsed.
#
#  jpda_transport (optional) jpda transport used when the "jpda start"
#          command is executed. the default is "dt_socket".
#
#  jpda_address  (optional) java runtime options used when the "jpda start"
#          command is executed. the default is localhost:8000.
#
#  jpda_suspend  (optional) java runtime options used when the "jpda start"
#          command is executed. specifies whether jvm should suspend
#          execution immediately after startup. default is "n".
#
#  jpda_opts    (optional) java runtime options used when the "jpda start"
#          command is executed. if used, jpda_transport, jpda_address,
#          and jpda_suspend are ignored. thus, all required jpda
#          options must be specified. the default is:
#
#          -agentlib:jdwp=transport=$jpda_transport,
#            address=$jpda_address,server=y,suspend=$jpda_suspend
#
#  jsse_opts    (optional) java runtime options used to control the tls
#          implementation when jsse is used. default is:
#          "-djdk.tls.ephemeraldhkeysize=2048"
#
#  catalina_pid  (optional) path of the file which should contains the pid
#          of the catalina startup java process, when start (fork) is
#          used
#
#  logging_config (optional) override tomcat's logging config file
#          example (all one line)
#          logging_config="-djava.util.logging.config.file=$catalina_base/conf/logging.properties"
#
#  logging_manager (optional) override tomcat's logging manager
#          example (all one line)
#          logging_manager="-djava.util.logging.manager=org.apache.juli.classloaderlogmanager"
#
#  use_nohup    (optional) if set to the string true the start command will
#          use nohup so that the tomcat process will ignore any hangup
#          signals. default is "false" unless running on hp-ux in which
#          case the default is "true"
# -----------------------------------------------------------------------------
 
# os specific support. $var _must_ be set to either true or false.
cygwin=false
darwin=false
os400=false
hpux=false
case "`uname`" in
cygwin*) cygwin=true;;
darwin*) darwin=true;;
os400*) os400=true;;
hp-ux*) hpux=true;;
esac
 
# resolve links - $0 may be a softlink
prg="$0"
 
while [ -h "$prg" ]; do
 ls=`ls -ld "$prg"`
 link=`expr "$ls" : '.*-> \(.*\)$'`
 if expr "$link" : '/.*' > /dev/null; then
  prg="$link"
 else
  prg=`dirname "$prg"`/"$link"
 fi
done
 
# get standard environment variables
prgdir=`dirname "$prg"`
 
# only set catalina_home if not already set
[ -z "$catalina_home" ] && catalina_home=`cd "$prgdir/.." >/dev/null; pwd`
 
# copy catalina_base from catalina_home if not already set
[ -z "$catalina_base" ] && catalina_base="$catalina_home"
 
# ensure that any user defined classpath variables are not used on startup,
# but allow them to be specified in setenv.sh, in rare case when it is needed.
classpath=
 
if [ -r "$catalina_base/bin/setenv.sh" ]; then
 . "$catalina_base/bin/setenv.sh"
elif [ -r "$catalina_home/bin/setenv.sh" ]; then
 . "$catalina_home/bin/setenv.sh"
fi
 
# for cygwin, ensure paths are in unix format before anything is touched
if $cygwin; then
 [ -n "$java_home" ] && java_home=`cygpath --unix "$java_home"`
 [ -n "$jre_home" ] && jre_home=`cygpath --unix "$jre_home"`
 [ -n "$catalina_home" ] && catalina_home=`cygpath --unix "$catalina_home"`
 [ -n "$catalina_base" ] && catalina_base=`cygpath --unix "$catalina_base"`
 [ -n "$classpath" ] && classpath=`cygpath --path --unix "$classpath"`
fi
 
# ensure that neither catalina_home nor catalina_base contains a colon
# as this is used as the separator in the classpath and java provides no
# mechanism for escaping if the same character appears in the path.
case $catalina_home in
 *:*) echo "using catalina_home:  $catalina_home";
    echo "unable to start as catalina_home contains a colon (:) character";
    exit 1;
esac
case $catalina_base in
 *:*) echo "using catalina_base:  $catalina_base";
    echo "unable to start as catalina_base contains a colon (:) character";
    exit 1;
esac
 
# for os400
if $os400; then
 # set job priority to standard for interactive (interactive - 6) by using
 # the interactive priority - 6, the helper threads that respond to requests
 # will be running at the same priority as interactive jobs.
 command='chgjob job('$jobname') runpty(6)'
 system $command
 
 # enable multi threading
 export qibm_multi_threaded=y
fi
 
# get standard java environment variables
if $os400; then
 # -r will only work on the os400 if the files are:
 # 1. owned by the user
 # 2. owned by the primary group of the user
 # this will not work if the user belongs in secondary groups
 . "$catalina_home"/bin/setclasspath.sh
else
 if [ -r "$catalina_home"/bin/setclasspath.sh ]; then
  . "$catalina_home"/bin/setclasspath.sh
 else
  echo "cannot find $catalina_home/bin/setclasspath.sh"
  echo "this file is needed to run this program"
  exit 1
 fi
fi
 
# add on extra jar files to classpath
if [ ! -z "$classpath" ] ; then
 classpath="$classpath":
fi
classpath="$classpath""$catalina_home"/bin/bootstrap.jar
 
if [ -z "$catalina_out" ] ; then
 catalina_out="$catalina_base"/logs/catalina.out
fi
 
if [ -z "$catalina_tmpdir" ] ; then
 # define the java.io.tmpdir to use for catalina
 catalina_tmpdir="$catalina_base"/temp
fi
 
# add tomcat-juli.jar to classpath
# tomcat-juli.jar can be over-ridden per instance
if [ -r "$catalina_base/bin/tomcat-juli.jar" ] ; then
 classpath=$classpath:$catalina_base/bin/tomcat-juli.jar
else
 classpath=$classpath:$catalina_home/bin/tomcat-juli.jar
fi
 
# bugzilla 37848: when no tty is available, don't output to console
have_tty=0
if [ "`tty`" != "not a tty" ]; then
  have_tty=1
fi
 
# for cygwin, switch paths to windows format before running java
if $cygwin; then
 java_home=`cygpath --absolute --windows "$java_home"`
 jre_home=`cygpath --absolute --windows "$jre_home"`
 catalina_home=`cygpath --absolute --windows "$catalina_home"`
 catalina_base=`cygpath --absolute --windows "$catalina_base"`
 catalina_tmpdir=`cygpath --absolute --windows "$catalina_tmpdir"`
 classpath=`cygpath --path --windows "$classpath"`
 java_endorsed_dirs=`cygpath --path --windows "$java_endorsed_dirs"`
fi
 
if [ -z "$jsse_opts" ] ; then
 jsse_opts="-djdk.tls.ephemeraldhkeysize=2048"
fi
java_opts="$java_opts $jsse_opts"
 
# register custom url handlers
# do this here so custom url handles (specifically 'war:...') can be used in the security policy
java_opts="$java_opts -djava.protocol.handler.pkgs=org.apache.catalina.webresources"
 
# set juli logmanager config file if it is present and an override has not been issued
if [ -z "$logging_config" ]; then
 if [ -r "$catalina_base"/conf/logging.properties ]; then
  logging_config="-djava.util.logging.config.file=$catalina_base/conf/logging.properties"
 else
  # bugzilla 45585
  logging_config="-dnop"
 fi
fi
 
if [ -z "$logging_manager" ]; then
 logging_manager="-djava.util.logging.manager=org.apache.juli.classloaderlogmanager"
fi
 
# uncomment the following line to make the umask available when using the
# org.apache.catalina.security.securitylistener
#java_opts="$java_opts -dorg.apache.catalina.security.securitylistener.umask=`umask`"
 
if [ -z "$use_nohup" ]; then
  if $hpux; then
    use_nohup="true"
  else
    use_nohup="false"
  fi
fi
unset _nohup
if [ "$use_nohup" = "true" ]; then
  _nohup=nohup
fi
 
# ----- execute the requested command -----------------------------------------
 
# bugzilla 37848: only output this if we have a tty
if [ $have_tty -eq 1 ]; then
 echo "using catalina_base:  $catalina_base"
 echo "using catalina_home:  $catalina_home"
 echo "using catalina_tmpdir: $catalina_tmpdir"
 if [ "$1" = "debug" ] ; then
  echo "using java_home:    $java_home"
 else
  echo "using jre_home:    $jre_home"
 fi
 echo "using classpath:    $classpath"
 if [ ! -z "$catalina_pid" ]; then
  echo "using catalina_pid:  $catalina_pid"
 fi
fi
 
if [ "$1" = "jpda" ] ; then
 if [ -z "$jpda_transport" ]; then
  jpda_transport="dt_socket"
 fi
 if [ -z "$jpda_address" ]; then
  jpda_address="localhost:8000"
 fi
 if [ -z "$jpda_suspend" ]; then
  jpda_suspend="n"
 fi
 if [ -z "$jpda_opts" ]; then
  jpda_opts="-agentlib:jdwp=transport=$jpda_transport,address=$jpda_address,server=y,suspend=$jpda_suspend"
 fi
 catalina_opts="$jpda_opts $catalina_opts"
 shift
fi
 
if [ "$1" = "debug" ] ; then
 if $os400; then
  echo "debug command not available on os400"
  exit 1
 else
  shift
  if [ "$1" = "-security" ] ; then
   if [ $have_tty -eq 1 ]; then
    echo "using security manager"
   fi
   shift
   exec "$_runjdb" "$logging_config" $logging_manager $java_opts $catalina_opts \
    -djava.endorsed.dirs="$java_endorsed_dirs" -classpath "$classpath" \
    -sourcepath "$catalina_home"/../../java \
    -djava.security.manager \
    -djava.security.policy=="$catalina_base"/conf/catalina.policy \
    -dcatalina.base="$catalina_base" \
    -dcatalina.home="$catalina_home" \
    -djava.io.tmpdir="$catalina_tmpdir" \
    org.apache.catalina.startup.bootstrap "$@" start
  else
   exec "$_runjdb" "$logging_config" $logging_manager $java_opts $catalina_opts \
    -djava.endorsed.dirs="$java_endorsed_dirs" -classpath "$classpath" \
    -sourcepath "$catalina_home"/../../java \
    -dcatalina.base="$catalina_base" \
    -dcatalina.home="$catalina_home" \
    -djava.io.tmpdir="$catalina_tmpdir" \
    org.apache.catalina.startup.bootstrap "$@" start
  fi
 fi
 
elif [ "$1" = "run" ]; then
 
 shift
 if [ "$1" = "-security" ] ; then
  if [ $have_tty -eq 1 ]; then
   echo "using security manager"
  fi
  shift
  eval exec "\"$_runjava\"" "\"$logging_config\"" $logging_manager $java_opts $catalina_opts \
   -djava.endorsed.dirs="\"$java_endorsed_dirs\"" -classpath "\"$classpath\"" \
   -djava.security.manager \
   -djava.security.policy=="\"$catalina_base/conf/catalina.policy\"" \
   -dcatalina.base="\"$catalina_base\"" \
   -dcatalina.home="\"$catalina_home\"" \
   -djava.io.tmpdir="\"$catalina_tmpdir\"" \
   org.apache.catalina.startup.bootstrap "$@" start
 else
  eval exec "\"$_runjava\"" "\"$logging_config\"" $logging_manager $java_opts $catalina_opts \
   -djava.endorsed.dirs="\"$java_endorsed_dirs\"" -classpath "\"$classpath\"" \
   -dcatalina.base="\"$catalina_base\"" \
   -dcatalina.home="\"$catalina_home\"" \
   -djava.io.tmpdir="\"$catalina_tmpdir\"" \
   org.apache.catalina.startup.bootstrap "$@" start \
   >> "$catalina_out" 2>&1 
 fi
fi 

编写dockerfile(基于上面的tomcat镜像)

# 基础镜像
from tomcat:1.0
 
# 创建项目目录和日志目录,这个是要在宿主机-v挂载的
run set -x \
  &&mkdir -p /webs/logs \
  \
  &&rm -rf /usr/local/tomcat/logs \
  \
  &&ln -sf /webs/logs /usr/local/tomcat/logs
 
# 将启动文件copy到容器
copy start.sh /usr/local/tomcat/bin/
 
# 给容器的启动脚本权限
run chmod +x /usr/local/tomcat/bin/start.sh
 
# 开放8080端口
expose 8080
 
# 运行tomcat
cmd ["start.sh","run"] 

创建镜像

[root@docker43 tomcat_web]# docker build -t tomcat_web:v1 . 

查看镜像

[root@docker43 tomcat_web]# docker images
repository     tag         image id      created       size
tomcat_web     v1         b3651c50a7b5    26 seconds ago   124 mb
tomcat       1.0         64c9cec4375d    6 minutes ago    124 mb
alpine_jre     latest       614bc57ab66e    14 minutes ago   91.1 mb
docker.io/alpine  latest       196d12cf6ab1    3 weeks ago     4.41 mb 

3、创建容器

创建项目目录

我们是基于tomcat_web镜像进行创建容器的

首先我先在宿主机上创建个项目的目录(server.xml的配置文件拷贝改路径就能用)

[root@docker43 ~]# cd /home/
[root@docker43 home]# mkdir test.tomcat.com && cd test.tomcat.com
[root@docker43 test.tomcat.com]# touch server.xml # 配置文件
[root@docker43 test.tomcat.com]# mkdir logs    # 日志目录
[root@docker43 test.tomcat.com]# mkdir wwwroot   # 项目主目录(要是修改了记得也在server.xml进行修改)
[root@docker43 test.tomcat.com]# ll
总用量 0
drwxr-xr-x 2 root root 6 10月 6 18:03 logs
-rw-r--r-- 1 root root 0 10月 6 18:02 server.xml
drwxr-xr-x 2 root root 6 10月 6 18:03 wwwroot 

server.xml配置文件

  <?xml version='1.0' encoding='utf-8'?>
  <server port="8005" shutdown="shutdown">
  <listener classname="org.apache.catalina.startup.versionloggerlistener" />
  <listener classname="org.apache.catalina.core.aprlifecyclelistener" sslengine="on" />
  <listener classname="org.apache.catalina.core.jrememoryleakpreventionlistener" />
  <listener classname="org.apache.catalina.mbeans.globalresourceslifecyclelistener" />
  <listener classname="org.apache.catalina.core.threadlocalleakpreventionlistener" />
 
  <globalnamingresources>
 
   <resource name="userdatabase" auth="container"
        type="org.apache.catalina.userdatabase"
        description="user database that can be updated and saved"
        factory="org.apache.catalina.users.memoryuserdatabasefactory"
        pathname="conf/tomcat-users.xml" />
  </globalnamingresources>
 
  <service name="catalina">
   <connector port="8080" protocol="org.apache.coyote.http11.http11aprprotocol"
        connectiontimeout="20000"
        maxthreads="1000" 
        minsparethreads="100" 
        redirectport="8443" uriencoding="utf-8"/>
   <connector port="8009" maxthreads="1000" minsparethreads="100" protocol="ajp/1.3" redirectport="8443" />
   <engine name="catalina" defaulthost="localhost">
    <realm classname="org.apache.catalina.realm.lockoutrealm">
    <realm classname="org.apache.catalina.realm.userdatabaserealm" resourcename="userdatabase"/>
    </realm>
 
    <host name="localhost" appbase="/webs/wwwroot" unpackwars="true" autodeploy="true">
     <context path="" docbase="/webs/wwwroot" reloadable="true" />
     <valve classname="org.apache.catalina.valves.accesslogvalve" 
     directory="logs" 
     prefix="localhost__access_log" 
     suffix=".txt" 
     pattern="%{x-real-ip}i %l %u %t "%r" %s %b" />
 
    </host>
 
   </engine>
  </service>
 </server>

编写测试页面

在wwwroot中编写个测试页面

[root@docker43 test.tomcat.com]# cd wwwroot/
[root@docker43 wwwroot]# cat  

成功了

创建并启动容器

复制代码 代码如下:
docker run -tid  --restart=always --name test.tomcat.com  -p 5081:8080 -v /home/test.tomcat.com/:/webs -m 2048m  --memory-swap=2048m  --cpu-shares=256  tomcat_web:v1 start.sh  run -config /webs/server.xml

查看容器

[root@docker43 test.tomcat.com]# docker ps
container id    image        command         created       status       ports          names
ab62045e0688    tomcat_web:v1    "start.sh run -con..."  2 seconds ago    up 1 second     0.0.0.0:5081->8080/tcp  test.tomcat.com 

测试

[root@docker43 test.tomcat.com]# curl 127.0.0.1:5081
成功了
[root@docker43 test.tomcat.com]# 

4、温馨提示

1、各位肯定会问我问什么要做这么多的镜像,我的回答是每个都独立出来 放在之后的私有仓库中,以后可以单独的使用

2、这篇文章的代码都完全的可以直接的复制粘贴使用,要注意的是根据自己的情况来修改dockerfile的变量版本,还有就是项目的路径和创建容器的参数

3、我们在创建第二个项目的使用,完全可以重复的执行创建容器的步骤只是修改下项目目录的名字和容器的名字而已

4、也可以把上面的三个dockerfile文件合并创建镜像,但是不建议这样做

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网