最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

基于alpine基础镜像构建jdk镜像以及tomcat镜像及业务构建

来源:博客园


【资料图】

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 <

1.2 基于自定义alpine镜像制作jdk镜像

#从alpine容器中拷贝一份/etc/profile文件[root@rocky8 jdk]$ docker run -it --rm alpine-base:3.17.2 sh[root@rocky8 jdk]$ docker psCONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMESae3595b2d8cf        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/jdkexport TOMCAT_HOME=/apps/tomcatexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$TOMCAT_HOME/bin:$PATHexport 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└── profile0 directories, 3 files#制作Dockerfile文件[root@rocky8 jdk]$ vi DockerfileFROM alpine-base:3.17.2LABEL maintainer="yanlinux"ENV JDK_VERSION=8u202ENV 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/profileRUN 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-jdkREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEalpine-jdk          8u202               af9682420c5c        29 seconds ago      591MB#测试镜像[root@rocky8 jdk]$ docker run -it --rm --name jdk alpine-jdk:8u202 sh/ # java  -versionjava 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:8u202ENV TZ "Asia/Shanghai"ENV LANG en_US.UTF-8ENV TERM xtermENV TOMCAT_MAJOR_VERSION 8ENV TOMCAT_MINOR_VERSION 8.5.85ENV CATALINA_HOME /apps/tomcatENV APP_DIR ${CATALINA_HOME}/webappsRUN mkdir /appsADD apache-tomcat-8.5.85.tar.gz /appsRUN 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-baseREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEtomcat-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 bashbash-5.0# /apps/tomcat/bin/catalina.sh startUsing CATALINA_BASE:   /apps/tomcatUsing CATALINA_HOME:   /apps/tomcatUsing CATALINA_TMPDIR: /apps/tomcat/tempUsing JRE_HOME:        /usr/local/jdk/jreUsing CLASSPATH:       /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jarUsing CATALINA_OPTS:   Tomcat started.bash-5.0# ps auxfPID   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 auxfbash-5.0# netstat -ntlActive 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    └── Dockerfile3 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       ......

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/bashecho "nameserver 180.76.76.76" > /etc/resolv.confsu - 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.85LABEL maintainer="yanlinux"ADD server.xml /apps/tomcat/conf/server.xmlADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.shADD app.tar.gz /data/tomcat/webapps/RUN chown -R www.www /apps/ /data/tomcat/EXPOSE 8080 8009CMD ["/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 app1tomcat-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

关键词: 操作系统 配置文件