作用: 利用文本定义某个镜像需要的东西,然后构建成新的镜像,非常适合CICD。而commit需要手动进入容器,手动增强镜像功能,在手动commit形成新的镜像
官网: https://docs.docker.com/engine/reference/builder/
参考Tomcat的Dockerfile实例: https://github.com/docker-library/tomcat/blob/be77ba3410933594589a61415d302686ab9798cb/10.0/jdk8/corretto-al2/Dockerfile
官网: https://docs.docker.com/engine/reference/builder/#from
官网: https://docs.docker.com/engine/reference/builder/#maintainer-deprecated
注意: 新版Dockerfile官方建议使用 LABEL 进行取代
官网: https://docs.docker.com/engine/reference/builder/#label
使用时期: 构建镜像时执行的命令
官网: https://docs.docker.com/engine/reference/builder/#run
官网: https://docs.docker.com/engine/reference/builder/#expose
官网: https://docs.docker.com/engine/reference/builder/#workdir
即:进入容器后当前所在的目录
官网: https://docs.docker.com/engine/reference/builder/#user
官网: https://docs.docker.com/engine/reference/builder/#env
官网: https://docs.docker.com/engine/reference/builder/#add
官网: https://docs.docker.com/engine/reference/builder/#copy
//格式 == COPY src dest
COPY 源路径文件、目录 镜像路径(不用事先建好)
使用时期: 用户使用镜像时(即run)自动执行的命令
官网: https://docs.docker.com/engine/reference/builder/#cmd
如果dockerfile文件末尾有定义CMD命令,当你使用容器时千万别多手加入其他命令,如/bin/bash
//主动执行dockerfile定义的 sh catalina.sh run 命令
docker run -d -p 8080:8080 --name=tomcat tomcat镜像ID
//会将 CMD ["catalina.sh", "run"] 替换成 ["/bin/bash"] == 导致tomcat没执行启动命令
//所以run起服务型容器时,建议别手多多加后续的命令
docker run --rm -it -p 8080:8080 --name=tomcat-test tomcat /bin/bash
//如果是命令型容器,如liquibase、jdk镜像,则要加命令了
官网: https://docs.docker.com/engine/reference/builder/#entrypoint
//单独使用ENTRYPOINT == 容器启动时执行 sh executable.sh param1 param2
ENTRYPOINT ["executable.sh", "param1", "param2"]
//配合CMD使用 == 容器启动时执行 sh executable.sh param1 param2 param3
ENTRYPOINT ["executable.sh", "param1", "param2"]
CMD ["param3"]
//容器启动时默认执行 nginx -c /etc/nginx/nginx.conf
//如果run时自定义传入my.conf字符,则容器启动执行则会变成 nginx -c my.conf
ENTRYPOINT["nginx", "-c"]
CMD ["/etc/nginx/nginx.conf"]
特别注意1: 每次Run都会自动跳回WORKDIR目录
特别注意2: ADD从宿主机迁移文件进入镜像时,必须将宿主机的文件跟Dockerfile放在同一个目录下,不得使用其他目录的绝对路径
特别注意3: Centos8的yum镜像已经停止使用,故需换成阿里云的centos stream的yum镜像 == 阿里云yum镜像切换官网
Centos-7.repo
[base]
name=CentOS-8-stream - Base - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos/8-stream/BaseOS/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[extras]
name=CentOS-8-stream - Extras - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos/8-stream/extras/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[centosplus]
name=CentOS-8-stream - Plus - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos/8-stream/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[PowerTools]
name=CentOS-8-stream - PowerTools - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos/8-stream/PowerTools/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[AppStream]
name=CentOS-8-stream - AppStream - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos/8-stream/AppStream/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
Dockerfile
FROM centos:latest
# 定义镜像描述信息
LABEL "author"="LinRuChang"
LABEL version="1.0"
LABEL description="This text illustrates"
# 进入容器后的目录
WORKDIR /www/server
# 阿里镜像源
# 特别注意:每次Run都会自动跳回WORKDIR目录
RUN cd /etc/yum.repos.d/ && echo "目录:$(pwd)" && ( ls -l *.repo &>/dev/null ) && ( [ -d centos.back ] || mkdir centos.back ) && mv *.repo centos.back/
# 阿里云的yum镜像信息 == centos8已经停更故使用 centos stream的信息
ADD Centos-7.repo /etc/yum.repos.d/
RUN echo "/etc/yum.repos.d/的文件: $( ls -l /etc/yum.repos.d )"
# RUN wget http://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum clean all
RUN yum makecache
# 安装wget、vim、ifconfig、clean工具
RUN yum install wget -y
RUN yum -y install vim
RUN yum -y install net-tools
RUN yum -y install ncurses
# 安装JAVA
ADD jdk-8u202-linux-x64.tar.gz.gz /www/server/jdk8/
RUN [ -d /www/server/jdk8/jdk1.8.0_202 ] && echo 'jdk-8u202-linux-x64.tar.gz.gz压缩包解压成功!!!!'
ENV JAVA_HOME /www/server/jdk8/jdk1.8.0_202
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/
ENV PATH $PATH:$JAVA_HOME/bin/
EXPOSE 80
CMD echo "当前目录: $(pwd)"
CMD echo "成功====================OK"
CMD /bin/bash
Dockerfile
# 基于java:8的镜像制作
FROM java:8
# 作者
MAINTAINER LinRuChang
# 将jar包传到容器上
RUN mkdir -p /www/server/docker
ADD docker-springboot-test-0.0.1-SNAPSHOT.jar /www/server/docker/docker-springboot-test-0.0.1-SNAPSHOT.jar
# run时执行的命令
ENTRYPOINT cd /www/server/docker && java -jar docker-springboot-test-0.0.1-SNAPSHOT.jar
EXPOSE 8080
# 制作镜像开始
docker build -t my-spring-boot:1.0 .
整个操作流程