1. 镜像和容器在Docker中是两个非常重要的概念,简单来讲,镜像是一些程序和文件的集合,容器是镜像的一个运行实例。Docker为了节约存储空间及共享数据,对镜像和容器进行分层。不同的镜像可以共享相同的数据。例如从同一个基础镜像中生成的子镜像可以共享基础镜像,从同一个镜像启动不同的容器也可以共享这个镜像。Docker为了加快容器的启动速度,在启动容器时,会在镜像层上为容器分配一个可写数据层,在容器运行中,新增,删除、修改的数据都会保存在容器层中
  • 镜像,分层构建(节省空间)
  • 容器启动,新增容器层,所有数据都写在容器层(容器删除,容器层数据丢失)
  • 镜像是一些程序和文件的集合,容器是镜像的一个运行实例(一个镜像,可以启动多个容器)

1)镜像和镜像层

  1. 每个镜像都由多个镜像层组成,镜像层是只读的,从下网上,以栈的方式组合在一起,组成容器的根文件系统,如图<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12595061/1615360881308-44aaa745-fbae-4ba7-a8b2-1e964a35e920.png#align=left&display=inline&height=267&margin=%5Bobject%20Object%5D&name=image.png&originHeight=267&originWidth=314&size=91751&status=done&style=none&width=314)<br /> Docker的存储驱动用于管理这些镜像层,对外提供单一的文件系统。镜像中的层都是只读的,当把镜像启动为容器后,Docker会创建一个可读写的容器层(采用预分配存储空间的方式),容器运行时,所有文件变化的数据都保存在容器层。<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12595061/1615361108835-0e8833e1-6b79-4f13-8e00-7428f135db12.png#align=left&display=inline&height=310&margin=%5Bobject%20Object%5D&name=image.png&originHeight=310&originWidth=563&size=179129&status=done&style=none&width=563)
  • 每个镜像层,分配一个独立的UUID
  • 容器和镜像都是由多个层组成,最大的区别: 容器在最上面一层新增读写层,叫容器层(在容器中所有操作,都在读写层操作)
  • 每个容器运行,都由自己的读写层(容器层之下的所有镜像层都是只读的,因此多个容器可以共享同一个镜像)
  • 使用镜像共享技术,可以减小硬盘大小

2)使用复制技术加快容器启动的时间

当基于某个镜像,启动多个容器,在宿主机中,镜像层只保存一份,每个容器都由自己的读写层(容器层),如图:
image.png
当容器中修改文件时,Docker会通过存储驱动,发起一个写时复制操作。

  • 在镜像层寻找需要修改的数据文件
  • 把这个文件从镜像层复制到容器层中
  • 在容器层修改这个文件

3)介绍overlayfs 和 overlayfs2存储驱动

  1. [root@worker09 ~]# docker info
  2. Client:
  3. Debug Mode: false
  4. Server:
  5. Containers: 62
  6. Running: 56
  7. Paused: 0
  8. Stopped: 6
  9. Images: 269
  10. Server Version: 19.03.5
  11. Storage Driver: overlay2
  12. Backing Filesystem: extfs
  13. Supports d_type: true
  14. Native Overlay Diff: true
  15. Logging Driver: json-file

3.1)overlay存储驱动

  • 两个目录:使用分层结构,一个目录在下层,保存 镜像层,另一个目录在上层,保存容器层,overlayfs把这两个目录组合在一起,对外提供一个文件系统
  • overlayfs使用联合挂载技术组合这两个目录,底层目录: lowerdir ,顶层目录: upperdir,对外提供统一的文件系统叫merged
  • overlay存储驱动只能使用两层,每个镜像层在/var/lib/docker/overlay都由对应的目录
  • 运行容器时,存储驱动会把所有镜像层对应的目录组合起来,在上层添加一个容器层

image.png

