Zacard's Notes

Docker学习系列七:构建jetty镜像

前言

Docker hub官方已经维护了一套比较完善的jetty镜像,但是依赖的是openjdk。所以这里只是把jdk换成之前学习系列中构建过的oracle-jdk8。

Dockerfile描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
FROM oraclejdk8
MAINTAINER zacard <[email protected]>
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN addgroup -S jetty && adduser -D -S -H -G jetty jetty && rm -rf /etc/group- /etc/passwd- /etc/shadow-
ENV JETTY_HOME /usr/local/jetty
ENV PATH $JETTY_HOME/bin:$PATH
RUN mkdir -p "$JETTY_HOME"
WORKDIR $JETTY_HOME
ENV JETTY_BASE /var/lib/jetty
RUN mkdir -p "$JETTY_BASE"
ENV JETTY_VERSION 9.3.10.v20160621
ENV JETTY_TGZ_URL https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-distribution/$JETTY_VERSION/jetty-distribution-$JETTY_VERSION.tar.gz
# GPG Keys are personal keys of Jetty committers (see https://dev.eclipse.org/mhonarc/lists/jetty-users/msg05220.html)
ENV JETTY_GPG_KEYS \
# 1024D/8FB67BAC 2006-12-10 Joakim Erdfelt <[email protected]>
B59B67FD7904984367F931800818D9D68FB67BAC \
# 1024D/D7C58886 2010-03-09 Jesse McConnell (signing key) <[email protected]>
5DE533CB43DAF8BC3E372283E7AE839CD7C58886
RUN set -xe \
# Install required packages for build time. Will be removed when build finishes.
&& apk add --no-cache --virtual .build-deps gnupg coreutils curl \
&& curl -SL "$JETTY_TGZ_URL" -o jetty.tar.gz \
&& curl -SL "$JETTY_TGZ_URL.asc" -o jetty.tar.gz.asc \
&& export GNUPGHOME="$(mktemp -d)" \
&& for key in $JETTY_GPG_KEYS; do \
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; done \
&& gpg --batch --verify jetty.tar.gz.asc jetty.tar.gz \
&& rm -r "$GNUPGHOME" \
&& tar -xvzf jetty.tar.gz \
&& mv jetty-distribution-$JETTY_VERSION/* ./ \
&& sed -i '/jetty-logging/d' etc/jetty.conf \
&& rm -fr demo-base javadoc \
&& rm jetty.tar.gz* \
&& rm -fr jetty-distribution-$JETTY_VERSION/ \
# Get the list of modules in the default start.ini and build new base with those modules, then add setuid
&& cd $JETTY_BASE \
&& modules="$(grep -- ^--module= "$JETTY_HOME/start.ini" | cut -d= -f2 | paste -d, -s)" \
&& java -jar "$JETTY_HOME/start.jar" --add-to-startd="$modules,setuid" \
# Remove installed packages and various cleanup
&& apk del .build-deps \
&& rm -fr .build-deps \
&& rm -rf /tmp/hsperfdata_root
WORKDIR $JETTY_BASE
ENV TMPDIR /tmp/jetty
RUN set -xe \
&& mkdir -p "$TMPDIR" \
&& chown -R jetty:jetty "$TMPDIR" "$JETTY_BASE"
COPY docker-entrypoint.sh /
EXPOSE 8080
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["java","-jar","/usr/local/jetty/start.jar"]

说明

这里的Dockerfile是根据官方的jetty9-alpine改写。其实也仅仅是把依赖镜像改成了自己构建的oracle-jdk8而已。

这里还依赖了一个sh文件,具体内容如下:

docker-entrypoint.sh:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh
set -e
if [ "$1" = jetty.sh ]; then
if ! command -v bash >/dev/null 2>&1 ; then
cat >&2 <<- 'EOWARN'
********************************************************************
ERROR: bash not found. Use of jetty.sh requires bash.
********************************************************************
EOWARN
exit 1
fi
cat >&2 <<- 'EOWARN'
********************************************************************
WARNING: Use of jetty.sh from this image is deprecated and may
be removed at some point in the future.
See the documentation for guidance on extending this image:
https://github.com/docker-library/docs/tree/master/jetty
********************************************************************
EOWARN
fi
if ! command -v -- "$1" >/dev/null 2>&1 ; then
set -- java -jar "$JETTY_HOME/start.jar" "$@"
fi
if [ -n "$TMPDIR" ] ; then
case "$JAVA_OPTIONS" in
*-Djava.io.tmpdir=*) ;;
*) JAVA_OPTIONS="-Djava.io.tmpdir=$TMPDIR $JAVA_OPTIONS" ;;
esac
fi
if [ "$1" = "java" -a -n "$JAVA_OPTIONS" ] ; then
shift
set -- java $JAVA_OPTIONS "$@"
fi
exec "$@"

注意:docker-entrypoint.sh要和Dockerfile同一目录,且需要设置可执行权限。

build镜像

docker buile -t jetty:9-oraclejdk8 .

测试镜像

docker run –rm jetty:9-oraclejdk8 –list-config

启动镜像

具体如何启动部署镜像,请参考官方说明文档。传送门

保存镜像

docker save -o jetty.tar jetty:9-oraclejdk8

加载镜像

docker -i jetty.tar

坚持原创技术分享,您的支持将鼓励我继续创作!

热评文章