Dockerfile
- Dockerfile是用来构建docker镜像的构建文件,是由一系列命令和参数构成的脚本。
保留关键字
Docker–Dockerfile保留关键字
- FROM 基础镜像,当前新镜像是基于哪个镜像的
- MAINTAINER 镜像维护者的姓名和邮箱地址
- RUN 容器构建时需要运行的命令
- EXPOSE 当前容器对外暴露出的端口号
- WORKDIR 指定在创建容器后,终端默认登录的进来的工作目录,落脚点,默认是根目录
/ - ENV 用来在构建镜像过程中设置环境变量
- ADD 将宿主机目录下的文件拷贝进镜像且ADD命令会自动处理URL和解压tar压缩包
- COPY 类似ADD,拷贝文件和目录到镜像中。eg.
COPY src dest or COPY ["src","desc"] - VOLUME 数据容器卷,用于数据保存和持久化工作
- CMD 指定一个容器启动时要运行的命令。DockerFile中可以有多个CMD指令,但只有最后一个生效(docker build时还是会全部执行),CMD会被
docker run 之后的参数替换 - ENTRYPOINT 指定一个容器启动时要运行的命令。目的和CMD一样,但
docker run 之后的参数会追加而不是替换 - ONBUILD 当构建一个被继承的DockerFile时运行命令,父镜像在被子继承后父镜像的ONBUILD被触发
编写Dockerfile案例
FROM centos
MAINTAINER hnn<xxx@qq.com>
ENV MYPATH /usr
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "successed----------------ok!"
CMD /bin/bash
root@ubuntu18-04:/mydocker
root@ubuntu18-04:/mydocker
[root@c32cf4317478 usr]
FROM centos
RUN yum install -y curl
ENTRYPOINT ["curl","-s","http://www.baidu.com"]
root@ubuntu18-04:/mydocker
FROM centos
RUN yum install -y curl
ENTRYPOINT ["curl","-s","http://www.baidu.com"]
ONBUILD RUN echo "hello world!--------------------------son"
root@ubuntu18-04:/mydocker
FROM mybaidu_father
RUN yum install -y curl
ENTRYPOINT ["curl","-s","http://www.baidu.com"]
root@ubuntu18-04:/mydocker
Sending build context to Docker daemon 5.12kB
Step 1/3 : FROM mybaidu_father
---> Running in dfa0ea3e6bf8
hello world!--------------------------son
FROM centos
MAINTAINER hnn<xxx@qq.com>
COPY c.txt /usr/local/container.txt
ADD jdk-8u301-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.52.tar.gz /usr/local
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_301
ENV CLASSPATH ${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.52
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.52
ENV PATH ${PATH}:${JAVA_HOME}/bin:${CATALINA_HOME}/lib:${CATALINA_HOME}/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.52/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.52/bin/logs/catalina.out
root@ubuntu18-04:/home/hnn/linux/mydockerfile/tomcat9
docker run -d -p 9080:8080 --name mytomcat9 -v /home/hnn/linux/mydockerfile/test:/usr/local/apache-tomcat-9.0.52/webapps/test -v /home/hnn/linux/mydockerfile/tomcat9logs:/usr/local/apache-tomcat-9.0.52/logs --privileged=true hnntomcat9
http://localhost:9080/
root@ubuntu18-04:/home/hnn/linux/mydockerfile/tomcat9
java version "1.8.0_301"
Java(TM) SE Runtime Environment (build 1.8.0_301-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.301-b09, mixed mode)
<--->自定义的tomcat9上发布演示</--->
<--->宿主机共享文件夹创建文件</--->
<--->/test/WEB-INF/web.xml</--->
<?xml version="1.0" encoding="UTF-8"?>
<web-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">
<display-name>test</display-name>
</web-app>
<--->/test/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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title herek</title>
</head>
<body>
---------welcome---------
<%="i am in docker tomcat self"%>
<br>
<br>
<% System.out.println("============docker tomcat self");%>
</body>
</html>
root@ubuntu18-04:/home/hnn/linux/mydockerfile/test
WEB-INF
a.jsp
root@ubuntu18-04:/home/hnn/linux/mydockerfile/test
http://localhost:9080/test/a.jsp
root@ubuntu18-04:/home/hnn/linux/mydockerfile/tomcat9logs
catalina.2021-08-11.log catalina.out host-manager.2021-08-11.log localhost.2021-08-11.log localhost_access_log.2021-08-11.txt manager.2021-08-11.log
root@ubuntu18-04:/home/hnn/linux/mydockerfile/tomcat9logs
|