3.2)overlay2存储驱动
overlay2支持多个lowerdir,保存多层镜像更有优势;overlay2使用更少的inode,默认在/var/lib/docker/overlay2

  1. 我更改了docker的存储位置
  2. [root@blog-xhaihua ~]# cd /data/docker/overlay2/
  3. [root@blog-xhaihua overlay2]# ll
  4. total 4
  5. drwx------ 4 root root 72 Oct 16 14:45 006f20ff6bc78d97a269b9cd754b7ed37467dc44a98685d8e3c3e6e9be2abc24
  6. drwx------ 5 root root 69 Mar 9 08:18 00dabc3a28c2f96a7e6de0f74bf77a333540897d388ac4bb90b98d77aeecea86
  7. drwx------ 4 root root 72 Mar 5 15:04 00dabc3a28c2f96a7e6de0f74bf77a333540897d388ac4bb90b98d77aeecea86-init
  8. drwx------ 4 root root 72 Oct 16 14:43 00de57d1613d257fcc248939b049ff1c07b8a8b4f4090b6b50a525346631d206
  9. drwx------ 4 root root 72 Oct 16 14:45 0b97366575709f23329ca395264df7b0d7e9bf4201c9fd63936b8f88a75f3a21
  10. drwx------ 4 root root 72 Oct 16 14:45 14c24de11d235fea4821b7590c10426ee61ca05af9b1c1753957eafc6cb67192
  • /var/lib/docker/overlay2中的l目录保存的镜像层的短标识,每个短标识都是一个符号链接
  • 最底层的镜像层包含了link文件,diff目录包含了镜像层的内容 ```python [root@blog-xhaihua overlay2]# cat 006f20ff6bc78d97a269b9cd754b7ed37467dc44a98685d8e3c3e6e9be2abc24/link RYNALN6HQ55RBPTXPLXTBZSI4R [root@blog-xhaihua overlay2]# ls 006f20ff6bc78d97a269b9cd754b7ed37467dc44a98685d8e3c3e6e9be2abc24/diff bin etc lib tmp usr var
  1. OverLayFS结构分为三个层: LowerDirUpperDirMergedDir
  2. - LowerDir(只读): 只读的image layer,其实就是rootfs。在使用dockerfile构建镜像的时候,image layer可以分很多层,所以对应的lowerdir会很多(源镜像 FROM
  3. - Upperdir(读写): upperdir则在lowerdir之上的一层,为读写层。容器在启动的时候会创建,所有容器的修改,都会在这层。
  4. - MergedDir(展示层): merged目录是容器的挂载点,在用户视角能够看见的所有文件
  5. ![](https://cdn.nlark.com/yuque/0/2021/png/12595061/1615363211918-028ddf9a-5bbc-4134-b4c4-309693af8303.png#align=left&display=inline&height=780&margin=%5Bobject%20Object%5D&originHeight=780&originWidth=1363&size=0&status=done&style=none&width=1363)
  6. <a name="NJVru"></a>
  7. ### 4) 镜像操作基础命令
  8. 镜像结构标识: ${register_name}/${reposttory_name}/${image_name}:${tag_name}
  9. ```shell
  10. 镜像的结构:
  11. ${register_name}/${reposttory_name}/${image_name}:${tag_name}
  12. 例如:
  13. docker.io/library/apline:3.10.1
  14. #docker.io
  15. 远程仓库地址
  16. #library
  17. 仓库分类的名称
  18. #apline
  19. 镜像名称
  20. #3.10.1
  21. 版本号 (tag)
  22. 如何标识一个镜像:
  23. 1)通过镜像名:tag 例如: ubuntu:16.04
  24. 2) 通过IMAGE ID号
  25. [root@blog-server merged]# docker image ls
  26. REPOSITORY TAG IMAGE ID CREATED SIZE
  27. ubuntu 16.04 096efd74bb89 3 weeks ago 127MB
  • 列出镜像 ```shell docker images

docker image ls

  1. ```shell
  2. 我们可以使用 docker images 来列出本地主机上的镜像。
  3. runoob@runoob:~$ docker images
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. ubuntu 14.04 90d5884b1ee0 5 days ago 188 MB
  6. php 5.6 f40e9e0f10c8 9 days ago 444.8 MB
  7. nginx latest 6f8d099c3adc 12 days ago 182.7 MB
  8. mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
  9. httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
  10. ubuntu 15.10 4e3b13c8a266 4 weeks ago 136.3 MB
  11. hello-world latest 690ed74de00f 6 months ago 960 B
  12. training/webapp latest 6fae60ef3446 11 months ago 348.8 MB
  13. 各个选项说明:
  14. REPOSITORY:表示镜像的仓库源
  15. TAG:镜像的标签
  16. IMAGE ID:镜像ID
  17. CREATED:镜像创建时间
  18. SIZE:镜像大小
  • 拉取镜像

    1. docker pull 拉取镜像 (旧命令)
    2. docker image pull 拉取镜像(新命令)
  • 推送镜像

    1. docker push 拉取镜像 (旧命令)
    2. docker image push 拉取镜像(新命令)
  • 删除镜像

    1. docker rmi 镜像ID (旧命令)
    2. docker image rmi 镜像ID(新命令)
  • 通过修改容器来创建镜像

    1. docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] (旧命令)
    2. docker image commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] (新命令)
  • 构建镜像

    1. docker tag 原镜像 新镜像 (镜像打标,推送到私有镜像仓库)
    2. docker build -t ${register_name}/${reposttory_name}/${image_name}:${tag_name} . 基于dockerfile构建镜像
    1. 设置镜像标签,我们可以使用 docker tag 命令,为镜像添加一个新的标签。
    2. docker tag 860c279d2fec runoob/centos:dev

    5)Dockerfile(重点)

    **
    5.1)dockerignore (类似于gitignore)

