一、制作openjdk8基础镜像【基于自定义alpine-3.18.0:v1 】
docker pull maven:3.5.0-jdk-8-alpine
78.56 MB
https://hub.docker.com/_/maven/tagspage=8&name=alpine
openjdk二进制下载地址
https://blog.csdn.net/fenglllle/article/details/124786948
https://adoptopenjdk.net/releases.html
Dockerfile中,source /etc/profile不能加载的原因
为什么还需要选择使用他的原因:三 中,tomcat普通用户交互式启动tomcat
#在 Docker 容器中,/etc/profile 文件不会在容器启动时自动执行,这是因为 Docker 容器通常不会启动交互式登录 shell,而是直接运行指定的命令。/etc/profile 和用户的 shell 配置文件(如 .bash_profile[root]、.bashrc[普通用户] 等)是在交互式登录 shell 中被加载的。如果容器启动命令不涉及启动一个交互式 shell,这些文件就不会被执行。
解决方法
如果你需要在 Docker 容器中自动加载 /etc/profile 或其他环境配置,你可以采取以下几种方法之一:
1. 修改容器的启动命令
确保容器启动时使用交互式 shell。例如,你可以在 Dockerfile 中设置 CMD 或 ENTRYPOINT 指令来启动一个 Bash shell:
这样,当你以交互式方式运行容器时(使用 docker run -it <image>),它将启动 Bash,该 Bash 会加载 /etc/profile。
2. 在 Dockerfile 中使用环境变量
对于一些简单的环境设置,你也可以直接在 Dockerfile 中使用 ENV 指令来设置环境变量,这样就不需要依赖 /etc/profile
root@ubuntu:~/system/openjdk# cat profile
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"export PAGER=less
umask 022# use nicer PS1 for bash and busybox ash
if [ -n "$BASH_VERSION" -o "$BB_ASH_VERSION" ]; thenPS1='h:w$ '
# use nicer PS1 for zsh
elif [ -n "$ZSH_VERSION" ]; thenPS1='%m:%~%# '
# set up fallback default PS1
else: "${HOSTNAME:=$(hostname)}"PS1='${HOSTNAME%%.*}:$PWD'[ "$(id -u)" -eq 0 ] && PS1="${PS1}# " || PS1="${PS1}$ "
fiif [ -n "$BASH_VERSION" ] && [ "$BASH" != "/bin/sh" ]; then# if we're bash (and not /bin/sh bash), also source the bashrc# by default, bash sources the bashrc for non-login,# and only /etc/profile on login (-l). so, make it do both on login.# this ensures that login-shell bash (e.g. -bash or bash -l) still sources the# system bashrc, which e.g. loads bash-completion. /etc/bash/bashrc
fifor script in /etc/profile.d/*.sh ; doif [ -r "$script" ] ; then. "$script"fi
done
unset script
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=$JAVA_HOME/jre
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib:$JAVA_HOME/lib/tools.jarroot@ubuntu:~/system/openjdk# tree ./
./
└── OpenJDK8U-jdk_x64_alpine-linux_hotspot_8u432b06.tar.gz
└── profile
root@ubuntu:~/system/openjdk# cat Dockerfile
#JDK Base Image
#基于alpine-3.18.0:v1【官方镜像+自定义环境】
FROM registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0:v1
LABEL Image="Openjdk base"
ADD OpenJDK8U-jdk_x64_alpine-linux_hotspot_8u432b06.tar.gz /usr/local/src/
RUN ln -s /usr/local/src/jdk8u432-b06 /usr/local/jdk
ADD profile /etc/profile
ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV PATH $JAVA_HOME/bin:$JRE_HOME/bin:$PATH
ENV CLASSPATH .$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib:$JAVA_HOME/lib/tools.jardocker build -t registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-openjdk-8u432b06:v1 .
docker push registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-openjdk-8u432b06:v1docker run -ti --rm registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-openjdk-8u432b06:v1 sh/ # java -version
openjdk version "1.8.0_432"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_432-b06)
OpenJDK 64-Bit Server VM (Temurin)(build 25.432-b06, mixed mode)/ # env
HOSTNAME=ba44d1f0c9fe
SHLVL=1
HOME=/root
JRE_HOME=/usr/local/jdk/jre
TERM=xterm
PATH=/usr/local/jdk/bin:/usr/local/jdk/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
CLASSPATH=.:/usr/local/jdk/lib:/usr/local/jdk/jre/lib:/usr/local/jdk/lib/tools.jar
JAVA_HOME=/usr/local/jdk
PWD=// # id www
uid=2024(www) gid=2024(www) groups=2024(www),2024(www)
tomcat_9_Baseopenjdk8__118">二、制作tomcat 9 Base镜像【基于自制openjdk8基础镜像 】
2.1 基于自定义的 JDK 基础镜像,构建出通用的自定义 Tomcat 基础镜像,此镜像后期会被多个业务的多个服务共同引用
2.2 JDK 版本和Tomcat 版本对应关系,如下:
https://tomcat.apache.org/whichversion.html
tomcat_8tomcat_9_127">2.3 tomcat 8版本,官网已经没有维护了,使用tomcat 9版本
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.97/bin/apache-tomcat-9.0.97.tar.gz
cat Dockerfile
#Tomcat Base Image
#Tomcat Base Image
FROM registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-openjdk-8u432b06:v1
#env
ENV TZ "Asia/Shanghai"
ENV LANG en_US.UTF-8
ENV TERM xterm
ENV TOMCAT_MAJOR_VERSION 9
ENV TOMCAT_MINOR_VERSION 9.0.97
ENV CATALINA_HOME /apps/tomcat
ENV APP_DIR ${CATALINA_HOME}/webapps#tomcat
RUN mkdir /apps
#添加系统账户
RUN addgroup -g 2025 -S tomcat && adduser -u 2025 -G tomcat -D -S -s /bin/sh tomcat
ADD apache-tomcat-9.0.97.tar.gz /apps
RUN ln -s /apps/apache-tomcat-9.0.97 /apps/tomcatdocker build -t registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-openjdk-8u432b06-v1-tomcat-9.0.97:v1 .
docker run -it --rm -p 8080:8080 registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-openjdk-8u432b06-v1-tomcat-9.0.97:v1 sh
/ # /apps/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /apps/tomcat
Using CATALINA_HOME: /apps/tomcat
Using CATALINA_TMPDIR: /apps/tomcat/temp
Using JRE_HOME: /usr/local/jdk/jre
Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started./ # netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN 14/java
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 14/java
/ # env
HOSTNAME=3e8f4f50d5b6
TOMCAT_MINOR_VERSION=9.0.97
SHLVL=1
HOME=/root
JRE_HOME=/usr/local/jdk/jre
CATALINA_HOME=/apps/tomcat
TERM=xterm
PATH=/usr/local/jdk/bin:/usr/local/jdk/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
APP_DIR=/apps/tomcat/webapps
LANG=en_US.UTF-8
CLASSPATH=.:/usr/local/jdk/lib:/usr/local/jdk/jre/lib:/usr/local/jdk/lib/tools.jar
TOMCAT_MAJOR_VERSION=9
JAVA_HOME=/usr/local/jdk
PWD=/
TZ=Asia/Shanghaidocker push registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-openjdk-8u432b06-v1-tomcat-9.0.97:v1
tomcat_9_Base_194">三、构建业务镜像1【基于tomcat 9 Base镜像】
root@ubuntu:~/web# mkdir tomcat/tomcat-app{1,2}.
root@ubuntu:~/web# tree tomcat
tomcat
├── apache-tomcat-9.0.97.tar.gz
├── Dockerfile
├── tomcat-app1
└── tomcat-app2
root@ubuntu:~/web/tomcat# tar xzvf apache-tomcat-9.0.97.tar.gz
root@ubuntu:~/web/tomcat# cp apache-tomcat-9.0.97/conf/server.xml tomcat-app1/
root@ubuntu:~/web/tomcat# vim tomcat-app1/server.xml
......
<Host name="localhost" appBase="/data/tomcat/webapps"
unpackWARs="true" autoDeploy="true">
......
root@ubuntu:~/web/tomcat# cd tomcat-app1
root@ubuntu:~/web/tomcat/tomcat-app1# mkdir app
root@ubuntu:~/web/tomcat/tomcat-app1# echo "Tomcat Page in app1" > app/index.jsp
root@ubuntu:~/web/tomcat/tomcat-app1# tar zcf app.tar.gz app
root@ubuntu:~/web/tomcat/tomcat-app1# ls
app app.tar.gz server.xml
root@ubuntu:~/web/tomcat/tomcat-app1# cat run_tomcat.sh
#!/bin/bash
echo "nameserver 192.168.31.1" > /etc/resolv.conf
su - tomcat -c "/apps/tomcat/bin/catalina.sh start"
su - tomcat -c "tail -f /etc/hosts"
root@ubuntu:~/web/tomcat/tomcat-app1# chmod a+x run_tomcat.sh
root@ubuntu:~/web/tomcat/tomcat-app1# cat Dockerfile
#Tomcat Web Image
FROM registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-openjdk-8u432b06-v1-tomcat-9.0.97:v1
ADD server.xml /apps/tomcat/conf/server.xml
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD app.tar.gz /data/tomcat/webapps/
RUN chown -R tomcat.tomcat /apps/ /data/tomcat/
EXPOSE 8080
CMD ["/apps/tomcat/bin/run_tomcat.sh"]root@ubuntu:~/web/tomcat/tomcat-app1# docker build -t registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-openjdk-8u432b06-v1-tomcat-9.0.97-v1-app1:v1 .
root@ubuntu:~/web/tomcat/tomcat-app1# docker push registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-openjdk-8u432b06-v1-tomcat-9.0.97-v1-app1:v1docker run -ti --rm -p 8080:8080 registry.cn-hangzhou.aliyuncs.com/qiushi/alpine-3.18.0-openjdk-8u432b06-v1-tomcat-9.0.97-v1-app1:v1
Using CATALINA_BASE: /apps/tomcat
Using CATALINA_HOME: /apps/tomcat
Using CATALINA_TMPDIR: /apps/tomcat/temp
Using JRE_HOME: /usr/local/jdk/jre
Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.100.2 79a46dbde53f