docker镜像包制作方法(如何使用Docker制作镜像文件)
Docker制作镜像文件
简介在工作当中我们往往在dockerhub上找不到自己想要的镜像,还有很多场景是我们的项目对基础环境有特殊要求,这种情况下就需要运维人员来根据项目要求来定制镜像了,本文介绍了如何定制一个tomcat服务所需的镜像。
准备工作我的项目需要的java版本是jdk-8u151,tomcat版本是Server number: 10.0.2x,操作系统是centos7,一般来说要尽量收集多项目依赖环境,以免因环境不一致造成不必要的麻烦
ll jdk-8u151-linux-x64.tar.gz tomcat-demo.zip
-rwxrwxrwx 1 root root 189736377 Nov 28 09:32 jdk-8u151-linux-x64.tar.gz*
-rwxrwxrwx 1 root root 8171286 Nov 28 15:32 tomcat-demo.zip*
Dockerfile为制作镜像的配置文件
FROM centos:centos7
MAINTAINER leenhem
RUN mkdir /usr/local/jdk
WORKDIR /usr/local/jdk
ADD jdk-8u151-linux-x64.tar.gz /usr/local/jdk
ADD tomcat /usr/local/tomcat
ENV JAVA_HOME /usr/local/jdk/jdk1.8.0_151
ENV JRE_HOME /usr/local/jdk/jdk1.8.0_151/jre
ENV CLASSPATH .:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$JRE_HOME/bin:$PATH
ENV CATALINA_HOME=/usr/local/tomcat
WORKDIR /usr/local/tomcat
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
这里我构建的是一个项目需要的基础镜像模板
#这里是一个基础镜像,如果以后的项目是使用的这个环境,都使用这一个镜像
docker build -t tomcat:v1 .
[ ] Building 38.3s (12/12) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 32B 0.1s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:centos7 2.3s
=> [internal] load build context 1.1s
=> => transferring context: 8.99MB 1.1s
=> [1/7] FROM docker.io/library/centos:centos7@sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407 22.1s
=> => resolve docker.io/library/centos:centos7@sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407 0.0s
=> => sha256:c73f515d06b0fa07bb18d8202035e739a494ce760aa73129f60f4bf2bd22b407 1.20kB / 1.20kB 0.0s
=> => sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f 529B / 529B 0.0s
=> => sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9 2.75kB / 2.75kB 0.0s
=> => sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc 76.10MB / 76.10MB 15.2s
=> => extracting sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc 6.2s
=> [2/7] RUN mkdir /usr/local/jdk 1.4s
=> [3/7] WORKDIR /usr/local/jdk 0.0s
=> [4/7] ADD jdk-8u151-linux-x64.tar.gz /usr/local/jdk 7.4s
=> [5/7] ADD tomcat /usr/local/tomcat 0.1s
=> [6/7] WORKDIR /usr/local/tomcat 0.1s
=> [7/7] RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 0.5s
=> exporting to image 4.1s
=> => exporting layers 4.1s
=> => writing image sha256:95ea78f3f8ba29ad7d9f015061e87f6f53099a6261917d6a5021a59cbe813715 0.0s
=> => naming to docker.io/library/tomcat:v1
上面是个基础镜像,想要部署项目,还需要把项目放到镜像内
我这里又加了一个名字 Dockerfile 的配置文件,用于部署项目时生成镜像
cat > service_dockerfile <<EOF
FROM tomcat:v1
RUN mkdir -p /usr/local/tomcat/webapps/test/
COPY test /usr/local/tomcat/webapps/test/
EOF
docker build -f ./service_dockerfile -t tomcat_service:v1 .
[ ] Building 0.8s (8/8) FINISHED
=> [internal] load build definition from service_dockerfile 0.1s
=> => transferring dockerfile: 150B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/tomcat:v1 0.0s
=> CACHED [1/3] FROM docker.io/library/tomcat:v1 0.0s
=> [internal] load build context 0.1s
=> => transferring context: 60B 0.0s
=> [2/3] RUN mkdir -p /usr/local/tomcat/webapps/test/ 0.5s
=> [3/3] COPY test /usr/local/tomcat/webapps/test/ 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:698a3608f716758d51d259d0017f2311217d97f8ec00e5187281afc42f352b72 0.0s
=> => naming to docker.io/library/tomcat_service:v1
可以看到最后一步就把项目test目录下的文件都复制到镜像里了,然后就可以使用生成的tomcat_service:v1进行部署项目了。
,免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com