.dockerignore 应用于 docker 镜像的构建,它存在于 docker 构建上下文的根目录,用来排除不需要上传到 docker 服务端的文件或目录
好处是:

  • 构建镜像时能避免不需要的大文件上传到服务端,从而拖慢构建的速度、网络带宽的消耗;
  • 可以避免构建镜像时将一些敏感文件及其他不需要的文件打包到镜像中,从而提高镜像的安全性;

.dockerignore 文件编写方法
.dockerignore 文件的写法和 .gitignore 类似,支持正则和通配符,具体规则如下:

  • 每行为一个条目;
  • # 开头的行为注释;
  • 空行被忽略;
  • 构建上下文路径为所有文件的根路径;


    文件匹配规则具体语法如下:
    三、Docker镜像原理及镜像制作 - 图3

5.2)为什么要使用dockerfile构建镜像

  • 抑郁版本化管理,dockerfile本身是一个文本文件,文件可以放到代码仓库做版本管理
  • 过程可以追溯,dockerfile的每一行指令代理一个镜像层,根据dockerfile的内容可以明确的查看镜像的完整构建过程
  • 屏蔽构建环境,使用dockerfile构建镜像无需考虑构建环境,相同的dockerfile无论在哪里运行,构建结构都一样

5.3) Dockerfile 书写规则

  • 单一职责,由于容器的本质就是进程,一个容器代表一个进程,因此不同功能的应用应该尽量拆分成不同容器,每个容器只负责单一业务进程
  • 提供注释信息
  • 保持容器最小化,避免安装无用的软件包
  • 合理的选择基础镜像,容器的运行时运行业务应用,因此只要基础镜像满足应用的运行环境即可
  • 尽量使用构建缓存(默认的)
  • 正确设置时区 针对Centos系统(RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime)
  • 使用国内软件源加快镜像构建速度
  • 最小化镜像层,能合在一起的,就写在一起,比如RUN命令等

