1.1 制作alpine自定义镜像
Alpine使用的c库使用mini版的musl libc与其他Linux发行版使用的gnu libc不一样。虽说号称兼容,但也只是部分兼容了。Alpine官方给出了Alpine的三大特征 Small、Simple、Secure,但其实我们知道一个jdk就已经不小了,强行安装只会违背Alpine的设计初衷,最后其实与其他操作系统差不多了。所以对于java程序来说使用CentOS等操作系统会更好一下。
强行利用alpinean使用jdk就要安装glibc
#下载alpine镜像
[root@rocky8 alpine]$ docker pull alpine:3.17.2
#准备相关文件
[root@rocky8 alpine]$ pwd
/data/dockerfile/system/alpine
[root@rocky8 alpine]$ cat > repositories <<EOF
http://mirrors.aliyun.com/alpine/v3.11/main
http://mirrors.aliyun.com/alpine/v3.11/community
EOF
#制作Dockerfile文件
[root@rocky8 alpine]$ vi Dockerfile
[root@rocky8 alpine]$ cat Dockerfile
FROM alpine:3.17.2
LABEL author=yanlinux
COPY repositories /etc/apk/repositories
RUN apk update && apk --no-cache add iotop gcc bash libgcc libc-dev libcurl libc-utils pcre-dev zlib-dev libnfs make pcre pcre2 zip unzip net-tools pstree wget libevent libevent-dev iproute2 && \
wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.30-r0/glibc-2.30-r0.apk && \
apk add --force glibc-2.30-r0.apk && \
rm -rf *.apk && \
rm -rf /var/cache/apk/*
#构建镜像
[root@rocky8 alpine]$ docker build -t alpine-base:3.17.2 .
[root@rocky8 alpine]$ docker images alpine-base
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine-base 3.17.2 28edf4ec236f 55 seconds ago 189MB
1.2 基于自定义alpine镜像制作jdk镜像
#从alpine容器中拷贝一份/etc/profile文件
[root@rocky8 jdk]$ docker run -it --rm alpine-base:3.17.2 sh
[root@rocky8 jdk]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae3595b2d8cf alpine-base:3.17.2 "sh" 3 minutes ago Up 3 minutes ecstatic_nobel
[root@rocky8 jdk]$ docker cp ae3595b2d8cf:/etc/profile /data/dockerfile/web/jdk/
#修改profile文件,加下面四行相关变量
[root@rocky8 jdk]$ vi profile
......
export JAVA_HOME=/usr/local/jdk
export TOMCAT_HOME=/apps/tomcat
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$TOMCAT_HOME/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
#下载jdk文件到目录下
[root@rocky8 jdk]$ tree /data/dockerfile/web/jdk/
/data/dockerfile/web/jdk/
├── Dockerfile
├── jdk-8u202-linux-x64.tar.gz
└── profile
0 directories, 3 files
#制作Dockerfile文件
[root@rocky8 jdk]$ vi Dockerfile
FROM alpine-base:3.17.2
LABEL maintainer="yanlinux"
ENV JDK_VERSION=8u202
ENV JAVA_HOME="/usr/local/jdk"
ENV JRE_HOME="${JAVA_HOME}/jre"
ENV CLASSPATH="$JAVA_HOME/lib/:$JRE_HOME/lib/"
ENV PATH="$PATH:$JAVA_HOME/bin"
ADD jdk-${JDK_VERSION}-linux-x64.tar.gz /usr/local/src/
ADD profile /etc/profile
RUN ln -s /usr/local/src/jdk1.8.0_202 /usr/local/jdk && \
. /etc/profile
#构建镜像
[root@rocky8 jdk]$ docker build -t alpine-jdk:8u202 .
[root@rocky8 jdk]$ docker images alpine-jdk
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine-jdk 8u202 af9682420c5c 29 seconds ago 591MB
#测试镜像
[root@rocky8 jdk]$ docker run -it --rm --name jdk alpine-jdk:8u202 sh
/ # java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
1.3 JDK镜像构建tomcat 8 Base镜像
#准备tomcat包
[root@rocky8 tomcat]$ pwd
/data/dockerfile/web/tomcat
[root@rocky8 tomcat]$ mkdir tomcat-base-8.5.85
[root@rocky8 tomcat]$ cd tomcat-base-8.5.85/
[root@rocky8 tomcat-base-8.5.85]$ pwd
/data/dockfile/web/tomcat/tomcat-base-8.5.85
[root@rocky8 tomcat-base-8.5.85]$ wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.85/bin/apache-tomcat-8.5.85.tar.gz
#编辑Dockerfie文件
[root@rocky8 tomcat-base-8.5.85]$ cat Dockerfile
FROM alpine-jdk:8u202
ENV TZ "Asia/Shanghai"
ENV LANG en_US.UTF-8
ENV TERM xterm
ENV TOMCAT_MAJOR_VERSION 8
ENV TOMCAT_MINOR_VERSION 8.5.85
ENV CATALINA_HOME /apps/tomcat
ENV APP_DIR ${CATALINA_HOME}/webapps
RUN mkdir /apps
ADD apache-tomcat-8.5.85.tar.gz /apps
RUN ln -s /apps/apache-tomcat-8.5.85 /apps/tomcat
#构建镜像
[root@rocky8 tomcat-base-8.5.85]$ docker build -t tomcat-base:v8.5.85 .
[root@rocky8 tomcat-base-8.5.85]$ docker images tomcat-base
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat-base v8.5.85 f5a9ab5c83ab 26 seconds ago 615MB
#验证镜像
[root@rocky8 tomcat-base-8.5.85]$ docker run -it --rm -p 8080:8080 tomcat-base:v8.5.85 bash
bash-5.0# /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.
bash-5.0# ps auxf
PID USER TIME COMMAND
1 root 0:00 bash
11 root 0:01 /usr/local/jdk/jre/bin/java -Djava.util.logging.config.file=/apps/tomcat/conf/log
39 root 0:00 ps auxf
bash-5.0# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN
1.4 构建tomcat web业务镜像
1.4.1 准备tomcat的配置文件
[root@rocky8 tomcat]$ pwd
/data/dockfile/web/tomcat
[root@rocky8 tomcat]$ mkdir tomcat-app{1,2}
[root@rocky8 tomcat]$ tree
.
├── tomcat-app1
├── tomcat-app2
└── tomcat-base-8.5.85
├── apache-tomcat-8.5.85.tar.gz
└── Dockerfile
3 directories, 2 files
#上传和修改server.xml
[root@rocky8 tomcat]$ cd tomcat-base-8.5.85/
[root@rocky8 tomcat-base-8.5.85]$ tar xf apache-tomcat-8.5.85.tar.gz
[root@rocky8 tomcat-base-8.5.85]$ cp apache-tomcat-8.5.85/conf/server.xml /data/dockfile/web/tomcat/tomcat-app1/
[root@rocky8 tomcat-base-8.5.85]$ cd /data/dockfile/web/tomcat/tomcat-app1/
[root@rocky8 tomcat-app1]$ vi server.xml
......
151 <Host name="localhost" appBase="/data/tomcat/webapps"
152 unpackWARs="true" autoDeploy="true">
......
1.4.2 准备自定义页面
[root@rocky8 tomcat-app1]$ mkdir app
[root@rocky8 tomcat-app1]$ echo "Tomcat Page in app1" > app/index.jsp
[root@rocky8 tomcat-app1]$ tar zcf app.tar.gz app
1.4.3 准备容器启动执行脚本
[root@rocky8 tomcat-app1]$ cat run_tomcat.sh
#!/bin/bash
echo "nameserver 180.76.76.76" > /etc/resolv.conf
su - www -c "/apps/tomcat/bin/catalina.sh start"
su - www -c "tail -f /etc/hosts"
[root@rocky8 tomcat-app1]$ chmod +x run_tomcat.sh
1.4.4 准备Dockerfile
[root@rocky8 tomcat-app1]$ cat Dockerfile
FROM tomcat-base:v8.5.85
LABEL maintainer="yanlinux"
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 www.www /apps/ /data/tomcat/
EXPOSE 8080 8009
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
1.4.5 执行构建脚本制作镜像
[root@rocky8 tomcat-app1]$ docker build -t tomcat-web:app1 .
[root@rocky8 tomcat-app1]$ docker images |grep app1
tomcat-web app1 0382417d301a 15 seconds ago 761MB
1.4.6 从镜像启动测试容器
[root@rocky8 tomcat-app1]$ docker run -d -p 8080:8080 tomcat-web:app1
1.4.7 访问测试
[root@rocky8 ~]$ curl 127.0.0.1:8080/app/
Tomcat Page in app1
标签:tomcat,jdk,base,构建,镜像,root,rocky8,85
From: https://www.cnblogs.com/yan-linux/p/17170096.html