Dockerfile是用来构建docker镜像的构建文件,是由一系列命令和参数构成的脚本
构建三步骤:
1、编写dockerfile文件
2、docker build 镜像
3、docker run 容器
1、Dockerfile内容基础知识
①每条保留字指令都必须为大写字母且后面要跟随至少一个参数
②指令按照从上到下,顺序执行
③#表示注释
④每条指令都会创建一个新的镜像层,并对镜像进行提交
2、Docker执行Dockerfile的大致流程
①docker从基础镜像运行一个容器
②执行一条指令并对容器做出修改
③执行类似docker commit的操作提交一个新的镜像层
④docker再基于刚提交的镜像运行一个新容器
⑤执行dockerfile中的下一条指令直到所有指令都执行完成
3、总结
从应用软件的角度来看,dockerfile、docker镜像与docker容器分别代表软件的三个不同阶段,
dockerfile是软件的原材料
docker镜像是软件的交付品
docker容器则可以认为是软件的运行态。
Dockerfile面向开发,Docker镜像成为交付标准,Docker容器则涉及部署与运维,三者缺一不可,合力充当Docker体系的基石。
Dockerfile,需要定义一个Dockerfile,Dockerfile定义了进程需要的一切东西。Dockerfile涉及的内容包括执行代码活着是文件、环境变量、依赖包、运行时环境、动态链接库、操作系统的发行版、服务进程和内核进程(当应用进程需要和系统服务和内核进程打交道,这时需要考虑如何设计namespace的权限控制)等等。
Docker镜像,在用dockerfile定义一个文件后,docker build时会产生一个Docker镜像,当运行Docker镜像时,会真正开始提供服务。
Docker容器,容器是直接提供服务的。
Base镜像(scratch) Docker Hub中99%的镜像都是通过在base镜像中安装和配置需要的软件构建出来的
自定义镜像mycentos:
1、编写
Hub默认Centos镜像什么情况
[root@hadoop101 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ghj/centos latest ea55868fb6d4 22 hours ago 202MB
atdixin/mytomcat 7.0 38d1e8f04740 3 days ago 508MB
nginx latest f949e7d76d63 5 days ago 126MB
tomcat latest 8973f493aa0a 9 days ago 508MB
centos latest 67fa590cfc1c 5 weeks ago 202MB
hello-world latest fce289e99eb9 9 months ago 1.84kB
training/webapp latest 6fae60ef3446 4 years ago 349MB
[root@hadoop101 /]# docker run -it centos /bin/bash
[root@89f6f4831dd9 /]# pwd
/
[root@89f6f4831dd9 /]# vim abc.txt
bash: vim: command not found
[root@89f6f4831dd9 /]# ifconfig
bash: ifconfig: command not found
①初始centos运行该镜像进入默认路径是/
②默认不支持vim和ifconfig
自定义mycentos目的使我们自己的镜像具备如下:
①登陆后的默认路径
②支持vim编辑器和查看网络配置ifconfig支持
准备编写DockerFile文件
[root@hadoop101 /]# cd mydocker/
[root@hadoop101 mydocker]# ll
总用量 4
-rw-r--r--. 1 root root 133 9月 29 10:43 Dockerfile
[root@hadoop101 mydocker]# vim Dockerfile2
------------------------------------------------------------------------------------------------
FROM centos
MAINTAINER ghj.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "success-------ok"
CMD /bin/bash
myCentos内容DockerFile
2、构建
docker build -t 新镜像名字:TAG.
注意最后有一个. 表示当前目录
[root@hadoop101 mydocker]# docker build -f /mydocker/Dockerfile2 -t mycentos:1.3 .
Successfully built d9dff72321ec
Successfully tagged mycentos:1.3
[root@hadoop101 mydocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.3 d9dff72321ec 13 minutes ago 350MB
ghj/centos latest ea55868fb6d4 30 hours ago 202MB
atdixin/mytomcat 7.0 38d1e8f04740 4 days ago 508MB
nginx latest f949e7d76d63 5 days ago 126MB
tomcat latest 8973f493aa0a 10 days ago 508MB
centos latest 67fa590cfc1c 5 weeks ago 202MB
hello-world latest fce289e99eb9 9 months ago 1.84kB
training/webapp latest 6fae60ef3446 4 years ago 349MB
3、运行
[root@hadoop101 ~]# docker run -it mycentos:1.3
[root@12fb3443ef0f local]# pwd
/usr/local
此时运行容器可以发现,我们通过DockerFile文件配置的ENV 的路径就是运行容器直接进入的路径
4、列出镜像的变更历史
[root@hadoop101 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.3 d9dff72321ec 7 days ago 350MB
ghj/centos latest ea55868fb6d4 9 days ago 202MB
atdixin/mytomcat 7.0 38d1e8f04740 11 days ago 508MB
nginx latest f949e7d76d63 13 days ago 126MB
tomcat latest 8973f493aa0a 2 weeks ago 508MB
centos latest 67fa590cfc1c 6 weeks ago 202MB
hello-world latest fce289e99eb9 9 months ago 1.84kB
training/webapp latest 6fae60ef3446 4 years ago 349MB
[root@hadoop101 ~]# docker history d9dff72321ec
IMAGE CREATED CREATED BY SIZE COMMENT
d9dff72321ec 7 days ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
3abd63b182b2 7 days ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
8619d9c27954 7 days ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
88c714721366 7 days ago /bin/sh -c #(nop) EXPOSE 80 0B
18070c333f54 7 days ago /bin/sh -c yum -y install net-tools 23.7MB
53d10d80a787 7 days ago /bin/sh -c yum -y install vim 124MB
73e7f7cff11c 7 days ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
249da334797f 7 days ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
15c91369a1cc 7 days ago /bin/sh -c #(nop) MAINTAINER ghj
67fa590cfc1c 6 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
6 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
6 weeks ago /bin/sh -c #(nop) ADD file:4e7247c06de9ad117… 202MB
很明显是根据DockerFile的配置文件逐层加载的。
CMD/ENTRYPOINT镜像案例
都是指定一个容器启动时要运行的命令
CMD:dockerfile中可以有多个CMD指令,但只有最后一个生效,CMD会被docker run 之后的参数替换
Case
在Docker Hub上找到tomcat的DockerFile文件如下
FROM adoptopenjdk:8-jdk-openj9
ENV CATALINA_HOME /usr/local/tomcat
ENV PATH $CATALINA_HOME/bin:$PATH
RUN mkdir -p "$CATALINA_HOME"
WORKDIR $CATALINA_HOME
# let "Tomcat Native" live somewhere isolated
ENV TOMCAT_NATIVE_LIBDIR $CATALINA_HOME/native-jni-lib
ENV LD_LIBRARY_PATH ${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$TOMCAT_NATIVE_LIBDIR
# see https://www.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/KEYS# see also "update.sh" (https://github.com/docker-library/tomcat/blob/master/update.sh)ENV GPG_KEYS 05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 713DA88BE50911535FE716F5208B0AB1D63011C7 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23
ENV TOMCAT_MAJOR 8
ENV TOMCAT_VERSION 8.5.46
ENV TOMCAT_SHA512 9d6243ec47ec0f431c55a612fa6a8fac00262ed2731640ad98628b275221d3e8e241b2fee748196b64029997f4d9f8e63831b43986fedb88a62381a92b05ca68
RUN set -eux; \ \ savedAptMark="$(apt-mark showmanual)"; \ apt-get update; \ apt-get install -y --no-install-recommends \ gnupg dirmngr \ wget ca-certificates \ ; \ \ ddist() { \ local f="$1"; shift; \ local distFile="$1"; shift; \ local success=; \ local distUrl=; \ for distUrl in \# https://issues.apache.org/jira/browse/INFRA-8753?focusedCommentId=14735394#comment-14735394 'https://www.apache.org/dyn/closer.cgi?action=download&filename=' \# if the version is outdated (or we're grabbing the .asc file), we might have to pull from the dist/archive :/ https://www-us.apache.org/dist/ \ https://www.apache.org/dist/ \ https://archive.apache.org/dist/ \ ; do \ if wget -O "$f" "$distUrl$distFile" && [ -s "$f" ]; then \ success=1; \ break; \ fi; \ done; \ [ -n "$success" ]; \ }; \ \ ddist 'tomcat.tar.gz' "tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz"; \ echo "$TOMCAT_SHA512 *tomcat.tar.gz" | sha512sum --strict --check -; \ ddist 'tomcat.tar.gz.asc' "tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz.asc"; \ export GNUPGHOME="$(mktemp -d)"; \ for key in $GPG_KEYS; do \ gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \ done; \ gpg --batch --verify tomcat.tar.gz.asc tomcat.tar.gz; \ tar -xf tomcat.tar.gz --strip-components=1; \ rm bin/*.bat; \ rm tomcat.tar.gz*; \ command -v gpgconf && gpgconf --kill all || :; \ rm -rf "$GNUPGHOME"; \ \ nativeBuildDir="$(mktemp -d)"; \ tar -xf bin/tomcat-native.tar.gz -C "$nativeBuildDir" --strip-components=1; \ apt-get install -y --no-install-recommends \ dpkg-dev \ gcc \ libapr1-dev \ libssl-dev \ make \ ; \ ( \ export CATALINA_HOME="$PWD"; \ cd "$nativeBuildDir/native"; \ gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \ aprConfig="$(command -v apr-1-config)"; \ ./configure \ --build="$gnuArch" \ --libdir="$TOMCAT_NATIVE_LIBDIR" \ --prefix="$CATALINA_HOME" \ --with-apr="$aprConfig" \ --with-java-home="$JAVA_HOME" \ --with-ssl=yes; \ make -j "$(nproc)"; \ make install; \ ); \ rm -rf "$nativeBuildDir"; \ rm bin/tomcat-native.tar.gz; \ \# reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies apt-mark auto '.*' > /dev/null; \ [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \ find "$TOMCAT_NATIVE_LIBDIR" -type f -executable -exec ldd '{}' ';' \ | awk '/=>/ { print $(NF-1) }' \ | sort -u \ | xargs -r dpkg-query --search \ | cut -d: -f1 \ | sort -u \ | xargs -r apt-mark manual \ ; \ apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ rm -rf /var/lib/apt/lists/*; \ \# sh removes env vars it doesn't support (ones with periods)# https://github.com/docker-library/tomcat/issues/77 find ./bin/ -name '*.sh' -exec sed -ri 's|^#!/bin/sh$|#!/usr/bin/env bash|' '{}' +; \ \# fix permissions (especially for running as non-root)# https://github.com/docker-library/tomcat/issues/35 chmod -R +rX .; \ chmod 777 logs temp work
# verify Tomcat Native is working properly
RUN set -e \ && nativeLines="$(catalina.sh configtest 2>&1)" \ && nativeLines="$(echo "$nativeLines" | grep 'Apache Tomcat Native')" \ && nativeLines="$(echo "$nativeLines" | sort -u)" \ && if ! echo "$nativeLines" | grep 'INFO: Loaded APR based Apache Tomcat Native library' >&2; then \ echo >&2 "$nativeLines"; \ exit 1; \ fi
EXPOSE 8080
CMD ["catalina.sh", "run"]
[root@hadoop101 ~]# docker images tomcat
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 8973f493aa0a 2 weeks ago 508MB
[root@hadoop101 ~]# docker run -it -p 7777:8080 tomcat ls -l
total 124
-rw-r--r--. 1 root root 19318 Sep 16 18:19 BUILDING.txt
-rw-r--r--. 1 root root 5407 Sep 16 18:19 CONTRIBUTING.md
-rw-r--r--. 1 root root 57011 Sep 16 18:19 LICENSE
-rw-r--r--. 1 root root 1726 Sep 16 18:19 NOTICE
-rw-r--r--. 1 root root 3255 Sep 16 18:19 README.md
-rw-r--r--. 1 root root 7139 Sep 16 18:19 RELEASE-NOTES
-rw-r--r--. 1 root root 16262 Sep 16 18:19 RUNNING.txt
drwxr-xr-x. 2 root root 4096 Sep 20 01:40 bin
drwxr-sr-x. 2 root root 238 Sep 16 18:19 conf
drwxr-sr-x. 2 root staff 78 Sep 20 01:40 include
drwxr-xr-x. 2 root root 4096 Sep 20 01:40 lib
drwxrwxrwx. 2 root root 6 Sep 16 18:17 logs
drwxr-sr-x. 3 root staff 151 Sep 20 01:40 native-jni-lib
drwxrwxrwx. 2 root root 30 Sep 20 01:40 temp
drwxr-xr-x. 7 root root 81 Sep 16 18:17 webapps
drwxrwxrwx. 2 root root 6 Sep 16 18:17 work
此时可以看出执行的路径就是dockerfile中ENV CATALINA_HOME /usr/local/tomcat
[root@hadoop101 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12fb3443ef0f mycentos:1.3 "/bin/sh -c /bin/bash" 2 hours ago Up 2 hours 80/tcp beautiful_volhard
然而ps发现tomcat并没有起,因为ls -l的命令覆盖了DockerFile中的CMD [“catalina.sh”, “run”]
ENTRYPOINT:docker run 之后得参数会被当做参数传递给ENTRYPOINT,之后形成新的命令组合
Case:
制作CMD版可以查询IP信息得容器
[root@hadoop101 /]# cd mydocker/
[root@hadoop101 mydocker]# ll
总用量 8
-rw-r--r--. 1 root root 133 9月 29 10:43 Dockerfile
-rw-r--r--. 1 root root 214 9月 30 16:10 Dockerfile2
[root@hadoop101 mydocker]# vim Dockerfile3
FROM centos
RUN yum install -y curl
CMD ["curl","-s","https://ip.cn"]
[root@hadoop101 mydocker]# docker build -f /mydocker/Dockerfile3 -t myip .
[root@hadoop101 ~]# docker images myip
REPOSITORY TAG IMAGE ID CREATED SIZE
myip latest 07ad4fcefbba 4 minutes ago 276MB
[root@hadoop101 mydocker]# docker run -it myip
{"ip": "218.24.66.234", "country": "辽宁省沈阳市", "city": "联通"}
curl命令可以用来执行下载、发送各种HTTP请求,指定HTTP头部等操作。
如果系统没有curl可以使用yum install curl安装,也可以下载安装。
curl是将下载文件输出到stdout
使用命令:curl http://www.baidu.com
执行后,www.baidu.com的html就会显示在屏幕上了
这是最简单的使用方法。用这个命令获得了http://curl.haxx.se指向的页面,同样,如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地。如果下载的是HTML文档,那么缺省的将只显示文件头部,即HTML文档的header。要全部显示,请加参数-i。
问题:如果我们希望显示HTTP显示头信息,就需要加上-i参数
[root@hadoop101 mydocker]# curl -i https://ip.cn
HTTP/1.1 200 OK
Date: Tue, 08 Oct 2019 07:13:51 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d51fb1433051d9bb83dca8ad9bf3749491570518831; expires=Wed, 07-Oct-20 07:13:51 GMT; path=/; domain=.ip.cn; HttpOnly
CF-Cache-Status: DYNAMIC
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 52265088dba6d392-LAX
{"ip": "218.24.66.234", "country": "辽宁省沈阳市", "city": "联通"}
[root@hadoop101 mydocker]# docker run myip -i
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"-i\": executable file not found in $PATH": unknown.
Why?我们可以看到可执行文件找不到的报错,executable file not found之前我们说过,跟在镜像后面的是command,运行时会替换CMD的默认值。因此这里的-i替换了原来的CMD,而不是添加在原来的curl -s http://ip.cn后面。而-i根本不是命令,所以自然找不到。那么如果我们希望加入-i这参数,我们就必须重新完整的输入这个命令:
[root@hadoop101 mydocker]# docker run myip curl -s http://ip.cn -i
制作ENTRYPOINT版查询IP信息得容器
[root@hadoop101 mydocker]# vim Dockerfile4
FROM centos
RUN yum install -y curl
ENTRYPOINT ["curl","-s","https://ip.cn"]
[root@hadoop101 mydocker]# docker build -f /mydocker/Dockerfile4 -t myip2 .
[root@hadoop101 mydocker]# docker run myip2 -i
此时就不会再报错。
下面介绍的是继承
[root@hadoop101 mydocker]# vim Dockerfile4
FROM centos
RUN yum install -y curl
CMD ["curl","-s","https://ip.cn"]
ONBUILD RUN echo "father images onbuild---------886"
[root@hadoop101 mydocker]# docker build -f /mydocker/Dockerfile4 -t myip_father .
Sending build context to Docker daemon 7.168kB
Step 1/4 : FROM centos
---> 67fa590cfc1c
Step 2/4 : RUN yum install -y curl
---> Using cache
---> 5a49cc28a490
Step 3/4 : ENTRYPOINT ["curl","-s","https://ip.cn"]
---> Using cache
---> f9cf5df7c0f5
Step 4/4 : ONBUILD RUN echo "father images onbuild---------886"
---> Running in 9f9e77c00e27
Removing intermediate container 9f9e77c00e27
---> eb89c741f944
Successfully built eb89c741f944
Successfully tagged myip_father:latest
[root@hadoop101 mydocker]# vim Dockerfile5
FROM myip_father
RUN yum install -y curl
CMD ["curl","-s","https://ip.cn"]
[root@hadoop101 mydocker]# docker build -f /mydocker/Dockerfile5 -t myip_son .
Sending build context to Docker daemon 8.192kB
Step 1/3 : FROM myip_father
# Executing 1 build trigger
---> Running in b7152c74c107
father images onbuild---------886
Removing intermediate container b7152c74c107
---> 62a88bd9a34f
Step 2/3 : RUN yum install -y curl
---> Running in 3b75719030f3
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Package curl-7.29.0-54.el7.x86_64 already installed and latest version
Nothing to do
Removing intermediate container 3b75719030f3
---> 0db722fd1b51
Step 3/3 : CMD ["curl","-s","https://ip.cn"]
---> Running in 932472dc8a63
Removing intermediate container 932472dc8a63
---> bce3f7f4efcd
Successfully built bce3f7f4efcd
Successfully tagged myip_son:latest
自定义镜像Tomcat9
第一步:mkdir -p /ghj/mydockerfile/tomcat9
[root@hadoop101 ~]# mkdir -p /ghj/mydockerfile/tomcat9
[root@hadoop101 /]# cd /ghj/mydockerfile/tomcat9/
第二步:在上述目录下touch.txt
[root@hadoop101 tomcat9]# touch c.txt
[root@hadoop101 tomcat9]# ll
总用量 0
-rw-r--r--. 1 root root 0 10月 8 16:19 c.txt
第三步:将jdk和tomcat安装的压缩包拷贝进上一步目录
[root@hadoop101 tomcat9]# ll
总用量 197352
-rw-r--r--. 1 root root 12326996 10月 8 16:45 apache-tomcat-9.0.26.tar.gz
-rw-r--r--. 1 root root 0 10月 8 16:19 c.txt
-rw-r--r--. 1 root root 0 10月 8 16:59 Dockerfile
-rw-r--r--. 1 root root 189756259 10月 8 16:45 jdk-8u161-linux-x64.tar.gz
第四步:在/ghj/mydockerfile/tomcat9目录下新建Dockerfile文件
[root@hadoop101 tomcat9]# vim Dockerfile
FROM centos
MAINTAINER ghj.com>
#把宿主机当前上下文的c.txt拷贝到容器/usr/local/路径下
COPY c.txt /usr/local/cincontainer.txt
#把java与tomcat添加到容器中
ADD apache-tomcat-9.0.26.tar.gz /usr/local/
ADD jdk-8u161-linux-x64.tar.gz /usr/local/
#安装vim编辑器
RUN yum -y install vim
#设置工作访问时候的WORKDIR路径,登录落脚点
ENV MYPATH /usr/local
WORKDIR $MYPATH
#配置java与tomcat环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_161
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.26
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.26
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
#容器运行时监听的端口
EXPOSE 8080
#启动时运行tomcat
#ENTRYPOINT ["usr/local/apache-tomcat-9.0.26/bin/startup.sh"]
#CMD ["usr/local/apache-tomcat-9.0.26/bin/catalina,sh","run"]
CMD /usr/local/apache-tomcat-9.0.26/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.26/bin/logs/catalina.out
[root@hadoop101 tomcat9]# docker build -t ghjtomcat9 .
[root@hadoop101 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ghjtomcat9 latest d824ff2c00c6 About a minute ago 732MB
myip_son latest bce3f7f4efcd 23 hours ago 277MB
myip_father latest eb89c741f944 23 hours ago 276MB
myip2 latest f9cf5df7c0f5 24 hours ago 276MB
myip latest 166ac61dcd1b 24 hours ago 276MB
mycentos 1.3 d9dff72321ec 8 days ago 350MB
ghj/centos latest ea55868fb6d4 10 days ago 202MB
atdixin/mytomcat 7.0 38d1e8f04740 13 days ago 508MB
nginx latest f949e7d76d63 2 weeks ago 126MB
tomcat latest 8973f493aa0a 2 weeks ago 508MB
centos latest 67fa590cfc1c 7 weeks ago 202MB
hello-world latest fce289e99eb9 9 months ago 1.84kB
training/webapp latest 6fae60ef3446 4 years ago 349MB
第六步:RUN
[root@hadoop101 ~]# docker run -d -p 9080:8080 --name myt9 -v /ghj/mydockerfile/tomcat9/test:/usr/local/apache-tomcat-9.0.26/webapps/test -v /ghj/mydockerfile/tomcat9/tomcat9logs/:/usr/local/apache-tomcat-9.0.26/logs --privileged=true ghjtomcat9
55f550de949f063ec987fa678e1acc6e4692400405f1316912019297541ba4bd
[root@hadoop101 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55f550de949f ghjtomcat9 "/bin/sh -c '/usr/lo…" 7 seconds ago Up 6 seconds 0.0.0.0:9080->8080/tcp myt9
[root@hadoop101 ~]# docker exec 55f550de949f ls -l
total 0
drwxr-xr-x. 1 root root 45 Oct 9 07:02 apache-tomcat-9.0.26
drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
-rw-r--r--. 1 root root 0 Oct 8 08:19 cincontainer.txt
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 include
drwxr-xr-x. 8 10 143 255 Dec 20 2017 jdk1.8.0_161
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 49 Aug 1 01:09 share
drwxr-xr-x. 2 root root 6 Apr 11 2018 src
[root@hadoop101 tomcat9]# docker exec 55f550de949f ls -l /usr/local
total 0
drwxr-xr-x. 1 root root 45 Oct 9 07:02 apache-tomcat-9.0.26
drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
-rw-r--r--. 1 root root 0 Oct 8 08:19 cincontainer.txt
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 include
drwxr-xr-x. 8 10 143 255 Dec 20 2017 jdk1.8.0_161
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 49 Aug 1 01:09 share
drwxr-xr-x. 2 root root 6 Apr 11 2018 src
第七步:验证
第八步:结合前述的容器卷将测试的web服务test发布
[root@hadoop101 tomcat9]# ll
总用量 197356
-rw-r--r--. 1 root root 12326996 10月 8 16:45 apache-tomcat-9.0.26.tar.gz
-rw-r--r--. 1 root root 0 10月 8 16:19 c.txt
-rw-r--r--. 1 root root 1033 10月 9 15:00 Dockerfile
-rw-r--r--. 1 root root 189756259 10月 8 16:45 jdk-8u161-linux-x64.tar.gz
drwxr-xr-x. 2 root root 6 10月 9 15:06 test
drwxr-xr-x. 2 root root 197 10月 9 15:06 tomcat9logs
[root@hadoop101 tomcat9]# docker exec 55f550de949f java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
总体概述:
[root@hadoop101 tomcat9]# cd test/
[root@hadoop101 test]# pwd
/ghj/mydockerfile/tomcat9/test
web.xml
[root@hadoop101 test]# mkdir WEB-INF
[root@hadoop101 test]# cd WEB-INF/
[root@hadoop101 WEB-INF]# vim web.xml
"1.0" encoding="UTF-8"?>
-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
-name>test</display-name>
</web-app>
a.jsp
[root@hadoop101 WEB-INF]# cd ..
[root@hadoop101 test]# ll
总用量 0
drwxr-xr-x. 2 root root 32 10月 9 15:42 WEB-INF
[root@hadoop101 test]# vim a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
-equiv="content-type" content="text/html;charset=UTF-8">
Insert title here</title>
</head>
----------welcome-----------
<%="i am in docker tomcat self"%>
<% System.out.println("============docker tomcat self");%>
</body>
</html>
[root@hadoop101 test]# pwd
/ghj/mydockerfile/tomcat9/test
[root@hadoop101 test]# ll
总用量 4
-rw-r--r--. 1 root root 470 10月 9 16:09 a.jsp
drwxr-xr-x. 2 root root 32 10月 9 15:42 WEB-INF
[root@hadoop101 test]# docker exec 55f550de949f ls -l /usr/local/apache-tomcat-9.0.26/webapps/test
total 4
drwxr-xr-x. 2 root root 32 Oct 9 07:42 WEB-INF
-rw-r--r--. 1 root root 470 Oct 9 08:09 a.jsp
注:宿主机上写的东西根据容器数据卷的共享,容器中也会有的。
测试
重启容器
[root@hadoop101 test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55f550de949f ghjtomcat9 "/bin/sh -c '/usr/lo…" About an hour ago Up About an hour 0.0.0.0:9080->8080/tcp myt9
[root@hadoop101 test]# docker restart 55f550de949f
55f550de949f
[root@hadoop101 test]# vim a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
-equiv="content-type" content="text/html; charset=UTF-8">
Insert title here</title>
</head>
----------welcome-----------
<%="i am in docker tomcat self 1111"%>
<% System.out.println("============docker tomcat self");%>
</body>
</html>
[root@hadoop101 tomcat9logs]# ll
总用量 88
-rw-r-----. 1 root root 33278 10月 9 16:31 catalina.2019-10-09.log
-rw-r-----. 1 root root 33464 10月 9 16:50 catalina.out
-rw-r-----. 1 root root 0 10月 9 15:06 host-manager.2019-10-09.log
-rw-r-----. 1 root root 9671 10月 9 16:31 localhost.2019-10-09.log
-rw-r-----. 1 root root 1591 10月 9 16:50 localhost_access_log.2019-10-09.txt
-rw-r-----. 1 root root 0 10月 9 15:06 manager.2019-10-09.log
[root@hadoop101 tomcat9logs]# cat catalina.out
============docker tomcat self
============docker tomcat self
============docker tomcat self
============docker tomcat self
============docker tomcat self
============docker tomcat self
刷新几次就会打印几次