5.4) Dockerfile常用的指令

  • FROM指令 ( 指定基础镜像,并且必须是第一条指令) ```shell FROM 三、Docker镜像原理及镜像制作 - 图4 FROM 三、Docker镜像原理及镜像制作 - 图5: FROM 三、Docker镜像原理及镜像制作 - 图6:

说明: 其中 是可选项,如果没有选择,那么默认值为latest

  1. - LABEL指令 为镜像指定标签)
  2. ```shell
  3. 语法: LABEL <key>=<value> <key>=<value> <key>=<value> ...
  4. LABLE maintainer="xionghaihua <xhh_198605@163.com>"
  5. LABEL version="1.0"
  6. LABEL description="centos base images"
  • ENV指令 (设置镜像中的环境变量) ```shell ENV 一次设置一个环境变量 ENV = ….. 一次可以设置多个(常用)

通过$var使用变量

${var:-word} 如果没有定义var,就使用word作为默认值

  1. - EXPOSE (设置镜像需要暴露的端口,记录容器启动时监听在哪些端口)
  2. ```shell
  3. EXPOSE <port>
  4. 当容器启动时,Docker daemon会扫描镜像中的暴露端口,如果docker run 加入 -P参数,Docker Daemon会把镜像中的所有
  5. 暴露端口全部导出,并为每个暴露端口分配一个随机的主机端口,暴露端口就是容器的监听端口,主机的导出端口就是外部访问容器的端口
  • RUN ( 在镜像里,运行指定的命令)

    1. RUN <command> (常用)
    2. RUN ["executable", "param1", "param2"]

    第一种: shell方式,使用/bin/sh -c 命令运行脚本
    第二种: 通过exec方式,镜像中没有/bin/sh 如 RUN [“/bin/bash”,”-c”,”echo hello”]

    1. RUN [ "echo","$HOME"] 不会读取环境变量HOME,如果要调用shell,则必须使用 RUN ["sh","-c","echo","$HOME"]
  • CMD 和 ENTRYPOINT指令 (容器的启动命令)

    Dockerfile中只能由一条CMD命令,如果写了多条CMD指令,那只有最后一条CMD指令生效

    生产建议: 使用ENTRYPOINT指令,结合CMD指令,因为更加灵活

    1. CMD/ENTRYPOINT ["command","param"] #exec模式
    2. CMD/ENTRYPOINT comand param #shell模式

    ```shell CMD 和 ENTRYPOINT区别:

如果Dockerfile中使用了ENTRYPOINT指令,启动容器时候,可以使用—entrypoint参数覆盖dockerfile中的entrypoint指令

如果Dockerfile中使用的CMD指令,可以被docker run后面的参数直接覆盖

ENTRYPOINT指令可以结合CMD指令使用,也可以单独使用,而CMD指令只能单独使用

建议: 使用ENTRYPOINT指令,结合CMD指令,因为更加灵活

  1. ```shell
  2. CMD [ "echo","$HOME"] 不会读取环境变量HOME,如果要调用shell,则必须使用 CMD ["sh","-c","echo","$HOME"]
  • ADD (把文件复制到镜像中) ```shell ADD ADD [““,… ““]

src 本地文件、目录 《支持多个》 本地压缩包 (nginx.tar.gz),ADD到镜像里,会自动解压 ,COPY指令则不会 网上的url下载地址,不会自动解压

dest 如果src是多个文件或者目录,dest必须是目录 dest目录是容器内的绝对路径,不必要事先存在

  1. ADD指令必须遵循以下原则:
  2. - src必须在编译目录中
  3. - srcurl地址, dest结尾需要以/结尾
  4. - 如果src为目录,则复制目录内所有内容,不复制目录本身
  5. - 如果src是压缩文件,会自动解压
  6. - 如果src使用通配符,是一个文件列表,然后dest必须以/结尾
  7. - 如果dest不存在,则ADD会自动创建dest缺失的上级目录
  8. - COPY (把文件复制到镜像)
  9. ```shell
  10. COPY <src>... <dest>
  11. COPY ["<src>",... "<dest>"]
  12. 当路径中间由空格,需要使用COPY ["<src>",... "<dest>"]

