docker学习笔记-3.docker镜像制作
制作jdk1.8的基础镜像
DockerFile文件
设置 java的环境变量
1 #JDK 7 base centos7.5 1804 2 FROM centos-base:v1 3 MAINTAINER hexintong 137841632@qq.com 4 ADD jdk-8u131-linux-x64.tar.gz /usr/local/src/ 5 RUN ln -sv /usr/local/src/jdk1.8.0_131 /usr/local/jdk 6 ADD profile /etc/profile 7 ENV JAVA_HOME /usr/local/jdk 8 ENV JRE_HOME $JAVA_HOME/jre 9 ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/ 10 ENV PATH $PATH:$JAVA_HOME/bin 11 RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" >/etc/timezone
查看 profile 文件
1 # Path manipulation 2 if [ "$EUID" = "0" ]; then 3 pathmunge /usr/sbin 4 pathmunge /usr/local/sbin 5 else 6 pathmunge /usr/local/sbin after 7 pathmunge /usr/sbin after 8 fi 9 10 HOSTNAME=`/usr/bin/hostname 2>/dev/null` 11 HISTSIZE=1000 12 if [ "$HISTCONTROL" = "ignorespace" ] ; then 13 export HISTCONTROL=ignoreboth 14 else 15 export HISTCONTROL=ignoredups 16 fi 17 18 export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL 19 20 # By default, we want umask to get set. This sets it for login shell 21 # Current threshold for system reserved uid/gids is 200 22 # You could check uidgid reservation validity in 23 # /usr/share/doc/setup-*/uidgid file 24 if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then 25 umask 002 26 else 27 umask 022 28 fi 29 30 for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do 31 if [ -r "$i" ]; then 32 if [ "${-#*i}" != "$-" ]; then 33 . "$i" 34 else 35 . "$i" >/dev/null 36 fi 37 fi 38 done 39 40 unset i 41 unset -f pathmunge 42 export JAVA_HOME=/usr/local/jdk 43 export JRE_HOME=$JAVA_HOME/jre 44 export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/ 45 export PATH=$PATH:$JAVA_HOME/bin
[root@localhost jdk1.8]# cat build-commad.sh
1 #!/bin/bash 2 docker build -t jdk1.8:v1 .
制作tomcate 基础镜像
查看Dockerfile 文件
1 #tomcate 8.0.49 2 FROM jdk1.8:v1 3 MAINTAINER hexintong 137841632@qq.com 4 ADD apache-tomcat-8.0.49.tar.gz /apps/ 5 RUN ln -sv /apps/apache-tomcat-8.0.49 /apps/tomcat 6 RUN useradd www -u 2000
制作tomcat业务镜像
查看 Dockerfile 文件
1 #tomcate 8.0.49 2 FROM tomcat-base:v1 3 MAINTAINER hexintong 137841632@qq.com 4 #ADD index.html /apps/tomcat/webapp/ 5 ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh 6 ADD catalina.sh /apps/tomcat/bin/catalina.sh 7 ADD server.xml /apps/tomcat/conf/server.xml 8 RUN mkdir -p /data/webapps/myapp/ 9 ADD myapp/* /data/webapps/myapp/ 10 RUN echo "tomcat appo2" >/data/webapps/index.html 11 RUN chown www.www /apps/ /data/ -R 12 CMD ["/apps/tomcat/bin/run_tomcat.sh"] 13 EXPOSE 8080 8009
查看run_tomcat.sh文件
1 #!/bin/bash 2 su - www -c "/apps/tomcat/bin/catalina.sh start" 3 su - www -c "tail -f /etc/hosts"
查看catalina.sh文件
1 #!/bin/sh 2 3 # Licensed to the Apache Software Foundation (ASF) under one or more 4 # contributor license agreements. See the NOTICE file distributed with 5 # this work for additional information regarding copyright ownership. 6 # The ASF licenses this file to You under the Apache License, Version 2.0 7 # (the "License"); you may not use this file except in compliance with 8 # the License. You may obtain a copy of the License at 9 # 10 # http://www.apache.org/licenses/LICENSE-2.0 11 # 12 # Unless required by applicable law or agreed to in writing, software 13 # distributed under the License is distributed on an "AS IS" BASIS, 14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 # See the License for the specific language governing permissions and 16 # limitations under the License. 17 18 # ----------------------------------------------------------------------------- 19 # Control Script for the CATALINA Server 20 # 21 # Environment Variable Prerequisites 22 # 23 # Do not set the variables in this script. Instead put them into a script 24 # setenv.sh in CATALINA_BASE/bin to keep your customizations separate. 25 # 26 # CATALINA_HOME May point at your Catalina "build" directory. 27 # 28 # CATALINA_BASE (Optional) Base directory for resolving dynamic portions 29 # of a Catalina installation. If not present, resolves to 30 # the same directory that CATALINA_HOME points to. 31 # 32 # CATALINA_OUT (Optional) Full path to a file where stdout and stderr 33 # will be redirected. 34 # Default is $CATALINA_BASE/logs/catalina.out 35 # 36 # CATALINA_OPTS (Optional) Java runtime options used when the "start", 37 # "run" or "debug" command is executed. 38 # Include here and not in JAVA_OPTS all options, that should 39 # only be used by Tomcat itself, not by the stop process, 40 # the version command etc. 41 # Examples are heap size, GC logging, JMX ports etc. 42 # 43 # CATALINA_TMPDIR (Optional) Directory path location of temporary directory 44 # the JVM should use (java.io.tmpdir). Defaults to 45 # $CATALINA_BASE/temp. 46 # 47 # JAVA_HOME Must point at your Java Development Kit installation. 48 # Required to run the with the "debug" argument. 49 # 50 # JRE_HOME Must point at your Java Runtime installation. 51 # Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME 52 # are both set, JRE_HOME is used. 53 # 54 # JAVA_OPTS (Optional) Java runtime options used when any command 55 # is executed. 56 # Include here and not in CATALINA_OPTS all options, that 57 # should be used by Tomcat and also by the stop process, 58 # the version command etc. 59 # Most options should go into CATALINA_OPTS. 60 # 61 # JAVA_ENDORSED_DIRS (Optional) Lists of of colon separated directories 62 # containing some jars in order to allow replacement of APIs 63 # created outside of the JCP (i.e. DOM and SAX from W3C). 64 # It can also be used to update the XML parser implementation. 65 # Note that Java 9 no longer supports this feature. 66 # Defaults to $CATALINA_HOME/endorsed. 67 # 68 # JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start" 69 # command is executed. The default is "dt_socket". 70 # 71 # JPDA_ADDRESS (Optional) Java runtime options used when the "jpda start" 72 # command is executed. The default is localhost:8000. 73 # 74 # JPDA_SUSPEND (Optional) Java runtime options used when the "jpda start" 75 # command is executed. Specifies whether JVM should suspend 76 # execution immediately after startup. Default is "n". 77 # 78 # JPDA_OPTS (Optional) Java runtime options used when the "jpda start" 79 # command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS, 80 # and JPDA_SUSPEND are ignored. Thus, all required jpda 81 # options MUST be specified. The default is: 82 # 83 # -agentlib:jdwp=transport=$JPDA_TRANSPORT, 84 # address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND 85 # 86 # JSSE_OPTS (Optional) Java runtime options used to control the TLS 87 # implementation when JSSE is used. Default is: 88 # "-Djdk.tls.ephemeralDHKeySize=2048" 89 # 90 # CATALINA_PID (Optional) Path of the file which should contains the pid 91 # of the catalina startup java process, when start (fork) is 92 # used 93 # 94 # LOGGING_CONFIG (Optional) Override Tomcat's logging config file 95 # Example (all one line) 96 # LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties" 97 # 98 # LOGGING_MANAGER (Optional) Override Tomcat's logging manager 99 # Example (all one line) 100 # LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" 101 # 102 # USE_NOHUP (Optional) If set to the string true the start command will 103 # use nohup so that the Tomcat process will ignore any hangup 104 # signals. Default is "false" unless running on HP-UX in which 105 # case the default is "true" 106 # ----------------------------------------------------------------------------- 107 JAVA_OPTS="-server -Xms4g -Xmx4g -Xss512k -Xmn1g -XX:CMSInitiatingOccupancyFraction=65 -XX:+UseFastAccessorMethods -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=10 -XX:NewSize=2048M -XX:MaxNewSize=2048M -XX:NewRatio=2 -XX:PermSize=128m -XX:MaxPermSize=512m -XX:CMSFullGCsBeforeCompaction=5 -XX:+ExplicitGCInvokesConcurrent -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods" 108 109 # OS specific support. $var _must_ be set to either true or false. 110 cygwin=false 111 darwin=false 112 os400=false 113 hpux=false 114 case "`uname`" in 115 CYGWIN*) cygwin=true;; 116 Darwin*) darwin=true;; 117 OS400*) os400=true;; 118 HP-UX*) hpux=true;; 119 esac 120 121 # resolve links - $0 may be a softlink 122 PRG="$0" 123 124 while [ -h "$PRG" ]; do 125 ls=`ls -ld "$PRG"` 126 link=`expr "$ls" : '.*-> \(.*\)$'` 127 if expr "$link" : '/.*' > /dev/null; then 128 PRG="$link" 129 else 130 PRG=`dirname "$PRG"`/"$link" 131 fi 132 done 133 134 # Get standard environment variables 135 PRGDIR=`dirname "$PRG"` 136 137 # Only set CATALINA_HOME if not already set 138 [ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd` 139 140 # Copy CATALINA_BASE from CATALINA_HOME if not already set 141 [ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME" 142 143 # Ensure that any user defined CLASSPATH variables are not used on startup, 144 # but allow them to be specified in setenv.sh, in rare case when it is needed. 145 CLASSPATH= 146 147 if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then 148 . "$CATALINA_BASE/bin/setenv.sh" 149 elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then 150 . "$CATALINA_HOME/bin/setenv.sh" 151 fi 152 153 # For Cygwin, ensure paths are in UNIX format before anything is touched 154 if $cygwin; then 155 [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 156 [ -n "$JRE_HOME" ] && JRE_HOME=`cygpath --unix "$JRE_HOME"` 157 [ -n "$CATALINA_HOME" ] && CATALINA_HOME=`cygpath --unix "$CATALINA_HOME"` 158 [ -n "$CATALINA_BASE" ] && CATALINA_BASE=`cygpath --unix "$CATALINA_BASE"` 159 [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 160 fi 161 162 # Ensure that neither CATALINA_HOME nor CATALINA_BASE contains a colon 163 # as this is used as the separator in the classpath and Java provides no 164 # mechanism for escaping if the same character appears in the path. 165 case $CATALINA_HOME in 166 *:*) echo "Using CATALINA_HOME: $CATALINA_HOME"; 167 echo "Unable to start as CATALINA_HOME contains a colon (:) character"; 168 exit 1; 169 esac 170 case $CATALINA_BASE in 171 *:*) echo "Using CATALINA_BASE: $CATALINA_BASE"; 172 echo "Unable to start as CATALINA_BASE contains a colon (:) character"; 173 exit 1; 174 esac 175 176 # For OS400 177 if $os400; then 178 # Set job priority to standard for interactive (interactive - 6) by using 179 # the interactive priority - 6, the helper threads that respond to requests 180 # will be running at the same priority as interactive jobs. 181 COMMAND='chgjob job('$JOBNAME') runpty(6)' 182 system $COMMAND 183 184 # Enable multi threading 185 export QIBM_MULTI_THREADED=Y 186 fi 187 188 # Get standard Java environment variables 189 if $os400; then 190 # -r will Only work on the os400 if the files are: 191 # 1. owned by the user 192 # 2. owned by the PRIMARY group of the user 193 # this will not work if the user belongs in secondary groups 194 . "$CATALINA_HOME"/bin/setclasspath.sh 195 else 196 if [ -r "$CATALINA_HOME"/bin/setclasspath.sh ]; then 197 . "$CATALINA_HOME"/bin/setclasspath.sh 198 else 199 echo "Cannot find $CATALINA_HOME/bin/setclasspath.sh" 200 echo "This file is needed to run this program" 201 exit 1 202 fi 203 fi 204 205 # Add on extra jar files to CLASSPATH 206 if [ ! -z "$CLASSPATH" ] ; then 207 CLASSPATH="$CLASSPATH": 208 fi 209 CLASSPATH="$CLASSPATH""$CATALINA_HOME"/bin/bootstrap.jar 210 211 if [ -z "$CATALINA_OUT" ] ; then 212 CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out 213 fi 214 215 if [ -z "$CATALINA_TMPDIR" ] ; then 216 # Define the java.io.tmpdir to use for Catalina 217 CATALINA_TMPDIR="$CATALINA_BASE"/temp 218 fi 219 220 # Add tomcat-juli.jar to classpath 221 # tomcat-juli.jar can be over-ridden per instance 222 if [ -r "$CATALINA_BASE/bin/tomcat-juli.jar" ] ; then 223 CLASSPATH=$CLASSPATH:$CATALINA_BASE/bin/tomcat-juli.jar 224 else 225 CLASSPATH=$CLASSPATH:$CATALINA_HOME/bin/tomcat-juli.jar 226 fi 227 228 # Bugzilla 37848: When no TTY is available, don't output to console 229 have_tty=0 230 if [ "`tty`" != "not a tty" ]; then 231 have_tty=1 232 fi 233 234 # For Cygwin, switch paths to Windows format before running java 235 if $cygwin; then 236 JAVA_HOME=`cygpath --absolute --windows "$JAVA_HOME"` 237 JRE_HOME=`cygpath --absolute --windows "$JRE_HOME"` 238 CATALINA_HOME=`cygpath --absolute --windows "$CATALINA_HOME"` 239 CATALINA_BASE=`cygpath --absolute --windows "$CATALINA_BASE"` 240 CATALINA_TMPDIR=`cygpath --absolute --windows "$CATALINA_TMPDIR"` 241 CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 242 JAVA_ENDORSED_DIRS=`cygpath --path --windows "$JAVA_ENDORSED_DIRS"` 243 fi 244 245 if [ -z "$JSSE_OPTS" ] ; then 246 JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048" 247 fi 248 JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS" 249 250 # Register custom URL handlers 251 # Do this here so custom URL handles (specifically 'war:...') can be used in the security policy 252 JAVA_OPTS="$JAVA_OPTS -Djava.protocol.handler.pkgs=org.apache.catalina.webresources" 253 254 # Set juli LogManager config file if it is present and an override has not been issued 255 if [ -z "$LOGGING_CONFIG" ]; then 256 if [ -r "$CATALINA_BASE"/conf/logging.properties ]; then 257 LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties" 258 else 259 # Bugzilla 45585 260 LOGGING_CONFIG="-Dnop" 261 fi 262 fi 263 264 if [ -z "$LOGGING_MANAGER" ]; then 265 LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" 266 fi 267 268 # Java 9 no longer supports the java.endorsed.dirs 269 # system property. Only try to use it if 270 # JAVA_ENDORSED_DIRS was explicitly set 271 # or CATALINA_HOME/endorsed exists. 272 ENDORSED_PROP=ignore.endorsed.dirs 273 if [ -n "$JAVA_ENDORSED_DIRS" ]; then 274 ENDORSED_PROP=java.endorsed.dirs 275 fi 276 if [ -d "$CATALINA_HOME/endorsed" ]; then 277 ENDORSED_PROP=java.endorsed.dirs 278 fi 279 280 # Uncomment the following line to make the umask available when using the 281 # org.apache.catalina.security.SecurityListener 282 #JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.security.SecurityListener.UMASK=`umask`" 283 284 if [ -z "$USE_NOHUP" ]; then 285 if $hpux; then 286 USE_NOHUP="true" 287 else 288 USE_NOHUP="false" 289 fi 290 fi 291 unset _NOHUP 292 if [ "$USE_NOHUP" = "true" ]; then 293 _NOHUP=nohup 294 fi 295 296 # Add the JAVA 9 specific start-up parameters required by Tomcat 297 JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.lang=ALL-UNNAMED" 298 JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED" 299 export JDK_JAVA_OPTIONS 300 301 # ----- Execute The Requested Command ----------------------------------------- 302 303 # Bugzilla 37848: only output this if we have a TTY 304 if [ $have_tty -eq 1 ]; then 305 echo "Using CATALINA_BASE: $CATALINA_BASE" 306 echo "Using CATALINA_HOME: $CATALINA_HOME" 307 echo "Using CATALINA_TMPDIR: $CATALINA_TMPDIR" 308 if [ "$1" = "debug" ] ; then 309 echo "Using JAVA_HOME: $JAVA_HOME" 310 else 311 echo "Using JRE_HOME: $JRE_HOME" 312 fi 313 echo "Using CLASSPATH: $CLASSPATH" 314 if [ ! -z "$CATALINA_PID" ]; then 315 echo "Using CATALINA_PID: $CATALINA_PID" 316 fi 317 fi 318 319 if [ "$1" = "jpda" ] ; then 320 if [ -z "$JPDA_TRANSPORT" ]; then 321 JPDA_TRANSPORT="dt_socket" 322 fi 323 if [ -z "$JPDA_ADDRESS" ]; then 324 JPDA_ADDRESS="localhost:8000" 325 fi 326 if [ -z "$JPDA_SUSPEND" ]; then 327 JPDA_SUSPEND="n" 328 fi 329 if [ -z "$JPDA_OPTS" ]; then 330 JPDA_OPTS="-agentlib:jdwp=transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND" 331 fi 332 CATALINA_OPTS="$JPDA_OPTS $CATALINA_OPTS" 333 shift 334 fi 335 336 if [ "$1" = "debug" ] ; then 337 if $os400; then 338 echo "Debug command not available on OS400" 339 exit 1 340 else 341 shift 342 if [ "$1" = "-security" ] ; then 343 if [ $have_tty -eq 1 ]; then 344 echo "Using Security Manager" 345 fi 346 shift 347 exec "$_RUNJDB" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \ 348 -D$ENDORSED_PROP="$JAVA_ENDORSED_DIRS" \ 349 -classpath "$CLASSPATH" \ 350 -sourcepath "$CATALINA_HOME"/../../java \ 351 -Djava.security.manager \ 352 -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \ 353 -Dcatalina.base="$CATALINA_BASE" \ 354 -Dcatalina.home="$CATALINA_HOME" \ 355 -Djava.io.tmpdir="$CATALINA_TMPDIR" \ 356 org.apache.catalina.startup.Bootstrap "$@" start 357 else 358 exec "$_RUNJDB" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \ 359 -D$ENDORSED_PROP="$JAVA_ENDORSED_DIRS" \ 360 -classpath "$CLASSPATH" \ 361 -sourcepath "$CATALINA_HOME"/../../java \ 362 -Dcatalina.base="$CATALINA_BASE" \ 363 -Dcatalina.home="$CATALINA_HOME" \ 364 -Djava.io.tmpdir="$CATALINA_TMPDIR" \ 365 org.apache.catalina.startup.Bootstrap "$@" start 366 fi 367 fi 368 369 elif [ "$1" = "run" ]; then 370 371 shift 372 if [ "$1" = "-security" ] ; then 373 if [ $have_tty -eq 1 ]; then 374 echo "Using Security Manager" 375 fi 376 shift 377 eval exec "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \ 378 -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \ 379 -classpath "\"$CLASSPATH\"" \ 380 -Djava.security.manager \ 381 -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \ 382 -Dcatalina.base="\"$CATALINA_BASE\"" \ 383 -Dcatalina.home="\"$CATALINA_HOME\"" \ 384 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \ 385 org.apache.catalina.startup.Bootstrap "$@" start 386 else 387 eval exec "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \ 388 -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \ 389 -classpath "\"$CLASSPATH\"" \ 390 -Dcatalina.base="\"$CATALINA_BASE\"" \ 391 -Dcatalina.home="\"$CATALINA_HOME\"" \ 392 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \ 393 org.apache.catalina.startup.Bootstrap "$@" start 394 fi 395 396 elif [ "$1" = "start" ] ; then 397 398 if [ ! -z "$CATALINA_PID" ]; then 399 if [ -f "$CATALINA_PID" ]; then 400 if [ -s "$CATALINA_PID" ]; then 401 echo "Existing PID file found during start." 402 if [ -r "$CATALINA_PID" ]; then 403 PID=`cat "$CATALINA_PID"` 404 ps -p $PID >/dev/null 2>&1 405 if [ $? -eq 0 ] ; then 406 echo "Tomcat appears to still be running with PID $PID. Start aborted." 407 echo "If the following process is not a Tomcat process, remove the PID file and try again:" 408 ps -f -p $PID 409 exit 1 410 else 411 echo "Removing/clearing stale PID file." 412 rm -f "$CATALINA_PID" >/dev/null 2>&1 413 if [ $? != 0 ]; then 414 if [ -w "$CATALINA_PID" ]; then 415 cat /dev/null > "$CATALINA_PID" 416 else 417 echo "Unable to remove or clear stale PID file. Start aborted." 418 exit 1 419 fi 420 fi 421 fi 422 else 423 echo "Unable to read PID file. Start aborted." 424 exit 1 425 fi 426 else 427 rm -f "$CATALINA_PID" >/dev/null 2>&1 428 if [ $? != 0 ]; then 429 if [ ! -w "$CATALINA_PID" ]; then 430 echo "Unable to remove or write to empty PID file. Start aborted." 431 exit 1 432 fi 433 fi 434 fi 435 fi 436 fi 437 438 shift 439 touch "$CATALINA_OUT" 440 if [ "$1" = "-security" ] ; then 441 if [ $have_tty -eq 1 ]; then 442 echo "Using Security Manager" 443 fi 444 shift 445 eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \ 446 -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \ 447 -classpath "\"$CLASSPATH\"" \ 448 -Djava.security.manager \ 449 -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \ 450 -Dcatalina.base="\"$CATALINA_BASE\"" \ 451 -Dcatalina.home="\"$CATALINA_HOME\"" \ 452 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \ 453 org.apache.catalina.startup.Bootstrap "$@" start \ 454 >> "$CATALINA_OUT" 2>&1 "&" 455 456 else 457 eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \ 458 -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \ 459 -classpath "\"$CLASSPATH\"" \ 460 -Dcatalina.base="\"$CATALINA_BASE\"" \ 461 -Dcatalina.home="\"$CATALINA_HOME\"" \ 462 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \ 463 org.apache.catalina.startup.Bootstrap "$@" start \ 464 >> "$CATALINA_OUT" 2>&1 "&" 465 466 fi 467 468 if [ ! -z "$CATALINA_PID" ]; then 469 echo $! > "$CATALINA_PID" 470 fi 471 472 echo "Tomcat started." 473 474 elif [ "$1" = "stop" ] ; then 475 476 shift 477 478 SLEEP=5 479 if [ ! -z "$1" ]; then 480 echo $1 | grep "[^0-9]" >/dev/null 2>&1 481 if [ $? -gt 0 ]; then 482 SLEEP=$1 483 shift 484 fi 485 fi 486 487 FORCE=0 488 if [ "$1" = "-force" ]; then 489 shift 490 FORCE=1 491 fi 492 493 if [ ! -z "$CATALINA_PID" ]; then 494 if [ -f "$CATALINA_PID" ]; then 495 if [ -s "$CATALINA_PID" ]; then 496 kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1 497 if [ $? -gt 0 ]; then 498 echo "PID file found but no matching process was found. Stop aborted." 499 exit 1 500 fi 501 else 502 echo "PID file is empty and has been ignored." 503 fi 504 else 505 echo "\$CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted." 506 exit 1 507 fi 508 fi 509 510 eval "\"$_RUNJAVA\"" $LOGGING_MANAGER $JAVA_OPTS \ 511 -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \ 512 -classpath "\"$CLASSPATH\"" \ 513 -Dcatalina.base="\"$CATALINA_BASE\"" \ 514 -Dcatalina.home="\"$CATALINA_HOME\"" \ 515 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \ 516 org.apache.catalina.startup.Bootstrap "$@" stop 517 518 # stop failed. Shutdown port disabled? Try a normal kill. 519 if [ $? != 0 ]; then 520 if [ ! -z "$CATALINA_PID" ]; then 521 echo "The stop command failed. Attempting to signal the process to stop through OS signal." 522 kill -15 `cat "$CATALINA_PID"` >/dev/null 2>&1 523 fi 524 fi 525 526 if [ ! -z "$CATALINA_PID" ]; then 527 if [ -f "$CATALINA_PID" ]; then 528 while [ $SLEEP -ge 0 ]; do 529 kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1 530 if [ $? -gt 0 ]; then 531 rm -f "$CATALINA_PID" >/dev/null 2>&1 532 if [ $? != 0 ]; then 533 if [ -w "$CATALINA_PID" ]; then 534 cat /dev/null > "$CATALINA_PID" 535 # If Tomcat has stopped don't try and force a stop with an empty PID file 536 FORCE=0 537 else 538 echo "The PID file could not be removed or cleared." 539 fi 540 fi 541 echo "Tomcat stopped." 542 break 543 fi 544 if [ $SLEEP -gt 0 ]; then 545 sleep 1 546 fi 547 if [ $SLEEP -eq 0 ]; then 548 echo "Tomcat did not stop in time." 549 if [ $FORCE -eq 0 ]; then 550 echo "PID file was not removed." 551 fi 552 echo "To aid diagnostics a thread dump has been written to standard out." 553 kill -3 `cat "$CATALINA_PID"` 554 fi 555 SLEEP=`expr $SLEEP - 1 ` 556 done 557 fi 558 fi 559 560 KILL_SLEEP_INTERVAL=5 561 if [ $FORCE -eq 1 ]; then 562 if [ -z "$CATALINA_PID" ]; then 563 echo "Kill failed: \$CATALINA_PID not set" 564 else 565 if [ -f "$CATALINA_PID" ]; then 566 PID=`cat "$CATALINA_PID"` 567 echo "Killing Tomcat with the PID: $PID" 568 kill -9 $PID 569 while [ $KILL_SLEEP_INTERVAL -ge 0 ]; do 570 kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1 571 if [ $? -gt 0 ]; then 572 rm -f "$CATALINA_PID" >/dev/null 2>&1 573 if [ $? != 0 ]; then 574 if [ -w "$CATALINA_PID" ]; then 575 cat /dev/null > "$CATALINA_PID" 576 else 577 echo "The PID file could not be removed." 578 fi 579 fi 580 echo "The Tomcat process has been killed." 581 break 582 fi 583 if [ $KILL_SLEEP_INTERVAL -gt 0 ]; then 584 sleep 1 585 fi 586 KILL_SLEEP_INTERVAL=`expr $KILL_SLEEP_INTERVAL - 1 ` 587 done 588 if [ $KILL_SLEEP_INTERVAL -lt 0 ]; then 589 echo "Tomcat has not been killed completely yet. The process might be waiting on some system call or might be UNINTERRUPTIBLE." 590 fi 591 fi 592 fi 593 fi 594 595 elif [ "$1" = "configtest" ] ; then 596 597 eval "\"$_RUNJAVA\"" $LOGGING_MANAGER $JAVA_OPTS \ 598 -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \ 599 -classpath "\"$CLASSPATH\"" \ 600 -Dcatalina.base="\"$CATALINA_BASE\"" \ 601 -Dcatalina.home="\"$CATALINA_HOME\"" \ 602 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \ 603 org.apache.catalina.startup.Bootstrap configtest 604 result=$? 605 if [ $result -ne 0 ]; then 606 echo "Configuration error detected!" 607 fi 608 exit $result 609 610 elif [ "$1" = "version" ] ; then 611 612 "$_RUNJAVA" \ 613 -classpath "$CATALINA_HOME/lib/catalina.jar" \ 614 org.apache.catalina.util.ServerInfo 615 616 else 617 618 echo "Usage: catalina.sh ( commands ... )" 619 echo "commands:" 620 if $os400; then 621 echo " debug Start Catalina in a debugger (not available on OS400)" 622 echo " debug -security Debug Catalina with a security manager (not available on OS400)" 623 else 624 echo " debug Start Catalina in a debugger" 625 echo " debug -security Debug Catalina with a security manager" 626 fi 627 echo " jpda start Start Catalina under JPDA debugger" 628 echo " run Start Catalina in the current window" 629 echo " run -security Start in the current window with security manager" 630 echo " start Start Catalina in a separate window" 631 echo " start -security Start in a separate window with security manager" 632 echo " stop Stop Catalina, waiting up to 5 seconds for the process to end" 633 echo " stop n Stop Catalina, waiting up to n seconds for the process to end" 634 echo " stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running" 635 echo " stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running" 636 echo " configtest Run a basic syntax check on server.xml - check exit code for result" 637 echo " version What version of tomcat are you running?" 638 echo "Note: Waiting for the process to end and use of the -force option require that \$CATALINA_PID is defined" 639 exit 1 640 641 fi
查看server.xml文件
1 <?xml version='1.0' encoding='utf-8'?> 2 <!-- 3 Licensed to the Apache Software Foundation (ASF) under one or more 4 contributor license agreements. See the NOTICE file distributed with 5 this work for additional information regarding copyright ownership. 6 The ASF licenses this file to You under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with 8 the License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 --> 18 <!-- Note: A "Server" is not itself a "Container", so you may not 19 define subcomponents such as "Valves" at this level. 20 Documentation at /docs/config/server.html 21 --> 22 <Server port="8005" shutdown="SHUTDOWN"> 23 <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> 24 <!-- Security listener. Documentation at /docs/config/listeners.html 25 <Listener className="org.apache.catalina.security.SecurityListener" /> 26 --> 27 <!--APR library loader. Documentation at /docs/apr.html --> 28 <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> 29 <!-- Prevent memory leaks due to use of particular java/javax APIs--> 30 <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> 31 <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> 32 <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> 33 34 <!-- Global JNDI resources 35 Documentation at /docs/jndi-resources-howto.html 36 --> 37 <GlobalNamingResources> 38 <!-- Editable user database that can also be used by 39 UserDatabaseRealm to authenticate users 40 --> 41 <Resource name="UserDatabase" auth="Container" 42 type="org.apache.catalina.UserDatabase" 43 description="User database that can be updated and saved" 44 factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 45 pathname="conf/tomcat-users.xml" /> 46 </GlobalNamingResources> 47 48 <!-- A "Service" is a collection of one or more "Connectors" that share 49 a single "Container" Note: A "Service" is not itself a "Container", 50 so you may not define subcomponents such as "Valves" at this level. 51 Documentation at /docs/config/service.html 52 --> 53 <Service name="Catalina"> 54 55 <!--The connectors can use a shared executor, you can define one or more named thread pools--> 56 <!-- 57 <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" 58 maxThreads="150" minSpareThreads="4"/> 59 --> 60 61 62 <!-- A "Connector" represents an endpoint by which requests are received 63 and responses are returned. Documentation at : 64 Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) 65 Java AJP Connector: /docs/config/ajp.html 66 APR (HTTP/AJP) Connector: /docs/apr.html 67 Define a non-SSL/TLS HTTP/1.1 Connector on port 8080 68 --> 69 <Connector port="8080" protocol="HTTP/1.1" 70 connectionTimeout="20000" 71 redirectPort="8443" /> 72 <!-- A "Connector" using the shared thread pool--> 73 <!-- 74 <Connector executor="tomcatThreadPool" 75 port="8080" protocol="HTTP/1.1" 76 connectionTimeout="20000" 77 redirectPort="8443" /> 78 --> 79 <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 80 This connector uses the NIO implementation that requires the JSSE 81 style configuration. When using the APR/native implementation, the 82 OpenSSL style configuration is required as described in the APR/native 83 documentation --> 84 <!-- 85 <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" 86 maxThreads="150" SSLEnabled="true" scheme="https" secure="true" 87 clientAuth="false" sslProtocol="TLS" /> 88 --> 89 90 <!-- Define an AJP 1.3 Connector on port 8009 --> 91 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 92 93 94 <!-- An Engine represents the entry point (within Catalina) that processes 95 every request. The Engine implementation for Tomcat stand alone 96 analyzes the HTTP headers included with the request, and passes them 97 on to the appropriate Host (virtual host). 98 Documentation at /docs/config/engine.html --> 99 100 <!-- You should set jvmRoute to support load-balancing via AJP ie : 101 <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> 102 --> 103 <Engine name="Catalina" defaultHost="localhost"> 104 105 <!--For clustering, please take a look at documentation at: 106 /docs/cluster-howto.html (simple how to) 107 /docs/config/cluster.html (reference documentation) --> 108 <!-- 109 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> 110 --> 111 112 <!-- Use the LockOutRealm to prevent attempts to guess user passwords 113 via a brute-force attack --> 114 <Realm className="org.apache.catalina.realm.LockOutRealm"> 115 <!-- This Realm uses the UserDatabase configured in the global JNDI 116 resources under the key "UserDatabase". Any edits 117 that are performed against this UserDatabase are immediately 118 available for use by the Realm. --> 119 <Realm className="org.apache.catalina.realm.UserDatabaseRealm" 120 resourceName="UserDatabase"/> 121 </Realm> 122 123 <Host name="localhost" appBase="/data/webapps" unpackWARs="true" autoDeploy="true"> 124 125 <!-- SingleSignOn valve, share authentication between web applications 126 Documentation at: /docs/config/valve.html --> 127 <!-- 128 <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> 129 --> 130 131 <!-- Access log processes all example. 132 Documentation at: /docs/config/valve.html 133 Note: The pattern used is equivalent to using pattern="common" --> 134 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 135 prefix="localhost_access_log" suffix=".txt" 136 pattern="%h %l %u %t "%r" %s %b" /> 137 138 </Host> 139 </Engine> 140 </Service> 141 </Server>
制作haproxy镜像
准备haproxy的安装包
查看 Dockerfile
1 [root@192 haproxy]# cat Dockerfile 2 #haproxy 3 From centos-base:v1 4 MAINTAINER hexintong "137841632@qq.com" 5 RUN yum install glibc glibc-devel systemd-devel net-tools iotop bc screen lsof tcpdump wget ntpdate -y 6 ADD haproxy-1.8.12.tar.gz /usr/local/src/ 7 RUN cd /usr/local/src/haproxy-1.8.12 && make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy && cp haproxy /usr/sbin/ && mkdir /usr/local/haproxy/run 8 ADD haproxy.cfg /etc/haproxy/ 9 ADD run_haproxy.sh /usr/bin/ 10 EXPOSE 80 9999 11 CMD ["/usr/bin/run_haproxy.sh"]
查看 haproxy.cfg
1 global 2 chroot /usr/local/haproxy 3 #stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin 4 uid 99 5 gid 99 6 daemon 7 nbproc 1 8 pidfile /usr/local/haproxy/run/haproxy.pid 9 log 127.0.0.1 local3 info 10 11 defaults 12 option http-keep-alive 13 option forwardfor 14 mode http 15 timeout connect 300000ms 16 timeout client 300000ms 17 timeout server 300000ms 18 19 listen stats 20 mode http 21 bind 0.0.0.0:9999 22 stats enable 23 log global 24 stats uri /haproxy-status 25 stats auth haadmin:q1w2e3r4ys 26 27 listen web_port 28 bind 0.0.0.0:80 29 mode http 30 log global 31 balance roundrobin 32 server web1 nginx-web1:80 check inter 3000 fall 2 rise 5 33 server web2 nginx-web2:80 check inter 3000 fall 2 rise 5
查看run_haproxy.sh
1 [root@192 haproxy]# cat run_haproxy.sh 2 #!/bin/bash 3 haproxy -f /etc/haproxy/haproxy.cfg 4 tail -f /etc/hosts
查看 build-commad.sh
1 [root@192 haproxy]# cat build-commad.sh 2 #!/bin/bash 3 docker build -t haproxy:v1 .
查看产生的haproxy:v1 镜像
查看启动的haproxy容器
查看 haprxy的状态
查看haproxy的切换状态
查看haproxy的切换状态
troubleshootings
解决docker 容器内访问宿主机“No route to host”的问题
解决 haproxy 503 service unavailable no server is avaible to handle this request
是因为防火墙的问题,把 haproxy托管的接口在防火墙上放行
[root@192 haproxy]# systemctl status firewalld
[root@192 haproxy]# firewall-cmd --zone=public --list-ports
[root@192 haproxy]# firewall-cmd --zone=public --add-port=8812/tcp
[root@192 haproxy]# firewall-cmd --zone=public --add-port=8811/tcp