Docker-DockerFile

DockerFile构建过程

1、每个保留关键字(指令)都必须是大写字母

2、执行从上到下的顺序

3、#表示注释

4、每一个指令都会创建提交一个新的镜像层,并提交

DockerFile指令

FROM		       # 基础镜像,一切从这里开始构建
MAINTAINER	       # 镜像构建作者,姓名+邮箱
RUN		       # 镜像构建是需要运行的命令
ADD		       # 步骤,tomcat镜像,添加内容,tomcat压缩包
WORDDIR		       # 镜像工作目录
VOLUME		       # 挂载的目录
EXPOSE		       # 保留端口配置
CMD		       # 指定这个容器启动时需要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT	       # 指定这个容器启动时需要运行的命令,可以追加命令
ONBUILD		       # 当构建一个被继承 DockerFile这个时候就会运行ONBUILD的指令。触发指令
COPY	               # 类似ADD,将我们文件拷贝到镜像中
ENV		       # 构建的时候设置环境变量

DockerFile-1

创建一个dockerfile文件

FROM centos

VOLUME ["volume01","volume02"]	#构建镜像时自动匿名挂载这两个卷在根目录

CMD echo "----end----"
CMD /bin/bash

查看构建的镜像元数据

docker inspect id
        "Mounts": [
            {
                "Type": "volume",
                "Name": "9c037f18c4d02ced52396425133f2200be2cc0cec8a2468690525ec724f0ffc1",
                "Source": "/var/lib/docker/volumes/9c037f18c4d02ced52396425133f2200be2cc0cec8a2468690525ec724f0ffc1/_data",
                "Destination": "volume01",	#匿名挂载的卷
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "086141e7cafac32ebc5bdbb489a6957e985b6028b0368944042c8faa09075420",
                "Source": "/var/lib/docker/volumes/086141e7cafac32ebc5bdbb489a6957e985b6028b0368944042c8faa09075420/_data",
                "Destination": "volume02",	#匿名挂载的卷
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }

若构建镜像时没有挂载卷,则需要通过 “-v 卷名:容器内路径” 手动进行挂载

构建镜像

docker build -f /home/docker-test-colume/dockerfile1 -t home/centos7:1.0 .

DokcerFile-2

1、编写Dockerfile文件

FROM cenots
MAINTAINER Cai<cloud_chl@163.com>

ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim-enhanced
RUN yum -y install net-tools

EXPOSE 80

CMD echo "$MYPATH"
CMD echo "----build end ---"
CMD /bin/bash

2、构建镜像

docker build -f dockerfile文件路径 -t 自定义镜像名(小写):[tag] .
Successfully built 452d5ccf8515
Successfully tagged caicentos:1.0	#表示构建成功

3、编写java项目环境dockerfile

FROM centos:7
MAINTAINER cai<cloud_chl@163.com>

COPY readme.txt /usr/local/readme.txt

ADD apache-tomcat-8.5.61.tar.gz /usr/local/
ADD jdk-8u281-linux-x64.tar.gz /usr/local

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYPATH

ENV JAVA_HOME /usr/local/jdk1.8.0_281
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.61
ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.61
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

EXPOSE 8080

CMD $CATALINA_HOME/bin/startup.sh && tailf $CATALINA_HOME/logs/catalina.out

4、构建镜像

docker run -d -p 9090:8080 -v /home/tomcat/webapps:/usr/local/apache-tomcat-8.5.61/webapps/web -v /home/tomcat/logs:/usr/local/apache-tomcat-8.5.61/logs tomcat:1.0
posted @ 2021-08-26 11:13  Cai_HL  阅读(32)  评论(0编辑  收藏  举报
>