方法一、docker pull tomcat
查找 上的 tomcat 镜像:
可以通过 sort by 查看其他版本的 tomcat,默认是最新版本 tomcat:latest。
此外,我们还可以用 docker search tomcat 命令来查看可用版本:
coonote@coonote:~/tomcat$ docker search tomcat name description stars official automated tomcat apache tomcat is an open source implementa... 744 [ok] dordoka/tomcat ubuntu 14.04, oracle jdk 8 and tomcat 8 ba... 19 [ok] consol/tomcat-7.0 tomcat 7.0.57, 8080, "admin/admin" 16 [ok] consol/tomcat-8.0 tomcat 8.0.15, 8080, "admin/admin" 14 [ok] cloudesire/tomcat tomcat server, 6/7/8 8 [ok] davidcaste/alpine-tomcat apache tomcat 7/8 using oracle java 7/8 wi... 6 [ok] andreptb/tomcat debian jessie based image with apache tomc... 4 [ok] kieker/tomcat 2 [ok] fbrx/tomcat minimal tomcat image based on alpine linux 2 [ok] jtech/tomcat latest tomcat production distribution on l... 1 [ok]
这里我们拉取官方的镜像:
coonote@coonote:~/tomcat$ docker pull tomcat
等待下载完成后,我们就可以在本地镜像列表里查到 repository 为 tomcat 的镜像。
coonote@coonote:~/tomcat$ docker images|grep tomcat
tomcat latest 70f819d3d2d9 7 days ago 335.8 mb
方法二、通过 dockerfile 构建
创建dockerfile
首先,创建目录tomcat,用于存放后面的相关东西。
coonote@coonote:~$ mkdir -p ~/tomcat/webapps ~/tomcat/logs ~/tomcat/conf
webapps 目录将映射为 tomcat 容器配置的应用程序目录。
logs 目录将映射为 tomcat 容器的日志目录。
conf 目录里的配置文件将映射为 tomcat 容器的配置文件。
进入创建的 tomcat 目录,创建 dockerfile。
from openjdk:8-jre
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
# runtime dependencies for tomcat native libraries
# tomcat native 1.2 requires a newer version of openssl than debian:jessie has available
# > checking openssl library version >= 1.0.2...
# > configure: error: your version of openssl is not compatible with this version of tcnative
# see http://tomcat.10.x6.nabble.com/vote-release-apache-tomcat-8-0-32-tp5046007p5046024.html (and following discussion)
# and https://github.com/docker-library/tomcat/pull/31
env openssl_version 1.1.0f-3 deb9u2
run set -ex; \
currentversion="$(dpkg-query --show --showformat '${version}\n' openssl)"; \
if dpkg --compare-versions "$currentversion" '<<' "$openssl_version"; then \
if ! grep -q stretch /etc/apt/sources.list; then \
# only add stretch if we're not already building from within stretch
{ \
echo 'deb http://deb.debian.org/debian stretch main'; \
echo 'deb http://security.debian.org stretch/updates main'; \
echo 'deb http://deb.debian.org/debian stretch-updates main'; \
} > /etc/apt/sources.list.d/stretch.list; \
{ \
# add a negative "pin-priority" so that we never ever get packages from stretch unless we explicitly request them
echo 'package: *'; \
echo 'pin: release n=stretch*'; \
echo 'pin-priority: -10'; \
echo; \
# ... except openssl, which is the reason we're here
echo 'package: openssl libssl*'; \
echo "pin: version $openssl_version"; \
echo 'pin-priority: 990'; \
} > /etc/apt/preferences.d/stretch-openssl; \
fi; \
apt-get update; \
apt-get install -y --no-install-recommends openssl="$openssl_version"; \
rm -rf /var/lib/apt/lists/*; \
fi
run apt-get update && apt-get install -y --no-install-recommends \
libapr1 \
&& rm -rf /var/lib/apt/lists/*
# see https://www.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
env tomcat_major 8
env tomcat_version 8.5.32
env tomcat_sha512 fc010f4643cb9996cad3812594190564d0a30be717f659110211414faf8063c61fad1f18134154084ad3ddfbbbdb352fa6686a28fbb6402d3207d4e0a88fa9ce
env tomcat_tgz_urls \
# https://issues.apache.org/jira/browse/infra-8753?focusedcommentid=14735394#comment-14735394
https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-$tomcat_major/v$tomcat_version/bin/apache-tomcat-$tomcat_version.tar.gz \
# if the version is outdated, we might have to pull from the dist/archive :/
https://www-us.apache.org/dist/tomcat/tomcat-$tomcat_major/v$tomcat_version/bin/apache-tomcat-$tomcat_version.tar.gz \
https://www.apache.org/dist/tomcat/tomcat-$tomcat_major/v$tomcat_version/bin/apache-tomcat-$tomcat_version.tar.gz \
https://archive.apache.org/dist/tomcat/tomcat-$tomcat_major/v$tomcat_version/bin/apache-tomcat-$tomcat_version.tar.gz
env tomcat_asc_urls \
https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-$tomcat_major/v$tomcat_version/bin/apache-tomcat-$tomcat_version.tar.gz.asc \
# not all the mirrors actually carry the .asc files :'(
https://www-us.apache.org/dist/tomcat/tomcat-$tomcat_major/v$tomcat_version/bin/apache-tomcat-$tomcat_version.tar.gz.asc \
https://www.apache.org/dist/tomcat/tomcat-$tomcat_major/v$tomcat_version/bin/apache-tomcat-$tomcat_version.tar.gz.asc \
https://archive.apache.org/dist/tomcat/tomcat-$tomcat_major/v$tomcat_version/bin/apache-tomcat-$tomcat_version.tar.gz.asc
run set -eux; \
\
savedaptmark="$(apt-mark showmanual)"; \
apt-get update; \
\
apt-get install -y --no-install-recommends gnupg dirmngr; \
\
export gnupghome="$(mktemp -d)"; \
for key in $gpg_keys; do \
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
done; \
\
apt-get install -y --no-install-recommends wget ca-certificates; \
\
success=; \
for url in $tomcat_tgz_urls; do \
if wget -o tomcat.tar.gz "$url"; then \
success=1; \
break; \
fi; \
done; \
[ -n "$success" ]; \
\
echo "$tomcat_sha512 *tomcat.tar.gz" | sha512sum -c -; \
\
success=; \
for url in $tomcat_asc_urls; do \
if wget -o tomcat.tar.gz.asc "$url"; then \
success=1; \
break; \
fi; \
done; \
[ -n "$success" ]; \
\
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*; \
rm -rf "$gnupghome"; \
\
nativebuilddir="$(mktemp -d)"; \
tar -xvf bin/tomcat-native.tar.gz -c "$nativebuilddir" --strip-components=1; \
apt-get install -y --no-install-recommends \
dpkg-dev \
gcc \
libapr1-dev \
libssl-dev \
make \
"openjdk-${java_version%%[.~bu-]*}-jdk=$java_debian_version" \
; \
( \
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 "$(nproc)"; \
make install; \
); \
rm -rf "$nativebuilddir"; \
rm bin/tomcat-native.tar.gz; \
\
# reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies
apt-mark auto '.*' > /dev/null; \
[ -z "$savedaptmark" ] || apt-mark manual $savedaptmark; \
apt-get purge -y --auto-remove -o apt::autoremove::recommendsimportant=false; \
rm -rf /var/lib/apt/lists/*; \
\
# sh removes env vars it doesn't support (ones with periods)
# https://github.com/docker-library/tomcat/issues/77
find ./bin/ -name '*.sh' -exec sed -ri 's|^#!/bin/sh$|#!/usr/bin/env bash|' '{}'
# 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
expose 8080
cmd ["catalina.sh", "run"]
通过 dockerfile 创建一个镜像,替换成你自己的名字:
coonote@coonote:~/tomcat$ docker build -t tomcat .
创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像:
coonote@coonote:~/tomcat$ docker images|grep tomcat tomcat latest 70f819d3d2d9 7 days ago 335.8 mb
使用 tomcat 镜像
运行容器
coonote@coonote:~/tomcat$ docker run --name tomcat -p 8080:8080 -v $pwd/test:/usr/local/tomcat/webapps/test -d tomcat acb33fcb4beb8d7f1ebace6f50f5fc204b1dbe9d524881267aa715c61cf75320 coonote@coonote:~/tomcat$
命令说明:
-p 8080:8080:将主机的 8080 端口映射到容器的 8080 端口。
-v $pwd/test:/usr/local/tomcat/webapps/test:将主机中当前目录下的 test 挂载到容器的 /test。
查看容器启动情况
coonote@coonote:~/tomcat$ docker ps container id image command ... ports names acb33fcb4beb tomcat "catalina.sh run" ... 0.0.0.0:8080->8080/tcp tomcat
通过浏览器访问