Docker基础篇6:Dockerfile指令(1)

2024-08-28 19:08

本文主要是介绍Docker基础篇6:Dockerfile指令(1),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、Dockerfile指令

(1)FROM指令:构建的新镜像是基于那个镜像,例如:FROM centos:7。
(2)MAINTAINER指令:镜像维护者姓名或者邮件地址。
(3)RUN指令:构建镜像时运行的shell命令,写法一:RUN["yum","install","net-tools"];写法二:RUN yum install net-tools。
(4)CMD指令:运行容器时执行的shell命令。
(5)EXPOSE指令:声明容器运行的服务端口,例如 EXPOSE 80 443。
(6)ENV指令:设置容器内环境变量,例如:ENV MYSQL_ROOT_PASSWORD 123。
(7)ADD指令:拷贝文件或目录到镜像,如果是URL或者压缩包会自动下载或自动解压缩。语法:ADD <src> .. <dest>。例子1:ADD https://www.baidu.com/wx.tar.gz /usr/local;例子2:ADD nginx1.12.tar.gz  /usr/local。
(8)COPY指令:拷贝文件或目录到镜像,语法同ADD。
(9)ENTRYPOINT指令:运行容器时执行的Shell命令。例子 ENTRYPOINT /bin/bash -c 'start.sh'。
(10)VOLUME指令:指定容器挂载点到宿主主机自动生成的目录或其他容器。例如:VOLUME ["/var/lib/mysql"]
(11)USER指令:为RUN、CMD和ENTRYPOINT执行命令指定运行用户。语法 USER <user>[:group] or USER <UID>[:GID]>。例子USER baidu。
(12)WORKDIR指令:为RUN、CMD、ENTRYPOINT、COPY和ADD设置工作目录。例子:WORKDIR /opt
(13)HEALTHCHECK指令:检查健康。HEALTHCHECK --interval=5m --timeout=3s --retries=3 CMD curl -f http://localhost/ || exit 1
(14)ARG指令:在构建镜像时指定一些参数。

【RUN指令使用的例子】

RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel

【ADD指令使用的例子】

#例子1:
ADD nginx-1.12.1.tar.gz /tmp#例子2:
RUN cd /tmp/nginx-1.12.1 && \
./configure --prefix=/usr/local/nginx && \
make -j 2 && \
make install

 【COPY指令例子】

COPY nginx.conf /usr/local/nginx/conf

【WORKDIR指令例子】

WORKDIR /usr/local/nginx

【CMD指令例子】

CMD ["./sbin/nginx", "-g", "daemon off;"]

2、Build镜像命令

【 docker build语法】

[root@aliyun205 ~]# docker build --help
Usage:  docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:--add-host list           Add a custom host-to-IP mapping (host:ip)--build-arg list          Set build-time variables--cache-from strings      Images to consider as cache sources--cgroup-parent string    Optional parent cgroup for the container--compress                Compress the build context using gzip--cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period--cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota-c, --cpu-shares int          CPU shares (relative weight)--cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)--cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)--disable-content-trust   Skip image verification (default true)-f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')--force-rm                Always remove intermediate containers--iidfile string          Write the image ID to the file--isolation string        Container isolation technology--label list              Set metadata for an image-m, --memory bytes            Memory limit--memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap--network string          Set the networking mode for the RUN instructions during build (default "default")--no-cache                Do not use cache when building the image--pull                    Always attempt to pull a newer version of the image-q, --quiet                   Suppress the build output and print image ID on success--rm                      Remove intermediate containers after a successful build (default true)--security-opt strings    Security options--shm-size bytes          Size of /dev/shm-t, --tag list                Name and optionally a tag in the 'name:tag' format--target string           Set the target build stage to build.--ulimit ulimit           Ulimit options (default [])

语法使用注意事项:(1)-t, --tag list # 镜像名称;
(2)-f, --file string # 指定Dockerfile文件位置。

 

这篇关于Docker基础篇6:Dockerfile指令(1)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1115634

相关文章

Windows Docker端口占用错误及解决方案总结

《WindowsDocker端口占用错误及解决方案总结》在Windows环境下使用Docker容器时,端口占用错误是开发和运维中常见且棘手的问题,本文将深入剖析该问题的成因,介绍如何通过查看端口分配... 目录引言Windows docker 端口占用错误及解决方案汇总端口冲突形成原因解析诊断当前端口情况解

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

mysql的基础语句和外键查询及其语句详解(推荐)

《mysql的基础语句和外键查询及其语句详解(推荐)》:本文主要介绍mysql的基础语句和外键查询及其语句详解(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录一、mysql 基础语句1. 数据库操作 创建数据库2. 表操作 创建表3. CRUD 操作二、外键

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

Docker镜像修改hosts及dockerfile修改hosts文件的实现方式

《Docker镜像修改hosts及dockerfile修改hosts文件的实现方式》:本文主要介绍Docker镜像修改hosts及dockerfile修改hosts文件的实现方式,具有很好的参考价... 目录docker镜像修改hosts及dockerfile修改hosts文件准备 dockerfile 文

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

Docker镜像pull失败两种解决办法小结

《Docker镜像pull失败两种解决办法小结》有时候我们在拉取Docker镜像的过程中会遇到一些问题,:本文主要介绍Docker镜像pull失败两种解决办法的相关资料,文中通过代码介绍的非常详细... 目录docker 镜像 pull 失败解决办法1DrQwWCocker 镜像 pull 失败解决方法2总

C#基础之委托详解(Delegate)

《C#基础之委托详解(Delegate)》:本文主要介绍C#基础之委托(Delegate),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 委托定义2. 委托实例化3. 多播委托(Multicast Delegates)4. 委托的用途事件处理回调函数LINQ

通过Docker Compose部署MySQL的详细教程

《通过DockerCompose部署MySQL的详细教程》DockerCompose作为Docker官方的容器编排工具,为MySQL数据库部署带来了显著优势,下面小编就来为大家详细介绍一... 目录一、docker Compose 部署 mysql 的优势二、环境准备与基础配置2.1 项目目录结构2.2 基

Nginx指令add_header和proxy_set_header的区别及说明

《Nginx指令add_header和proxy_set_header的区别及说明》:本文主要介绍Nginx指令add_header和proxy_set_header的区别及说明,具有很好的参考价... 目录Nginx指令add_header和proxy_set_header区别如何理解反向代理?proxy