COPY指令必须遵循以下原则:

  • src必须在编译目录中
  • 如果src为目录,则复制目录内所有内容,不复制目录本身
  • 如果src使用通配符,是一个文件列表,然后dest必须以/结尾
  • 如果dest不存在,则COPY会自动创建dest缺失的上级目录

  • VOLUME (设置容器挂载点) ```shell volume [“/data”] volume /data1 /data2

Docker Daemon会把主机目录挂载到这些挂载点,把容器中的目录共享到宿主机上

  1. - ARG (设置编译变量)
  2. ```shell
  3. ARG <name>=<value>
  4. 编译镜像时,通过 docker build --build-arg <var>=<value> -t 镜像名 .

6)镜像构建案例

6.1) Centos基础镜像

  1. [root@blog-xhaihua centos7]# cat Dockerfile
  2. FROM centos:7.6.1810
  3. MAINTAINER 851628816@qq.com
  4. # 设置本地时区
  5. RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  6. && cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak \
  7. && sed -e 's|^mirrorlist=|#mirrorlist=|g' \
  8. -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
  9. -i.bak \
  10. /etc/yum.repos.d/CentOS-*.repo \
  11. && yum install epel-release -y \
  12. && sed -e 's!^metalink=!#metalink=!g' \
  13. -e 's!^#baseurl=!baseurl=!g' \
  14. -e 's!//download\.fedoraproject\.org/pub!//mirrors.tuna.tsinghua.edu.cn!g' \
  15. -e 's!http://mirrors\.tuna!https://mirrors.tuna!g' \
  16. -i /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo \
  17. && yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
  1. #!/bin/bash
  2. docker build -t registry.xhaihua.cn/baseimages/centos-base:7.6.1810-v1 .

6.2) ubuntu18.04基础镜像

  1. [root@blog-xhaihua ubuntu1804]# cat Dockerfile
  2. FROM ubuntu:18.04
  3. MAINTAINER xhaihua 851628816@qq.com
  4. RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list \
  5. && apt-get update -y \
  6. && apt-get -y install vim wget iproute2 tzdata net-tools iputils-ping \
  7. && echo "Asia/Shanghai" > /etc/timezone \
  8. && rm -f /etc/localtime
  1. #!/bin/bash
  2. docker build -t registry.xhaihua.cn/baseimages/ubuntu:18.04-v1 .

6.3) Java1.8基础镜像 (以centos7和ubuntu18.04)

  1. FROM registry.xhaihua.cn/baseimages/centos-base:7.6.1810-v1
  2. MAINTAINER xhaihua 851628816@qq.com
  3. RUN cd /usr/local/src \
  4. && wget http://download.51yuki.cn/jdk-8u211-linux-x64.tar.gz \
  5. && tar xf jdk-8u211-linux-x64.tar.gz \
  6. && ln -sv /usr/local/src/jdk1.8.0_211 /usr/local/jdk \
  7. && rm -rf /usr/local/src/jdk-8u211-linux-x64.tar.gz \
  8. && rm -rf /etc/localtime \
  9. && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  10. && echo "Asia/Shanghai" > /etc/timezone
  11. RUN yum install kde-l10n-Chinese glibc-common -y \
  12. && localedef -c -f UTF-8 -i zh_CN zh_CN.utf8 \
  13. && echo "export LANG=zh_CN.UTF-8" >> /etc/locale.conf
  14. ENV LC_ALL zh_CN.UTF-8
  15. ENV JAVA_HOME /usr/local/jdk
  16. ENV JRE_HOME $JAVA_HOME/jre
  17. ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
  18. ENV PATH $PATH:$JAVA_HOME/bin
  1. FROM registry.xhaihua.cn/baseimages/ubuntu:18.04-v1
  2. MAINTAINER xhaihua 851628816@qq.com
  3. RUN cd /usr/local/src \
  4. && wget http://download.51yuki.cn/jdk-8u211-linux-x64.tar.gz \
  5. && tar xf jdk-8u211-linux-x64.tar.gz \
  6. && ln -sv /usr/local/src/jdk1.8.0_211 /usr/local/jdk \
  7. && rm -rf /usr/local/src/jdk-8u211-linux-x64.tar.gz
  8. ENV JAVA_HOME /usr/local/jdk
  9. ENV JRE_HOME $JAVA_HOME/jre
  10. ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
  11. ENV PATH $JAVA_HOME/bin:$PATH
  1. #!/bin/bash
  2. docker build -t registry.xhaihua.cn/baseimages/java:1.8.211-v1 .

