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文件位置。