使用 alpine 镜像作为基源创建镜像,因为 alpine 镜像非容小,比 Redhat、Centos、Ubuntu 等镜像小了近 200M 多兆,因此基于 alpine 制作出来的镜像会小非常多。
[root@centos8-110 ~]# docker images |grep alpine
alpine latest c059bfaa849c 17 months ago 5.59MB
复制代码
本文以 X86 平台下制作基于 alpine 的 Tomcat 镜像为例进行说明。
# 1、制作 Dockerfile 之前,需要准备介质文件,主要是 Tomcat 和 JDK
Tomcat 下载:https://downloads.apache.org/tomcat/tomcat-9/v9.0.74/bin/apache-tomcat-9.0.74.zip
JDK 下载:链接:https://pan.baidu.com/s/1bM1A7Y8tcrS4wIM619fQ6Q?pwd=d5g8,也可以在OpenJDK官网或者是Oracle官网下载JDK。
最终目录如下:
[root@centos8-110 tomcat]# ls -alrt
total 113504
-rw-r--r-- 1 root root 104566766 May 2 08:17 OpenJDK8U-jdk_x64_linux_hotspot_8u242b08.tar.gz
-rw-r--r-- 1 root root 11642299 May 2 08:30 apache-tomcat-9.0.74.tar.gz
复制代码
# 2、制作 Dockerfile
主要是 ADD 解压 tomcat 和 JDK,以及设置对应的 JAVA_HOME
FROM alpine:latest
LABEL MAITAINER xuqingkang2@163.com
ENV CATALINA_HOME=/usr/local/tomcat
ENV JAVA_HOME=/usr/local/java
ENV PATH=$CATALINA_HOME:$JAVA_HOME/bin:$PATH
RUN mkdir /source
ADD apache-tomcat-9.0.74.tar.gz /source
ADD OpenJDK8U-jdk_x64_linux_hotspot_8u242b08.tar.gz /source
RUN mv /source/apache* $CATALINA_HOME && mv /source/jdk* $JAVA_HOME && rm -rf /source
WORKDIR $CATALINA_HOME
RUN ln -sf /dev/stdout $CATALINA_HOME/logs/access.log && ln -sf /dev/stderr $CATALINA_HOME/logs/error.log
EXPOSE 8080
CMD ["./bin/catalina.sh","run"]
复制代码
3、打包镜像,使用 docker build 命令制作镜像
docker build --force-rm=true --no-cache=true -t apache/tomcat:9.0.74-alpine -f Dockerfile .
复制代码
4、发现问题:直接使用 docker run 运行镜像发现 tomcat 无法启动。 原因是 java 命令无法运行,通过进入容器时直接运行 sh,做检查如下:
[root@centos8-110 tomcat]# docker run -it apache/tomcat:9.0.74-alpine "/bin/sh"
/usr/local/tomcat/bin # java -version
/bin/sh: java: not found
复制代码
5、解决问题:解决 glibc 的依赖,以上 java 命令找不到的原因就是因为 glibc 没有。
注意:本来 sgerrand.rsa.pub 是可以在https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub访问,但发现Dockerfile运行时无法连接。在可以访问公网的主机上下载保存到 Dockerfile 同级目录后操作。直接使用 COPY 命令进行拷贝。
FROM alpine:latest
LABEL MAITAINER xuqingkang2@163.com
ENV CATALINA_HOME=/usr/local/tomcat
ENV JAVA_HOME=/usr/local/java
ENV PATH=$CATALINA_HOME:$JAVA_HOME/bin:$PATH
RUN mkdir /source
ADD apache-tomcat-9.0.74.tar.gz /source
ADD OpenJDK8U-jdk_x64_linux_hotspot_8u242b08.tar.gz /source
COPY sgerrand.rsa.pub /etc/apk/keys/sgerrand.rsa.pub
RUN mv /source/apache* $CATALINA_HOME && mv /source/jdk* $JAVA_HOME && rm -rf /source
#RUN wget -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.29-r0/glibc-2.29-r0.apk && \
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
apk --no-cache add ca-certificates && \
apk add glibc-2.29-r0.apk
WORKDIR $CATALINA_HOME
RUN ln -sf /dev/stdout $CATALINA_HOME/logs/access.log && ln -sf /dev/stderr $CATALINA_HOME/logs/error.log
EXPOSE 8080
CMD ["./bin/catalina.sh","run"]
复制代码
6、重新打包运行镜像,在日志中确认 Tomcat 启动正常。
[root@centos8-110 tomcat]# docker run -it apache/tomcat:9.0.74-alpine "/bin/sh"
/usr/local/tomcat # which java
/usr/local/java/bin/java
/usr/local/tomcat # java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.242-b08, mixed mode)
/usr/local/tomcat # cd bin/
/usr/local/tomcat/bin # ./startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
/usr/local/tomcat # cd ../logs/
/usr/local/tomcat/logs # tail -f catalina.out
03-May-2023 02:22:18.854 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/docs]
03-May-2023 02:22:18.861 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/docs] has finished in [7] ms
03-May-2023 02:22:18.861 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/examples]
03-May-2023 02:22:18.947 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/examples] has finished in [85] ms
03-May-2023 02:22:18.947 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/host-manager]
03-May-2023 02:22:18.954 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/host-manager] has finished in [7] ms
03-May-2023 02:22:18.954 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/manager]
03-May-2023 02:22:18.959 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [5] ms
03-May-2023 02:22:18.960 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
03-May-2023 02:22:18.968 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [230] milliseconds
复制代码
评论