6.4)python3环境 (基于ubuntu系统)

  1. FROM registry.xhaihua.cn/baseimages/ubuntu:18.04-v1
  2. MAINTAINER xhaihua 851628816@qq.com
  3. RUN apt-get update \
  4. && apt-get -y install python3 python3-dev python3-pip \
  5. && pip3 install -U numpy matplotlib pandas seaborn scikit-learn \
  6. && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  7. && echo "Asia/Shanghai" > /etc/timezone \
  8. && apt-get clean \
  9. && apt-get autoclean \
  10. && rm -rf /var/lib/apt/lists/*
  11. ENV LC_ALL C.UTF-8
  12. ENV LANG en_US.UTF-8
  1. #!/bin/bash
  2. docker build -t registry.xhaihua.cn/baseimages/python3-v1 .

6.5) tomcat8.5基础镜像(基于Ubuntu系统)

run_tomcat.sh

  1. #!/bin/bash
  2. echo "nameserver 114.114.114.114" > /etc/resolv.conf
  3. su - tomcat -c "/apps/tomcat/bin/catalina.sh run"

server.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <!-- Note: A "Server" is not itself a "Container", so you may not
  17. define subcomponents such as "Valves" at this level.
  18. Documentation at /docs/config/server.html
  19. -->
  20. <Server port="8005" shutdown="SHUTDOWN">
  21. <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  22. <!-- Security listener. Documentation at /docs/config/listeners.html
  23. <Listener className="org.apache.catalina.security.SecurityListener" />
  24. -->
  25. <!--APR library loader. Documentation at /docs/apr.html -->
  26. <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  27. <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  28. <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  29. <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  30. <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  31. <!-- Global JNDI resources
  32. Documentation at /docs/jndi-resources-howto.html
  33. -->
  34. <GlobalNamingResources>
  35. <!-- Editable user database that can also be used by
  36. UserDatabaseRealm to authenticate users
  37. -->
  38. <Resource name="UserDatabase" auth="Container"
  39. type="org.apache.catalina.UserDatabase"
  40. description="User database that can be updated and saved"
  41. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
  42. pathname="conf/tomcat-users.xml" />
  43. </GlobalNamingResources>
  44. <!-- A "Service" is a collection of one or more "Connectors" that share
  45. a single "Container" Note: A "Service" is not itself a "Container",
  46. so you may not define subcomponents such as "Valves" at this level.
  47. Documentation at /docs/config/service.html
  48. -->
  49. <Service name="Catalina">
  50. <!--The connectors can use a shared executor, you can define one or more named thread pools-->
  51. <!--
  52. <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
  53. maxThreads="150" minSpareThreads="4"/>
  54. -->
  55. <!-- A "Connector" represents an endpoint by which requests are received
  56. and responses are returned. Documentation at :
  57. Java HTTP Connector: /docs/config/http.html
  58. Java AJP Connector: /docs/config/ajp.html
  59. APR (HTTP/AJP) Connector: /docs/apr.html
  60. Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
  61. -->
  62. <Connector port="8080" protocol="HTTP/1.1"
  63. connectionTimeout="20000"
  64. redirectPort="8443" />
  65. <!-- A "Connector" using the shared thread pool-->
  66. <!--
  67. <Connector executor="tomcatThreadPool"
  68. port="8080" protocol="HTTP/1.1"
  69. connectionTimeout="20000"
  70. redirectPort="8443" />
  71. -->
  72. <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
  73. This connector uses the NIO implementation. The default
  74. SSLImplementation will depend on the presence of the APR/native
  75. library and the useOpenSSL attribute of the
  76. AprLifecycleListener.
  77. Either JSSE or OpenSSL style configuration may be used regardless of
  78. the SSLImplementation selected. JSSE style configuration is used below.
  79. -->
  80. <!--
  81. <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
  82. maxThreads="150" SSLEnabled="true">
  83. <SSLHostConfig>
  84. <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
  85. type="RSA" />
  86. </SSLHostConfig>
  87. </Connector>
  88. -->
  89. <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
  90. This connector uses the APR/native implementation which always uses
  91. OpenSSL for TLS.
  92. Either JSSE or OpenSSL style configuration may be used. OpenSSL style
  93. configuration is used below.
  94. -->
  95. <!--
  96. <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
  97. maxThreads="150" SSLEnabled="true" >
  98. <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
  99. <SSLHostConfig>
  100. <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
  101. certificateFile="conf/localhost-rsa-cert.pem"
  102. certificateChainFile="conf/localhost-rsa-chain.pem"
  103. type="RSA" />
  104. </SSLHostConfig>
  105. </Connector>
  106. -->
  107. <!-- Define an AJP 1.3 Connector on port 8009 -->
  108. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
  109. <!-- An Engine represents the entry point (within Catalina) that processes
  110. every request. The Engine implementation for Tomcat stand alone
  111. analyzes the HTTP headers included with the request, and passes them
  112. on to the appropriate Host (virtual host).
  113. Documentation at /docs/config/engine.html -->
  114. <!-- You should set jvmRoute to support load-balancing via AJP ie :
  115. <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
  116. -->
  117. <Engine name="Catalina" defaultHost="localhost">
  118. <!--For clustering, please take a look at documentation at:
  119. /docs/cluster-howto.html (simple how to)
  120. /docs/config/cluster.html (reference documentation) -->
  121. <!--
  122. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
  123. -->
  124. <!-- Use the LockOutRealm to prevent attempts to guess user passwords
  125. via a brute-force attack -->
  126. <Realm className="org.apache.catalina.realm.LockOutRealm">
  127. <!-- This Realm uses the UserDatabase configured in the global JNDI
  128. resources under the key "UserDatabase". Any edits
  129. that are performed against this UserDatabase are immediately
  130. available for use by the Realm. -->
  131. <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
  132. resourceName="UserDatabase"/>
  133. </Realm>
  134. <Host name="localhost" appBase="/data/tomcat/webapps" unpackWARs="true" autoDeploy="true">
  135. <!-- SingleSignOn valve, share authentication between web applications
  136. Documentation at: /docs/config/valve.html -->
  137. <!--
  138. <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
  139. -->
  140. <!-- Access log processes all example.
  141. Documentation at: /docs/config/valve.html
  142. Note: The pattern used is equivalent to using pattern="common" -->
  143. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
  144. prefix="localhost_access_log" suffix=".txt"
  145. pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  146. </Host>
  147. </Engine>
  148. </Service>
  149. </Server>
  1. FROM registry.xhaihua.cn/baseimages/ubuntu:18.04-v1
  2. MAINTAINER xhaihua 851628816@qq.com
  3. RUN mkdir /apps && wget https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-8/v8.5.63/bin/apache-tomcat-8.5.63.tar.gz \
  4. && tar xf apache-tomcat-8.5.63.tar.gz -C /apps \
  5. && rm -rf apache-tomcat-8.5.63.tar.gz \
  6. && ln -sv /apps/apache-tomcat-8.5.33 /apps/tomcat \
  7. && mkdir -p /data/tomcat/webapps/webroot \
  8. && useradd tomcat -u 1001
  9. ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
  10. ADD server.xml /apps/tomcat/conf/server.xml
  11. RUN chown -R tomcat.tomcat /apps/tomcat /data/tomcat/webapps
  1. #!/bin/bash
  2. docker build -t registry.xhaihua.cn/baseimages/tomcat:8.5-v1 .

6.6) nginx基础镜像