docker---dockerfile自定义镜像指令

dockerfile指令

  • FROM #指定所基于的镜像名称及其标签来创建新镜像。

  • RUN #在镜像中执行命令。

  • ADD #将文件或目录复制到镜像中。可以使用 URL 作为源文件。

  • COPY #与 ADD 类似,将文件或目录复制到镜像中,但不支持 URL 作为源文件。

  • CMD #指定运行容器时要执行的命令,如果在运行时指定了命令,则覆盖 CMD。

  • ENTRPOINT #指定容器启动时要运行的命令,可以将容器作为可执行程序使用。

  • ENV #设置环境变量,可以在后续的指令中使用。

  • ARG #定义构建时候用到的一些参数,可以通过 --build-arg 参数传入。

  • WORKDIR #设置镜像的工作目录。

  • ONBUILD #用于定制子镜像,当子镜像被作为其他镜像的基础镜像时,ONBUILD 中的指令会被触发。

  • USER #设置运行容器时要使用的 UID。

  • HEALTHCHECK #检查容器是否健康,并可以指定检查的方式和间隔时间。

  • EXPOSE #声明在运行时将要使用的端口。

  • VOLUME #创建一个挂载点,可以在主机和容器之间共享数据。

  • LABEL #为镜像添加元数据,可以帮助用户更好地理解镜像的属性、用途等信息。

-------->

  • MAINTAINER #用于指定 Dockerfile 作者的信息。

  • SHELL #用于指定 Dockerfile 中后续 RUN 命令所使用的 shell 环境变量,通常用于指定非默认的 shell。

dockerfile演示

[root@centos201 centos]# cat Dockerfile 
# 指定基础镜像
FROM centos:7

# 声明作者信息,官方已弃用,建议使用LABEL
MAINTAINER  xxxxxx

# LABEL可以基于key=value方式指定信息
LABEL school=bd \
      class=01 \
      address=北京市 \
      office=www.lzh.com \
      email=xxxxx.com \


# 修改centos镜像的yum源为国内地址,并安装nginx,sshd服务(运行时,不能阻塞RUN指令,否则终止编译)
RUN sed -e 's|^mirrorlist=|#mirrorlist=|g' \
        -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos|g' \
        -i.bak \
        /etc/yum.repos.d/CentOS-*.repo && \
    yum -y install epel-release && \
    sed -e 's!^metalink=!#metalink=!g' \
        -e 's!^#baseurl=!baseurl=!g' \
        -e 's!https\?://download\.fedoraproject\.org/pub/epel!https://mirrors.tuna.tsinghua.edu.cn/epel!g' \
        -e 's!https\?://download\.example/pub/epel!https://mirrors.tuna.tsinghua.edu.cn/epel!g' \
        -i /etc/yum.repos.d/epel*.repo && \
    yum -y install nginx openssh-server initscripts && \
    rm -rf /var/cache/yum


# 指定工作目录,若不指定则默认路径为"/"
WORKDIR /usr/local/nginx/html 

# 将宿主机的文件拷贝到容器的指定路径
COPY config/games.conf  /etc/nginx/conf.d/
COPY scripts/start.sh /

# COPY softwares/test-games.tar.gz /usr/local/nginx/html/
## RUN tar xf /usr/local/nginx/html/test-games.tar.gz -C /usr/local/nginx/html/
### RUN cd /usr/local/nginx/html && tar xf test-games.tar.gz
#### RUN cd /usr/local/nginx/html && tar xf test-games.tar.gz && rm -f test-games.tar.gz
# RUN tar xf test-games.tar.gz && rm -f test-games.tar.gz

# 如果文件为tar包,会自动解压并删除源文件。
ADD softwares/test-games.tar.gz /usr/local/nginx/html/

# 暴露容器的80端口
EXPOSE 80 22 

# 将容器的指定路径进行持久化,会产生随机(匿名)存储卷
VOLUME /usr/local/nginx/html/

# 向容器传递环境变量,基于key=value的语法格式
ENV school=bd \
    class=01 \
    ADMIN=123456

# 容器启动时的命令
# CMD ["tail","-f","/etc/hosts"]
# CMD ["nginx","-g","daemon off;"]
CMD ["bash","-x","/start.sh"]
[root@centos201 centos]# 
posted @ 2024-02-07 17:13  只为心情愉悦  阅读(33)  评论(0编辑  收藏  举报